aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Problem updating a JTable in a tab before the tab is opened Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem updating a JTable in a tab before the tab is opened" Watch "Problem updating a JTable in a tab before the tab is opened" New topic
Author

Problem updating a JTable in a tab before the tab is opened

Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 316
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,
Alejandro Barrero
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

I think the table model's fire methods are what you need. Use fireTableDataChanged() to have all cells re-evaluated.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

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.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Rob Spoor wrote:I think the table model's fire methods are what you need. Use fireTableDataChanged() to have all cells re-evaluated.

DefaultTableModel does this automatically - assuming it is used correctly. If there is a need to call fireTable... methods when using DefaultTableModel, the AbstractTableModel should be probably used instead.
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 316
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


processorTable is a JPanel that contains the JTable with

smartTable is of the class that extends JTable with

beans is a list of beans and the model has
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

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.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
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.
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 316
Thanks to all; fireTableDataChanged() does it.
 
Consider Paul's rocket mass heater.
 
subject: Problem updating a JTable in a tab before the tab is opened