JTable:检测单元格数据变化 [英] JTable: Detect cell data change

查看:42
本文介绍了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:检测单元格数据变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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