I change the contents of a JTable when running code from a user's action in the currently opened tab (I am using Java 1.6). To update the contents I change the data in my TableModel class that extends DefaultTableModel so that the methods getRowCount, getColCount and getValueAt return the correct values. However, when I go to the tab that contains the table it is not displaying the updated contents. I have tried using invalidate and repaint to no avail; I don't know what happens when the RepaintManager queues the requests (I think it is used for managing components currently in view). However, if I close and restart the program the updated data (which comes from a database) is displayed correctly.
I want to invoke repaint immediately after the table is displayed on opening the tab; the question is what kind of event can I use to do this?
Your help will be greatly appreciated,
Hard to tell if you don't post the code, but based on your description, I guess the cause is that you're accidentally updating another instance of your TableModel class. After you restart the program, you construct a new instance of the TableModel based on database data, which is of course correct. Check carefully all places where you create a new instance of TableModel and track what happens to them.
Thank you Martin. Regarding the code, it is a generic table to display in columns the properties of beans in a list using reflection. With this class, you only have to set it up with the list, the class of the beans, the names of the columns and a few other things and you never have to deal with the actual table; in fact you can set the table to be editable and you can enter data in the cells. I would be very happy to email you a document that explains the table and a jar file with source code.
It is like this: in a dialog I use
where parent is the JFrame. This method closes the dialog and then the user can click on the tab that contains the table. In the frame I have
Is it the resetObjects methods that eventually updates the model? If it is the case, Rob Spoor has been right and you need to let the table know the model has changed. Add call to the fireTableDataChanged() method as the last statement of resetObjects.
Anyway, you should read the JTable tutorials. These tutorials are very well written and helped me immensely when I was beginning with Swing.
If you're still stuck, post your table model and SmartTable code. It's better to post it here on the forum, if more people see it, chances are you'll get the response sooner and very probably better.
To update the contents I change the data in my TableModel class that extends DefaultTableModel
If you are storing Beans in your custom TableModel, then I doubt you should be extend DefaultTableModel since DefaultTableModel is designed to store "cells of data", not "rows of data".
Intead you need to extend AbstractTableModel, which means you are responsible for implementing the proper storage of the Beans as well as invoking the appropreate fireXXX events when the model changes.
Joined: Aug 01, 2005
Thanks to all; fireTableDataChanged() does it.
subject: Problem updating a JTable in a tab before the tab is opened