Search code examples
javaswingjtablejcheckbox

How to properly set editable checkbox inside a JTable cell in Java Swing application?


I have a JTable, where I have couple of JCheckBoxes. I am initializing JCheckBoxes and other cells values from database. My problem is, when I click on the JCheckBox to checked or unchecked, they don't do nothing.

When I clicked twice, the editable JTable cell become active and shows the value of JCheckBox which is 0 OR 1. I can edit the cell with 0 OR 1 and when I save, it saves the record and load the checkbox with edited value.

Following snippet to show how I am loading and creating JCheckBoxes:

class TextBoxNewCellRenderer extends JPanel implements TableCellRenderer {

:::
public Component getTableCellRendererComponent(

case 10: //Active
                switch (column) {
                    case 0:
                        this.add(lblStar);
                        this.add(new JLabel(value.toString(), JLabel.LEFT));
                        break;
                    case 1:
                        checkBox = new JCheckBox();
                        checkBox.setToolTipText("Set 0 OR 1");
                        checkBox.setEnabled(true);
                        if(value.toString().equals("1"))
                            checkBox.setSelected(true);
                        if(value.toString().equals("0"))
                            checkBox.setSelected(false);
                        this.add(checkBox);
                        break;
:::

 return this;
    }
}

In my renderTable method I Override following isCellEditable:

 DefaultTableModel model = new DefaultTableModel() {
            @Override
            public boolean isCellEditable(int row, int column) {

                  switch (row){
                      case 10: //Active
                        switch (column) {
                            case 0:
                                return false;
                            case 1:
                                return true;
                            case 2:
                                return false;
                            case 3:
                                return false;
                            default:
                                return false;
                        }
                    default:
                 return false;
                }
            }
        };

Here is the picture of the JTable when I double click on JCheckBoxe:

enter image description here

Is there anyway I can checked or unchecked the JCheckBoxes with one click? I don't wanna update JTable cell with 0 OR 1 in order to checked or unchecked. Please Help.

Cheers.


Solution

  • When I clicked twice, the editable JTable cell become active and shows the value of JCheckBox which is 0 OR 1.

    If you want the default renderer/editor to be used then you should be storing Boolean.TRUE or Boolean.FALSE in the TableModel.

    Then you also need to override the getColumnClass() method of the TableModel to return Boolean.class.

    Read the section from the Swing tutorial on How to Use Tables for more information and working examples.