简体   繁体   English

单击JCheckBox时禁用JTable中的JComboBox

[英]Disable JComboBox inside JTable on click of JCheckBox

I have JTable and it has a JCheckBox and a JComoboBox in two different columns. 我有JTable ,它在两个不同的列中都有一个JCheckBoxJComoboBox When I select a JCheckBox that corresponds to that row, the JComboBox should be disable. 当我选择与该行相对应的JCheckBox ,应禁用JComboBox Kindly help me. 请帮助我。

Simply disable editing of the cell based on your model. 只需禁用基于模型的单元格编辑即可。 In your TableModel, override/implement the isCellEditable() method to return the "value" of the checkbox. 在TableModel中,重写/实现isCellEditable()方法以返回复选框的“值”。

Although the following example is not based on JComboBox, it illustrates how to disable edition of a cell based on the value of a checkbox at the beginning of the row: 尽管以下示例不是基于JComboBox,但它说明了如何基于行开头的复选框的值来禁用单元格的版本:

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

public class TestTable {

    public JFrame f;
    private JTable table;

    public class TestTableModel extends DefaultTableModel {

        public TestTableModel() {
            super(new String[] { "Editable", "DATA" }, 3);
            for (int i = 0; i < 3; i++) {
                setValueAt(Boolean.TRUE, i, 0);
                setValueAt(Double.valueOf(i), i, 1);
            }
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            if (column == 1) {
                return (Boolean) getValueAt(row, 0);
            }
            return super.isCellEditable(row, column);
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            if (columnIndex == 0) {
                return Boolean.class;
            } else if (columnIndex == 1) {
                return Double.class;
            }
            return super.getColumnClass(columnIndex);
        }
    }

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

            @Override
            public void run() {
                new TestTable().initUI();
            }
        });
    }

    protected void initUI() {
        table = new JTable(new TestTableModel());
        f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(300, 300);
        f.setLocationRelativeTo(null);
        f.add(new JScrollPane(table));
        f.setVisible(true);
    }

}

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

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