Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

refreshing a JTable  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
author
Bartender
Posts: 3280
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 21453
96
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is your table model? Are you clearing it for each search?
 
Author
Ranch Hand
Posts: 964
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.)
 
Dia Shukla
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21453
96
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, setRowCount(0) fires only one removal event instead of one for each row.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!