This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JTable repaint issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable repaint issue" Watch "JTable repaint issue" New topic

JTable repaint issue

Shreyas Rane

Joined: Apr 10, 2009
Posts: 16

The above code displays a Vector of 20 employees in JTable , used a SimpleTableModel which extends from AbstractTableModel....
i am tryin to do 2 things with JTable : 1st is Paging which works smoothly....

Issue with the above code is :
When i search employee on the basis of firstName the the result is not displayed in table smoothly... it takes some time to refresh on 1st time on subsequent search result is not displayed

I have used Hibernate classes to get all records, which i store in a vector the all search is done on employee records in this Vector

Any help would be greatly appreciated..
Martin Vajsar

Joined: Aug 22, 2010
Posts: 3733

When changing data in a JTable, you definitely should not create a new instance of JTable. I've reviewed your code only casually, but you seem to load all rows into memory. Several valid approaches exist in this case:

1) You can create a new table model from scratch (as you do now), but instead of creating a new JTable for it, set it to the existing JTable using the setModel method. It is quite straightforward to do this in your code.

2) Update the table model in your code and advertise the change to the table using proper fireTable... methods (note: you must call these methods on the Event Dispatch Thread).

3) Add a row filter to the table. This filter will simply hide the rows that do not match your criteria, you'll just have to put all rows into the table model. Filter could be used to perform the pagination too, only it does not look like much elegant solution. If you decided not to have all data in memory at once, this would have to be reworked.

All of these approaches are well covered in tutorials. These tutorials helped me immensely when I was starting with Swing, especially JTable.

Note: the way you "refresh" the table in the refreshTable method differs slightly from the search method, if you look closely. I assume this is why it behaves differently. However, changing the whole table is incorrect and must be avoided.

Another note: some of your SwingWorkers seem to be designed incorrectly. The first SwingWorker modifies the items Vector, but this instance (items) is passed to the JComboBox. This is not thread-safe - you're modifying data you've passed to a Swing component from a background thread. Correct way would be to construct a new instance of Vector in the background thread and (in the done() method) wrap it in the DefaultComboBoxModel which would then be set to the JComboBox using setModel().
Shreyas Rane

Joined: Apr 10, 2009
Posts: 16
Thanks a ton martin....
Shreyas Rane

Joined: Apr 10, 2009
Posts: 16

I have made a few changes to my previous code Pagination was smooth, Searching goes in smooth too.
Still got a issue here , i display 20 records in table while searching if <=20 records are returned for particular employee firstname then it works ok otherwise JVM goes

Any help would be greatly appreciated..
I agree. Here's the link:
subject: JTable repaint issue
jQuery in Action, 3rd edition