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
--
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
--