问题
I want to refresh a JTable
after performing db operations like insert/update/delete without frame reload. Is there a programmatic solution to do so?
回答1:
The answer depends on what type of TableModel
you're using...and at what level the events are occuring.
For example, if you have some kind of background Thread
or "update" button and want to reload the content from the database, simple create a new TableModel
based on the results from the database and apply it to the current JTable
, this will update the view automatically.
If you are performing database operations within your application (adding, updating, removing rows), you can provide some kind of notification back to the TableModel
and simply update it's content.
For example DefaultTableModel provides addRow
, insertRow
, removeRow
methods. TableModel
also provides setValueAt
which allows you to change the value of a given cell.
All these methods provide notification back to the JTable
that the model is associated with automatically
回答2:
You should use jtable.getViewport().removeAll()
and then refill it again with all data collected from database.
来源:https://stackoverflow.com/questions/23488314/how-to-refresh-a-jtable-after-database-operations-without-frame-reload