JTable: Detect cell data change(JTable:检测单元格数据变化)
问题描述
在 Netbeans 中,我使用 GUI Builder 将 JTable 插入到我的应用程序中.
到目前为止,我只有一个类 (CustomerDB):
I have just one class (CustomerDB) so far which is:
package CustomerDB;
import [...];
public class CustomerDB extends javax.swing.JFrame {
CellEditorListener ChangeNotification = new CellEditorListener() {
public void editingCanceled(ChangeEvent e) {
System.out.println("The user canceled editing.");
}
public void editingStopped(ChangeEvent e) {
System.out.println("The user stopped editing successfully.");
}
};
public CustomerDB() {
customerTable = new javax.swing.JTable();
customerTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"ID", "Name", "Address", "Phone"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
customerTable.getDefaultEditor(String.class).addCellEditorListener(ChangeNotification);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CustomerDB().setVisible(true);
}
});
}
// Variables declaration - do not modify
[...]
private javax.swing.JTable customerTable;
[...]
// End of variables declaration
}
每当用户更改表格中的数据时,我都想获取该单元格的旧值(可选)和新值.
为了获取这些数据,我尝试实现一个事件监听器:
In order to get this data, I tried to implement an event listener:
CellEditorListener ChangeNotification = new CellEditorListener() {
public void editingCanceled(ChangeEvent e) {
System.out.println("The user canceled editing.");
}
public void editingStopped(ChangeEvent e) {
System.out.println("The user stopped editing successfully.");
}
};
然后我将这个 CellEditorListener 分配给表格(它的单元格编辑器):
customerTable.getDefaultEditor(String.class).addCellEditorListener(ChangeNotification);
到目前为止有效.但它还不能让我检测到这个单元格的旧值和新值.我还需要做什么?
非常感谢您!
推荐答案
但它还不能让我检测到这个单元格的旧值和新值.我还需要做什么?
But it doesn't yet enable me to detect the old and the new value of this cell. What else do I have to do?
使用 TableModelListener 来监听变化更容易,但它仍然存在无法访问旧值的问题.
It is easier to use a TableModelListener to listen for changes but it still has the problem of not being able to access the old value.
查看表格单元侦听器,了解可让您访问的解决方案旧值"和新值".
Check out the Table Cell Listener for a solution that gives you access to the "old value" as well as the "new value".
这篇关于JTable:检测单元格数据变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JTable:检测单元格数据变化
基础教程推荐
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01