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

refreshing a JTable

Dia Shukla
Greenhorn

Joined: Dec 08, 2008
Posts: 2
I am using a JTable to display the values from a database. The rows are added dynamically to the JTable depending upon the rows in the ResultSet. The values retrieved are based upon the selections made in two combo boxes.
Initially, when I make a selection and display the JTable, the rows are dynamically added and shown. But, when I again change the values in the combo boxes and display the JTable again, the newly retrieved row values are shown but at the end of the previous selections rows i.e. the earlier values are not getting discarded and the new ones are being appended to the end of the earlier rows. I want to know how to refresh a JTable�s contents. I found out that there is a function called �fireTableDataChanged ()� in the DefaultTableModel Class (which is what I�m using) which according to the API, �Notifies all listeners that all cell values in the table's rows may have changed. The number of rows may also have changed and the JTable should redraw the table from scratch. The structure of the table (as in the order of the columns) is assumed to be the same.� But I don�t have any listeners for the JTable I am using. I only have a button listener on whose click I�m calling a function to retrieve rows and display them in the JTable. Please help.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Hi dia and welcome to Javaranch,

Firstly, can you please change your name according to our naming policy.

Your question appears to be about Swing, so I'm moving this to the appropriate forum for you.


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

What is your table model? Are you clearing it for each search?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by dia:
I am using a JTable to display the values from a database. The rows are added dynamically to the JTable depending upon the rows in the ResultSet.


If we want to be a bit more precise, the rows are added dynamically to the TableModel, not directly to the table.

The values retrieved are based upon the selections made in two combo boxes.
Initially, when I make a selection and display the JTable, the rows are dynamically added and shown. But, when I again change the values in the combo boxes and display the JTable again, the newly retrieved row values are shown but at the end of the previous selections rows i.e. the earlier values are not getting discarded and the new ones are being appended to the end of the earlier rows.


It sounds like you haven't removed the old rows from the TableModel before adding the new rows. The easiest way to do this with DefaultTableModel is to call setRowCount(0).

I want to know how to refresh a JTable�s contents. I found out that there is a function called �fireTableDataChanged ()� in the DefaultTableModel Class (which is what I�m using) which according to the API, �Notifies all listeners that all cell values in the table's rows may have changed. The number of rows may also have changed and the JTable should redraw the table from scratch. The structure of the table (as in the order of the columns) is assumed to be the same.� But I don�t have any listeners for the JTable I am using.


The JTable itself is listening to the TableModel.

What the fireTableDataChanged() method does is notify the JTable(s) that they should be redrawn to reflect changes in the Model, it doesn't actually change the model. DefaultTableModel has other methods that change the model, and these automatically call the fire___() methods for you, so you shouldn't have to call them yourself. (When I use JTables I usually subclass AbstractTableModel, and I am careful to call the fire methods appropriately. But this is not your situation.)


bitguru blog
Dia Shukla
Greenhorn

Joined: Dec 08, 2008
Posts: 2
Well thank you all for your help. And special thanks to Brian Cole for his help. Thanks for correcting me and helping me out. I used setRowCount(0) and it works as needed.
I had kept trying on my own too,to solve the problem,and I did it too. Here is what I did. I used the getRowCount() method of the DefaultTableModel and removed each row. This is what I used where dtm is the DefaultTableModel object name.

while(dtm.getRowCount()>0)
{
dtm.removeRow(0);
}
But I guess here, one statement of code is better than two. Anyways, thanks a lot for all your help.
And I also changed my name according to the naming policy. Thanks,Martijn Verburg.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Also, setRowCount(0) fires only one removal event instead of one for each row.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: refreshing a JTable