• Post Reply Bookmark Topic Watch Topic
  • New Topic

Refreshing JTable from database  RSS feed

 
Tim Nachreiner
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to add a Refresh button to my frame to refresh the data from the database, but I'm not sure how to refresh the data in the jtable. The constructor below initially creates the table and displays the data. Should my button destroy/create the JPanel all over, or is there a single method within the TableModel that will redisplay the data?

 
K. Tsang
Bartender
Posts: 3632
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Nachreiner wrote:I want to add a Refresh button to my frame to refresh the data from the database, but I'm not sure how to refresh the data in the jtable. The constructor below initially creates the table and displays the data. Should my button destroy/create the JPanel all over, or is there a single method within the TableModel that will redisplay the data?


I would suggest your "Refresh" button implement the action listener and use your table model to re-fetch the records.

Say my records are whole bunch of rooms. My table model has a setRooms() method.



In my code, the service.getLatestView() method does is fetch data from database
 
Tim Nachreiner
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Say my records are whole bunch of rooms. My table model has a setRooms() method.

In my code, the service.getLatestView() method does is fetch data from database


I agree this will work, but I thought it was bad design to have a reference to the database anywhere within your TableModel implementation. This reference makes the TableModel less reusable. Am I wrong on this point?
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should my button destroy/create the JPanel all over,


No, you don't want to recreate components, only change the data being displayed by the component

or is there a single method within the TableModel that will redisplay the data?


The is no method in the TableModel to do this. The easiest approach is to recreate the TableModel and then add the model to the table using:



By the way you should never use code like the following that was suggested in an earlier answer:



The fireXXX methods of the AbstractTableModel should only ever be invoked by the TableModel itself.
 
K. Tsang
Bartender
Posts: 3632
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes and no, depending on how you implement the table model.

For the example I showed, my table model extends AbstractTableModel and the constructor takes in 1 parm (the map of rooms). Since my records are "rooms", besides the setRooms() method, I also had getRoom(), addRoom(), removeRoom() (all singular).

The Room object here can be any other object. So if you really want to you can make a "generic" interface that set/get/add/remove (the CRUD functions) and have the table model implements that.



The problem with this GenericCrud interface is the set() method. What type/collection are you going to use to populate your JTable or display? Having all these set() methods for reuse may mean implement all of them? Does a default implementation class needed?

All these design issues raise more questions than answers. So KISS and direct may be the better approach. Different objects should have its own table model if I'm doing it.

Hope this helps.
 
Tim Nachreiner
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you don't want to recreate components, only change the data being displayed by the component


I refactored the constructor into granular method calls. Now I can call populateTable from virtually anywhere to refresh the table data.

 
K. Tsang
Bartender
Posts: 3632
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your connection variable can become local variable in populateTable() method by moving lines 8 and 9 to that method.

Also remember to close the connection with finally clause.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!