• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem in Editing/Rendering a CheckBox in a JTable

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
vijayender madura
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out DefaultCellEditor#setClickCountToStart(int)
The default is two
 
vijayender madura
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

i tried your suggestion:-



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

thanks
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a little late (actually very late...) ... but i forgot to post a thanks for the solution... it helped me solved the problem

thanks Brian :-)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic