且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更新数据库后如何使JTable显示刷新的数据?

更新时间:2023-12-03 13:19:58

由于模型本身(已发布代码),模型无法识别数据库中的修改.您可以刷新模型中的数据,例如调用现有的init()方法或创建类似的方法.或者,您可以构建一个新模型并在表上重置它.

As it is (posted code) the model is not aware of the modification in the database. You could either refresh the data in the model, such as calling existing init() method or creating a similar one. As an alternative, you can build a new model and reset it on the table.

模型负责触发您在actionPerformed中使用的通知,例如fireTableDataChanged.表客户端不应执行这些方法.例如,fireTableDataChanged属于init()方法.有关更多详细信息和示例,请参见触发数据更改事件.

It is the responsibility of the model to fire notifications such as fireTableDataChanged that you use in actionPerformed. Table clients should not execute these methods. fireTableDataChanged belongs to init() method for example. See Firing Data Change Events for more details and examples.

模型应该与数据有关,因此***将设置textField的逻辑移出模型.如果需要,您可以从模型中公开其他方法,以帮助客户端获取有关数据和状态的其他详细信息.

The model should be about data, so it is best if you move the logic of setting textField out of the model. If needed you can expose additional methods from the model to help clients get additional details about data and state.