highfish87
highfish87

Reputation: 21

fireTableDataChanged has no effect on JTable

I have a problem with updating my JTable in Java Swing.

The datas I want to show changes a few times per second and I look for a efficient way to update the data in the JTable.

I used the method setModel() to update the data, and it works, BUT it has 2 drawbacks:

  1. If the user resize the table columns in the header, then he wil get about 10 exceptions (I think because the model is no longer available because it changes a few times per second)

  2. The information of the length of the resized column (in Pixel) get lost, every time the data (and so also the TableModel) changed.

For the TableModel i use my own model ResultSetTableModel which extends AbstractTableModel. This ResultSetTableModel has a method setResultSet(ResultSet rs) and overwrites the method getValueAt(x,y)...

As I told if I set a new ResultSet to my ResultSetTableModel and then add it to the JTable by the method setModel(resultSetTableModel) it works, but it has the 2 drawbacks i told.

So I think I can solve this problem with the method fireTableDataChanged() but I tried many possibilities but get no change.

Do you know, where I have to place the fireevent?

At the moment I try this, but it doesn't work and I don't know why:

private ResultSetTableModel resultSetTableModel;
private DataFetcher dataFetcher;
private JTable table;

...

//works fine
public void initaialUpdateTable() {
    resultSetTableModel = new CachingResultSetTableModel(dataFetcher.getRS());
    table.setModel(resultSetTableModel);
}

//does not work
public void updateTable(){
    resultSetTableModel.setResultSet(dataFetcher.getRS());
    resultSetTableModel.fireTableDataChanged();
}

If I every times call initaialUpdateTable(), it works fine, but i want that just the data changes and not the whole model

Thanks for your answers

Michael

Upvotes: 2

Views: 11226

Answers (2)

Nate W.
Nate W.

Reputation: 9259

Sorry I don't have a concrete answer to your question, but I couldn't quite fit all that I want to say in a comment.

I used the method setModel() to update the data

You should probably stick to a single model that provides methods to modify its data. These methods should appropriately notify listeners when something has changed.

Here's a really awesome article that shows how to implement a high-performance, multi-threaded table with frequently changing data. You could probably use a lot of the example source code.

Upvotes: 0

mKorbel
mKorbel

Reputation: 109823

but i want that just the data changes and not the whole model

Hmm how can I..., there is no only one ...

1) Something that you can see in the GUI is TableView, only presentation layer, and all data are always stored in the TableModel

2) If you don't declare any TableModel, this doesn't mean that there isn't exist, still are there DefaultTableModel

3) Your private ResultSetTableModel resultSetTableModel; must extend AbstractTableModel,

4) If you'll to block any of fireXxxXxxChanged();, then no changes goes back to the TableView,

5) Basic stuff here, start with fireTableCellUpdated(row, col);

EDIT

More informations about TableModels here, here or search for ResultSetTableModel, TableFromDatabase

Upvotes: 1

Related Questions