This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
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
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 itemsVector, 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().
Joined: Apr 10, 2009
Thanks a ton martin....
Joined: Apr 10, 2009
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