JTable:检测单元格数据变化
在 Netbeans 中,我使用 GUI 构建器将 JTable 插入到我的应用程序中。
到目前为止,我只有一个类 (CustomerDB),它是:
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
}
每当用户更改表中的数据时,我想要获取该单元格的旧值(可选)和新值。
为了获取此数据,我尝试实现一个事件侦听器:
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);
到目前为止,这有效。但它还无法让我检测到该单元格的旧值和新值。我还需要做什么?
提前非常感谢您!
In Netbeans, I used the GUI Builder to insert a JTable into my application.
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
}
Whenever a user changes data in the table, I want to get the old value (optional) and the new value of that cell.
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.");
}
};
Then I assign this CellEditorListener to the table (its cell editor):
customerTable.getDefaultEditor(String.class).addCellEditorListener(ChangeNotification);
This works so far. 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?
Thank you very much in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 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".
或带有 ListSelectionListener 的 TableModelLister,如果 TableCell 发生更改,则首先返回,第二个从选定的调用中返回,然后只需比较 Row 和 Row 即可。来自 TableModelLister 和 ListSelectionListener 的列索引
or TableModelLister with ListSelectionListener, fist returns if TableCell changed, second from selected call, then just compare Row & Column Index from TableModelLister with ListSelectionListener
姆科贝尔正在做一些事情。如果您创建自己的单元格编辑器来扩展 DefaultCellEditor 会怎样:
mKorbel is on to something. What if you create your own cell editor that extends DefaultCellEditor: