How to refresh a JTable after database operations without frame reload?

戏子无情 提交于 2019-12-23 05:11:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!