• Post Reply Bookmark Topic Watch Topic
  • New Topic

Another CheckBox-Question

 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I am trying to change the Selection-Setting on a JCheckBox. Basically I have a JPane pane that several JCheckBoxes are added to. Thanks to you guys that is all working beautifully.

Now I would like a JButton to perform "Check All" (meaning set all CheckBoxes in the pane to isSelected = true . So in my actionPerformed I need to, well, check all. I can get access to the CheckBoxes with pane.getComponent(index) in a for-loop. I can disable them with setEnabled(false), I can find out whether they are selected or not with isSelected(), but I can't seem to change the setting. It's probably because getComponent returns a component, not a Button.

Or can I perhaps trigger the JCheckBoxes actionPerformed? That would be the easiest for me.

Thank you for your thoughts, M
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Martin Vietor:
It's probably because getComponent returns a component, not a Button.

You can always cast it down to the JCheckBox class then check it. You may want to check if the component is instanceof JCheckBox before casting though since you may have other component types.
 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
um, yes, good idea. How do I cast a thing to a thing with the instance of a thing?
[ December 08, 2008: Message edited by: Martin Vietor ]
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Casting is easy:

AbstractButton, of which JCheckBox is an indirect subclass, has to methods called doClick with which you can programmatically perform a click, and therefore also trigger the event.
 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sweet, working fine. I am using your code and then



It is painfully slow, though, one checkBox after the other get's clicked, and that takes a long time. Will probably do it manually. But thank you so much, that was exactly what I needed.

M
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may run into another risk: if one selection change triggers another, which then triggers another, etc. You need to be aware of this.
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The response should be in the matter of few miliseconds. If it is recognisably slow, then there must be some funny loopings going on there.
 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, if you choose .doClick as a means of selecting or unselesting all buttons the buttons actually get clicked: The icon changes briefly as if manually clicked. One after the other. Which takes about a second, maybe a little less for my 13 Checkboxes. I chose .setSelected(true) insraed and included the code in the respective actionPerformed-methods - miliseconds it is.

Thanks for the warning about the looping, not a problem in this case though.

M
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Martin Vietor:



It is painfully slow


Originally posted by Duc Vo:
The response should be in the matter of few miliseconds. If it is recognisably slow, then there must be some funny loopings going on there.


The doClick() method simulates the user pressing the button for a duration of time, 68ms I believe.

Unless you want the user to see this effect, I recommend something like
It should be much faster.
 
Darryl Burke
Bartender
Posts: 5154
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Brian's is the best suggestion, except that I would omit the check for !isSelected(), as I don't see any problem in invoking setSelected(true) on a JCheckBox that is already selected.

If however doClick is desirable for other reasons (can't see there would be any, but...) there is an overload that takes an int parameter. Note that doClick would toggle the state of the check box, not necessarily set it to selected == true.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!