vijayender madura wrote: but now I have a problem…
The CheckBoxes need to be clicked twice to get checked when I change rows- ie: when I click a checkbox on the first row, it gets checked immediately. Right after that,I click on the checkbox on the second row – but nothing happens – only when I click it again, it gets checked – it appears like the first click is to select the cell containing the checkbox and the second click actually sets the checkbox but i'm not sure – Can anyone help understand why this is happening? Thanks in advance.
Your problem is that you are using the same exact instance of JCheckBox for both
the renderer and the editor. This is a no-no because, for instance, the renderer may
be used to repaint table cells while an edit is in progress.
It should work better if you change your editor along these lines:
notes:
1) I wouldn't usually do it quite this way, but I've tried to somewhat keep your coding style.
2) Now that the editor is no longer using the renderer's checkbox, the checked/unchecked status of the editor's checkbox needs to be set before the user sees it. That's why getTableCellEditorComponent() has been overridden to pass a Boolean (not a ProductInfoBean) to super.getTableCellEditorComponent().
3) Notice that line 02 is no longer needed because of what line 10 is doing. You could get rid of line 10 and it would still work but I think it's better if you leave it in, then do the same thing for
checkBox and
disabledCheckBox in the getTableCellRendererComponent() method so the painting of table selection looks right.
4) On the topic of
checkBox and
disabledCheckBox, you don't really need both of them. It would be simpler to use a single instance of JCheckBox in the renderer, and just pass either true or false to it's setEnabled() method each time in the getTableCellRendererComponent() method.
5. There is no reason for your renderer to extend DefaultTableCellRenderer (which is a subclass of JLabel) since you're not using any of its label-like characteristics. If you change the
DefaultTableCellRenderer to
TableCellRenderer in your code everything should work the same.