4

I have 2 simple database query JTable (Client and Server).

Client has all functions like view records, print, save PDF etc. Server auto refresh database records to table with a timer of 30secs. (Not yet implemented.)

My problem is I can display the database records to table with no issue with the following code.

PreparedStatement pst = conn.prepareStatement("SQL");
ResultSet rs = pst.ExecuteQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(rs));

But I wish to implement the auto refreshing of table with the above code with a timer.

Example, I inserted the codes into a methods called public void Update_Records(). How am I supposed to use the timer to call the method to display the records into the table every 30 secs?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user1815586
  • 93
  • 1
  • 5
  • 12

1 Answers1

10

You could do:

Timer timer = new Timer(0, new ActionListener() {

   @Override
   public void actionPerformed(ActionEvent e) {
      updateRecords();
   }
});

timer.setDelay(30000); // delay for 30 seconds
timer.start();

Aside: Java naming conventions have methods starting with lowercase and underscores are generally not used so Update_Records becomes updateRecords.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • 2
    make sure it's `javax.swing.Timer`, not `java.util.Timer`. – mre Dec 27 '12 at 04:10
  • Thanks for the help, I tried the codes but somehow it kept adding full data into my jTable. Is there any way it will refresh new data instead of adding whole data into the jTable? – user1815586 Dec 30 '12 at 08:48