2

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.

Sadequer Rahman
  • 133
  • 5
  • 11
  • 2
    The best way to get us to fully and quickly understand your problem would be if you were to to create and post a [minimal example program](http://stackoverflow.com/help/mcve), a small but complete program that only has necessary code to demonstrate your problem, that we can copy, paste, compile and run without modification. – Hovercraft Full Of Eels Oct 06 '16 at 13:39

1 Answers1

3

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thank you very much for your reply. As you can see I am working with a table where I have to specify row and column index. getColumnClass() takes only column as parameter. Is there any working sample similar to my scenario. Thanks a bunch in advance. Cheers. – Sadequer Rahman Oct 06 '16 at 18:17
  • @SadequerRahman, A JTable is designed for the same data in each row. I would suggest you should not be using a JTable for the form displayed in your question. Just create a panel using JLabels, JCheckboxes, and JTextFields. Maybe you could create a JTable with 4 rows and 2 columns for your "enable data". If you really want to play with different data in the same column then this link might give you some ideas: http://stackoverflow.com/questions/33216409/class-specific-renderer-component-not-called/33218124#33218124 – camickr Oct 06 '16 at 18:50