Re: NSTableView problems [Java]
Re: NSTableView problems [Java]
- Subject: Re: NSTableView problems [Java]
- From: Tyler LaGrange <email@hidden>
- Date: Thu, 20 Sep 2001 15:33:42 -0400
This is a very BASIC sample of a table datasource. I did this once
before and it turned out I was using some very stupid methods - but I
THINK i have it working a little better now. This will only get you
started really - but here goes what I have:
First - I have a nib which has a 2 column table (make sure you label the
identifiers through the "Info" palette to get this example to work
right, "Column 1" and "Column 2")
Second - I made (and instantiated) a controller in IB called
MyController with ONE outlet called myTableView and I connected that to
the NSTableView in the nib.
Third - I built a custom class (myTableData.java) to handle the table
data and added it to my project. Here is the code:
===========================
//
// myTableData.java
// TableTest (quick table data sample)
//
import com.apple.cocoa.foundation.*;
import com.apple.cocoa.application.*;
public class myTableData
{
// I just use an NSMutableArray for each column in the table.
// You could use an Array of Arrays or a java Collection class if
you want.
private NSMutableArray column1Array = new NSMutableArray();
private NSMutableArray column2Array = new NSMutableArray();
// this method is REQUIRED. it lets the table to know how many rows
to display.
public int numberOfRowsInTableView(NSTableView tv)
{
// you could use tv if you want to make this datasource for more
than one table.
// hopefully column1 and column2 stay in sync...
return column1Array.count();
}
// this method is REQUIRED. it lets the table know what value to
display for this
// row and column combination...
public Object tableViewObjectValueForLocation(NSTableView tv,
NSTableColumn tc, int i)
{
// you could use tv if you want to make this datasource for more
than one table.
// tc.identifier() gives me the "Identifier" value from the
inspector window in IB
// for the specific column. Make sure you enter values for
your column Identifiers
// or this code will throw an exception (which i've caught
here)...
try{
if(tc.identifier().toString().equals("Column 1"))
return column1Array.objectAtIndex(i);
else if(tc.identifier().toString().equals("Column 2"))
return column2Array.objectAtIndex(i);
else
return "Bad identifier (could be mislabeled in IB)";
}
catch(Exception e)
{
return "Exception (could be unlabeled identifier in IB)";
}
}
// You can add other table datasource methods if you are going to
allow for data to be
// changed and whatnot. Check the documentation for more on that.
//
http://developer.apple.com/techpubs/macosx/Cocoa/Reference/ApplicationKit/
Java/Protocols/NSTableDataSource.html
// This is a sample convenience method I added just so you can see
it work.
// This is MY OWN method and not required for use as the data source.
public void addRow(String field1, String field2)
{
column1Array.insertObjectAtIndex(field1,column1Array.count());
column2Array.insertObjectAtIndex(field2,column2Array.count());
}
}
=============================
and fourth - I fixed up MyController to set the datasource of my table
accordingly. Here is the code:
==============================
/* MyController */
import com.apple.cocoa.foundation.*;
import com.apple.cocoa.application.*;
public class MyController {
NSTableView myTableView;
myTableData tableData = new myTableData();
public void awakeFromNib()
{
myTableView.setDataSource(tableData);
// this is just for adding some sample data
for(int i=1; i<6; i++)
{
tableData.addRow("column 1 row "+i,"column 2 row "+i);
}
}
}
==================
Notice I tried to document it a little - a task I am not used to :-).
Let me know if this helps you guys. And Cocoa gurus - let me know if
there is something VERY wrong with this example. I do still consider
myself a newbie. Again - this is very basic - but it should get you
started. Please let me know if this works...
Enjoy
Tyler
On Thursday, September 20, 2001, at 09:51 AM, Johan Lindberg wrote:
-- I just can't seem to get the NSTableView to function properly. In
fact, I haven't managed to get it to display any data at all.. There
seems to be very little documentation about it, and if anyone have some
code examples, I'd be very grateful.
/Johan.
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev