0

I have JTable and it has a JCheckBox and a JComoboBox in two different columns. When I select a JCheckBox that corresponds to that row, the JComboBox should be disable. Kindly help me.

user1329572
  • 6,176
  • 5
  • 29
  • 39

1 Answers1

4

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.

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:

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);
    }

}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117