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

查看:137
本文介绍了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天全站免登陆