aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes updating 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 "updating JTable" Watch "updating JTable" New topic
Author

updating JTable

Preeti Yarla
Ranch Hand

Joined: Sep 08, 2007
Posts: 69
I created a JTable with custom table model. My table model extends AbstractTableModel and its data is in a ResultSet obtained after executing a query on database.

I am able to populate this JTable with records from the database. The next step I wanted is to insert records into JTable and so I provided the required GUI components.But the record I added does not get updated in the JTable until I restart my application.

What do I need to do in order to refresh the JTable immediately without having to restart the application?

Do I need to implement any listener? I used fireDataChanged() on tablemodel but its not working.


SCJP
Ted Smyth
Ranch Hand

Joined: May 28, 2008
Posts: 73
Originally posted by Preeti Yarla:
I created a JTable with custom table model. My table model extends AbstractTableModel and its data is in a ResultSet obtained after executing a query on database.

I am able to populate this JTable with records from the database. The next step I wanted is to insert records into JTable and so I provided the required GUI components.But the record I added does not get updated in the JTable until I restart my application.

What do I need to do in order to refresh the JTable immediately without having to restart the application?

Do I need to implement any listener? I used fireDataChanged() on tablemodel but its not working.


Sounds like you are not refreshing your model properly. Execute the query again and provide the model a new ResultSet, then call fireTableDataChanged().


Edward Smith
Preeti Yarla
Ranch Hand

Joined: Sep 08, 2007
Posts: 69
Execute the query again and provide the model a new ResultSet


I did that part and then fireTableDataChanged() and a revalidate() and a repaint() on the table. And I also verified that the resultset in my table model is actually changing. But the table on the GUI itself is not refreshed. What else may be going wrong?
[ June 04, 2008: Message edited by: Preeti Yarla ]
Preeti Yarla
Ranch Hand

Joined: Sep 08, 2007
Posts: 69
Thanks ! its actually working now. I have been using different objects of my table model at different points. like

table.setModel(new MyTableModel());

new MyTableModel().insertRecord();

The tablemodel object that table is using is not getting updated after an insert. No wonder the table is not refreshed. With everything working now all that looks like silly mistakes.
Ted Smyth
Ranch Hand

Joined: May 28, 2008
Posts: 73
Glad to hear it
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: updating JTable