Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing - Refresh jTable using Timer

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?

like image 474
user1815586 Avatar asked Dec 26 '22 12:12

user1815586


1 Answers

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.

like image 140
Reimeus Avatar answered Jan 02 '23 21:01

Reimeus