数据库操作后如何在不重新加载框架的情况下刷新JTable? [英] How to refresh a JTable after database operations without frame reload?

查看:119
本文介绍了数据库操作后如何在不重新加载框架的情况下刷新JTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在执行数据库操作(如不重新加载帧的插入/更新/删除)后刷新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 提供addRowinsertRowremoveRow方法. 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆