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.
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.
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.
Use fireTableDataChanged()
Method. Click Here fore more details