JTable updates not showing

I have one JTable

with a custom TableModel

called DataTableModel

. I initialized the table with a set of column names and no data:

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

      

Later, I want to update the workbooks with a dataset and use the following:

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

      

where bookList is the one Vector<Vector<String>>

that definitely has data. However, while all this code is being executed, it is not displayed on the screen. Code for the method setValues()

:

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

      

Did I miss something?

Class and methods for my DataTableModel (all of these methods are 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


source to share


3 answers


Have you implemented other methods for TableModel

? If so, what does your implementation look like? Maybe you should post your table model code so we can inspect it?

BTW: My main implementation mistake TableModel

was overriding getRowCount()

and getColumnCount()

in return 0

. This will tell the table that there is no data to display ...

EDIT: So you are using something like AbstractTableModel

or DefaultTableModel

, right? Have you overridden some of the methods?



EDIT 2: Call fireTableStructureChanged

instead fireTabeDataChanged()

, because originally your table model returns 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 each time. Then you can call fireTabeDataChanged()

that just loads new data rather than completely creating the table and data ( fireTableStructureChanged()

) every time.

+1


source


This is a strange problem. You said it DataTableModel

does TableModel

. So. If you are not using an abstract class, the problem must be how you handle events. Are listeners actually registered and then notified? If you can, post a link to the source DataTableModel

. But first, make sure that you are handling the listeners registered with this model correctly.



0


source


this solves the problem; you are not allowed to unbind the main binding group, but you tell jtablebinding to update it like so:

Binding b = bindingGroup.getBindings().get(0);
b.unbind();
b.bind();

      

0


source







All Articles