-2

Hello every one I m lost with this problem :

I have a  Jtable and I fill data from mysql database ,
but I want the table to be refresh by a timer every 
xx seconds. Really i don't  know how to do ?

I would appreciate some direction/help.

Gianmarco
  • 2,536
  • 25
  • 57
af555423
  • 35
  • 1
  • 7
  • 2
    Show your best attempt? Have you tried to google? – StanislavL Sep 16 '13 at 09:29
  • i fill data from mysql data base but i want and idea to reload data and refresh jtable every delay – af555423 Sep 16 '13 at 09:45
  • Take a look at javax.swing.Timer – MadProgrammer Sep 16 '13 at 09:47
  • i use timer but i have multiple Jframe for each pass do you understand – af555423 Sep 16 '13 at 10:21
  • @MadProgrammer I do not think you need a swing Timer in this case. I would opt for a `java.util.Timer` in combination with a `SwingWorker` which can perform the query and create a new `TableModel` on a background thread, and replace the `JTable`s model on the EDT. Not sure what the added value of the swing Timer would be in this case as you do not want to trigger the db query on the EDT – Robin Sep 16 '13 at 13:19
  • @robin I would suspect that a SwingWorker and sleep would be enough... – MadProgrammer Sep 16 '13 at 19:59

2 Answers2

1

Use a javax.swing.Timer, with setRepeats(true) so that it repeatedly fires an ActionEvent. In your ActionListener, run the query against the database. The easiest approach is to throw away your old table model, generate a new one from the new database results, and call setTableModel on your JTable. (A more sophisticated approach is to work out the differences to the previous query results and only update those table cells that have changed, but you may not need this refinement.)

So far, I talked about an ActionListener doing the work of regenerating the table model, but a more elegant solution might be to have a custom table model that knows how to update its own data from the database - and will do so, for example, on invocation of (say) an updateFromDatabase() method. So the TableModel has the responsibility of updating itself and the Timer/ActionListener has the responsibility of deciding when these updates should happen.

As already mentioned by Kalathoki, changes to the model layer are not seen in the JTable view component unless the model layer notifies the JTable that the model has changed. If you're calling setTableModel on JTable, then the JTable knows that the model has changed, but if you have a custom table model that (from the point of view of the JTable) 'secretly' refreshes itself from the database, then you need to make sure that the JTable is notified by calling the fireTableDataChanged() method.

Another consideration is how long the database refresh takes. You would not want your UI to freeze up for long periods while the JTable refreshes, so a further refinement would be to use a SwingWorker so that the database query is run in a background thread. In this case the Swing timer would be used to initiate the SwingWorker, and the SwingWorker would use its done() method to update the table model and fire an event to the JTable view component.

Simon White
  • 166
  • 1
  • 3
0

Use fireTableDataChanged() Method. Click Here fore more details

Community
  • 1
  • 1
Yubaraj
  • 3,800
  • 7
  • 39
  • 57