Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Refreshing JTable  RSS feed

 
Debbie Argulkar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a JTable that is not editable. It gets its data from an xml file. I want to make sure that each time the file is changed, the changes are reflected in the table view. How do I update the table? repaint() does not work.
Thanks
-Deb
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Debbie,
You only need to update the "TableModel" associated with the "JTable".
The "javax.swing.table.TableModel" is an interface. If you are not specifically associating a "TableModel" with your "JTable", then your "TableModel" will be implemented by a class that extends "javax.swing.table.AbstractTableModel" (which is usually "javax.swing.table.DefaultTableModel").
If this is the case, then, whenever you modify the data in the "AbstractTableModel", it will notify the "JTable" that the data has been modified, and the "JTable" should refresh itself.
I suggest you refer to the javadocs for "JTable", "AbstractTableModel", "DefaultTableModel" and "TableModel" for more details.
The "tables" chapter from the book "Swing" (by Matthew Robinson and Pavel Vorobiev) is available for download from here:
http://developer.java.sun.com/developer/Books/swing2/
And there is also the "How to Use Tables" lesson in the "Java Tutorial":
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
Hope this helps you.
Good Luck,
Avi.
 
Debbie Argulkar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Avi,
Thanks a lot for your prompt and helpful reply. I am going through those chapters now and will hopefully solve the problem.
Thanks
-Deb
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I remember correctly, you need to call firTableModelChane() method in order for JTable to repaint.
 
Debbie Argulkar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I just modified the data in the DefaultTableModel and it worked!!!
Thanks,
-Deb
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!