简体   繁体   English

在JTable中监听JCheckBox的变化

[英]Listeneing for changes in JCheckBox in a JTable

How do I listen for changes in a JTable column that has a JCheckBox in it? 如何监听包含JCheckBox的JTable列中的更改? I want to know when the user selects/deselects the check box. 我想知道用户何时选择/取消选中复选框。 The class for the column is set as boolean so it is automatically rendered as a JCheckBox. 该列的类设置为boolean,因此它将自动呈现为JCheckBox。

I think what you want is to listen for data changes in the TableModel by using a TableModelListener . 我想你想要的是通过使用TableModelListener来监听TableModel的数据更改。 It's also possible to use a custom editor, but I think the TableModelListener is the simplest way to go. 也可以使用自定义编辑器,但我认为TableModelListener是最简单的方法。 Your overriden tableChanged method could look something like this 您的overriden tableChanged方法可能看起来像这样

@Override
public void tableChanged(TableModelEvent e) {
    int row = e.getFirstRow();
    int column = e.getColumn();
    if (column == BOOLEAN_COLUMN) {
        TableModel model = (TableModel) e.getSource();
        String columnName = model.getColumnName(column);
        Boolean checked = (Boolean) model.getValueAt(row, column);
        if (checked) {
            System.out.println(columnName + ": " + true);
        } else {
            System.out.println(columnName + ": " + false);
        }
    }
}

在此输入图像描述

Here's a complete running example 这是一个完整的运行示例

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class TestTableModelListener {
    private static final int BOOLEAN_COLUMN = 2;

    public TestTableModelListener() {
        JTable table = createTable();
        table.getModel().addTableModelListener(new CheckBoxModelListener());

        JFrame frame = new JFrame();
        frame.add(new JScrollPane(table));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private JTable createTable() {
        String[] cols = {"COL", "COL", "COL"};
        Object[][] data = {{"Hello", "Hello", false}, {"Hello", "Hello", false}};
        DefaultTableModel model = new DefaultTableModel(data, cols) {
            @Override
            public Class getColumnClass(int column) {
                return column == BOOLEAN_COLUMN ? Boolean.class : String.class;
            }
        };
        JTable table = new JTable(model);
        return table;
    }

    public class CheckBoxModelListener implements TableModelListener {

        public void tableChanged(TableModelEvent e) {
            int row = e.getFirstRow();
            int column = e.getColumn();
            if (column == BOOLEAN_COLUMN) {
                TableModel model = (TableModel) e.getSource();
                String columnName = model.getColumnName(column);
                Boolean checked = (Boolean) model.getValueAt(row, column);
                if (checked) {
                    System.out.println(columnName + ": " + true);
                } else {
                    System.out.println(columnName + ": " + false);
                }
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new TestTableModelListener();
            }
        });
    }
}

Thanks to the comment by mKorbel, I re-wrote the setValueAt method for the table model as such: 感谢mKorbel的评论,我为表模型重写了setValueAt方法:

public void setValueAt(Object value, int row, int col) {
    super.setValueAt(value, row, col);
    if (col == 4) {
        if ((Boolean) this.getValueAt(row, col) == true) {
            //code goes here
        }
        else if ((Boolean) this.getValueAt(row, col) == false) {
            //code goes here
        }
    }   
}

Now the code is only executed when the value in the cell actually changes, which is what I want. 现在代码只在单元格中的值实际发生变化时执行,这就是我想要的。

Now the code is only executed when the value in the cell actually changes, which is what I want. 现在代码只在单元格中的值实际发生变化时执行,这就是我想要的。

Check out the Table Cell Listener which was specifically designed for this situation. 查看专为此情况设计的Table Cell Listener

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM