0

我有JTable,它在两个不同的列中有 aJCheckBox和 a 。JComoboBox当我选择JCheckBox与该行对应的一个时,JComboBox应该禁用。请帮助我。

4

1 回答 1

4

只需根据您的模型禁用对单元格的编辑。在您的 TableModel 中,覆盖/实现isCellEditable()返回复选框“值”的方法。

尽管以下示例不是基于 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);
    }

}
于 2012-05-23T17:02:44.307 回答