Elie Elie - 1 year ago 77
Java Question

JTable updates not appearing

I have a

with a custom
. 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);
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();

where bookList is a
that definitely has data. However, although all this code is being executed, it is not displaying on the screen. The code for the
method is:

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

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();
return 0;

public int getRowCount() {
if (values != null && values.size() > 0)
return values.size();
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) {}

Answer Source

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?

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

EDIT 3: 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download