数据库操作后如何在不重新加载框架的情况下刷新JTable? [英] How to refresh a JTable after database operations without frame reload?
问题描述
我想在执行数据库操作(如不重新加载帧的插入/更新/删除)后刷新JTable
.有编程的解决方案可以做到这一点吗?
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?
推荐答案
答案取决于您正在使用的TableModel
类型...以及事件发生在什么级别.
The answer depends on what type of TableModel
you're using...and at what level the events are occuring.
例如,如果您有某种背景Thread
或更新"按钮,并且想从数据库中重新加载内容,则可以根据数据库的结果简单地创建一个新的TableModel
,并将其应用于当前JTable
,这将自动更新视图.
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.
如果您正在应用程序中执行数据库操作(添加,更新,删除行),则可以向TableModel
提供某种通知,并只需更新其内容即可.
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.
例如 DefaultTableModel
提供addRow
,insertRow
,removeRow
方法. TableModel
还提供了setValueAt
,它允许您更改给定单元格的值.
For example DefaultTableModel
provides addRow
, insertRow
, removeRow
methods. TableModel
also provides setValueAt
which allows you to change the value of a given cell.
所有这些方法都会向JTable
提供与模型自动关联的通知
All these methods provide notification back to the JTable
that the model is associated with automatically
这篇关于数据库操作后如何在不重新加载框架的情况下刷新JTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!