views:

339

answers:

2

I have a JTable with a custom TableModel called DataTableModel. I initialized the table with a set of column names and no data as follows:

books = new JTable(new DataTableModel(new Vector<Vector<String>>(), title2));
JScrollPane scroll1 = new JScrollPane(books);
scroll1.setEnabled(true);
scroll1.setVisible(true);
JSplitPane jsp1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, scroll1, scroll2);
JSplitPane jsp2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, inventory, jsp1);
myPanel.add(jsp2, BorderLayout.CENTER);

I later want to update books with a set of data, and use the following:

DataTableModel d = (DataTableModel)books.getModel();
d.setValues(bookList);
books.setModel(d);

where bookList is a Vector<Vector<String>> that definitely has data. However, although all this code is being executed, it is not displaying on the screen. The code for the setValues() method is:

public void setValues(Vector<Vector<String>> v) {
 values = v;
 fireTableDataChanged();
}

Am I missing something here?

The class and methods for my DataTableModel are (these methods are all implemented to return correct results):

public class DataTableModel extends AbstractTableModel {
    public DataTableModel(Vector<Vector<String>> v, Vector<String> c) {}

    public int getColumnCount() {
        if (values != null && values.size() > 0)
            return values.elementAt(0).size();
        else
            return 0;
    }

    public int getRowCount() {
        if (values != null && values.size() > 0)
            return values.size();
        else
            return 0;
    }

    public Object getValueAt(int arg0, int arg1) {}
    public void setValues(Vector<Vector<String>> v) {}
    public Vector<Vector<String>> getValues() {}
    public void setColumnNames(Vector<String> columns) {}
    public String getColumnName(int col) {}
}
+1  A: 

Have you implemented the other methods for TableModel? If so, how does your implementation look? Maybe you should post your table model code to let us inspect it?

BTW: My main error when implementing TableModel was to override getRowCount() and getColumnCount() to return 0. This will tell the table that there is no data to display...

EDIT: So you seem to be using something like an AbstractTableModel or a DefaultTableModel, right? Have you overridden some of the methods?

EDIT2: You should call fireTableStructureChanged instead of fireTabeDataChanged(), because initially your table model is returning 0 for getColumnCount().

EDIT3: To further optimize your model you should consider returning a fixed value for getColumnCount() if you have data that has the same number of columns every time. Then you can call the fireTabeDataChanged() which just loads the new data instead of completely building up the table and data (fireTableStructureChanged()) every time.

dhiller
ahhh.... crap. I'll post in a little while if that worked.
Elie
That fixed it! Thanks!
Elie
A: 

This is weird problem. You said that DataTableModel implements TableModel. So. If you does not use abstract class the problem should be in the way how you are handling the events. Are listeners really registered and then notified? If you can, please send link to source of DataTableModel. But before, verify that you are correctly handling listeners registered into this model.

Rastislav Komara
I've debugged the code. The update code is really being called, as is the updateValues() method. So the fireTableDataChanged() method is also being called, with no errors, but the display is not changing.
Elie