This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Problem in Editing/Rendering a CheckBox in a JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem in Editing/Rendering a CheckBox in a JTable" Watch "Problem in Editing/Rendering a CheckBox in a JTable" New topic
Author

Problem in Editing/Rendering a CheckBox in a JTable

vijayender madura
Greenhorn

Joined: Jun 18, 2009
Posts: 13
Hi,
I am rendering a JCheckBox in a JTable using the following code:-



I ensure that I pass a ProductInfoBean Object in the getValueAt() method of my implementation of the AbstractTableModel(A JGoodies Table Model) that I am using for my JTable.
Also, I use the setValueAt() method to update my bean based on whether the user checks/un-checks the checkboxes on each row :-



So far so good… 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.

________________________________________
******* Confidentiality Notice *******
This email, its electronic document attachments, and the contents of its website linkages may contain confidential health information. This information is intended solely for use by the individual or entity to whom it is addressed. If you have received this information in error, please notify the sender immediately and arrange for the prompt destruction of the material and any accompanying attachments.

'Scientia est Potentia'
vijayender madura
Greenhorn

Joined: Jun 18, 2009
Posts: 13
A minor correction in my post -

I ensure that I returna ProductInfoBean Object in the getValueAt() method of my implementation of the AbstractTableModel(A JGoodies Table Model) that I am using for my JTable.
And also the AbstractTableModel is not a JGoodies class ... its just a good ol' swing class...sorry about that...
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Check out DefaultCellEditor#setClickCountToStart(int)
The default is two


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
vijayender madura
Greenhorn

Joined: Jun 18, 2009
Posts: 13
Hi,

i tried your suggestion:-



but i'm still facing the same problem...

thanks
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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.

bitguru blog
Jeffrey Lin
Greenhorn

Joined: Sep 24, 2010
Posts: 1
Brian Cole wrote:
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.


Thanks this saved me.
vijayender madura
Greenhorn

Joined: Jun 18, 2009
Posts: 13
a little late (actually very late...) ... but i forgot to post a thanks for the solution... it helped me solved the problem

thanks Brian :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in Editing/Rendering a CheckBox in a JTable
 
Similar Threads
Check box in a Table
JTable - selecting multiple rows by selecting checkbox column
Checkbox editor for JTable column
JCheckBox in JTable
JCheckBox in JTable not working