help with JTable and Table Models

R

Richard

Hi all

I am having a bit of trouble with my TableModel class that extends
AbstractTableModel. My code so far is


------------------------------------------------------------------------
------------------------------------------------------------------------

public class ResultsTableModel extends AbstractTableModel {

private Vector columnHeaders;
private Vector data;

public ResultsTableModel(Vector data, Vector columnHeaders) {
this.data = data;
this.columnHeaders = columnHeaders;
}

public int getRowCount() {
return data.size();
}

public int getColumnCount() {
return columnHeaders.size();
}

public Object getValueAt(int rowIndex, int columnIndex) {
return ((Vector) data.get(rowIndex)).get(columnIndex);
}

public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}


public static class Test extends TestCase {
public void testTableModel(){
Vector rows = new Vector();
Vector row = new Vector();
row.add("1a");
row.add("1b");
row.add("1c");
rows.add(row);

Vector row2 = new Vector();
row2.add("2a");
row2.add("2b");
row2.add("2c");
rows.add(row2);

Vector columns = new Vector();
columns.add("a");
columns.add("b");
columns.add("c");

ResultsTableModel model = new ResultsTableModel(rows, columns);
assertEquals(2, model.getRowCount());
assertEquals(3, model.getColumnCount());
assertEquals("1a", model.getValueAt(0,0));
assertEquals("2a", model.getValueAt(1,0));
}
} //of test
}

------------------------------------------------------------------------
------------------------------------------------------------------------

When I construct this table model and pass this into the JTable
constructor I lose the table column headers values. All the headers are
just a,b,c... (the default I presume). If I just pass the vectors into
the JTable constructor and bypass the TableModel then I get the table
headers correct. ie not a,b,c but header1, header2...

I have looked at the AbstractTableModel class to try and figure out
what bit of wiring up I am missing but cant figure it out. Has anyone
got any ideas? Im sure it must be simple

Thanks

Richard
--
 
V

VisionSet

Richard said:
All the headers are
just a,b,c... (the default I presume). If I just pass the vectors into
the JTable constructor and bypass the TableModel then I get the table
headers correct. ie not a,b,c but header1, header2...

You need to implement

getColumnName(int column)
 
R

Richard

VisionSet said:
You need to implement

getColumnName(int column)



Thanks for your reply. Sorry it took me so long.

I did 'setAutoCreateColumnsFromModel(false);' and this worked


--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top