监听JTable单元值更改并相应更新数据库的最佳方法是什么? [英] What is the best way to listen for changes in JTable cell values and update database accordingly?
问题描述
我正在使用多个 JTables
构建和应用程序,我需要检测何时发生单元格值更改,以便我可以在数据库中更新它。我尝试了 TableModelListener
并覆盖 tableChanged
,但只有当我点击时它才会触发(点击在我编辑了一个单元格之后的另一行)。
I'm building and app with multiple JTables
and I need to detect when cell value change occurs so I can update it in the database. I tried TableModelListener
and overriding tableChanged
, but it fires only when I click away (click on another row) after I have edited a cell.
还有其他方法吗?
推荐答案
你可以实现 CellEditorListener
界面,如示例所示。请注意 JTable
本身是 CellEditorListener
。
You can implement the CellEditorListener
interface, as shown in this example. Note that JTable
itself is a CellEditorListener
.
它焦点丢失时也可以方便地终止编辑:
It may also be convenient to terminate the edit when focus is lost:
table.putClientProperty("terminateEditOnFocusLost", true);
这篇关于监听JTable单元值更改并相应更新数据库的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!