This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in Editing/Rendering a CheckBox in a JTable

 
vijayender madura
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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
  • 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...
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Hi,

i tried your suggestion:-



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

thanks
 
Brian Cole
Author
Ranch Hand
Posts: 906
1
  • Mark post as helpful
  • send pies
  • 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.
 
Jeffrey Lin
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
  • 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 :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic