Evaluating Checkbox Boolean Value
Asked Answered
S

3

18

I'm sure this is a rediculously easy question, but I just can't find the answer to it anywhere. I have a JCheckbox that I need to evaluate the boolean value of, and then change the value with an if statement. The problem is I just can't find the syntax anywhere for evaluating the contents of a JCheckbox, let alone changing it. This will probably be really easy one, but I just can't seem to find anything helpful. Thanks!

Sheya answered 30/5, 2012 at 17:35 Comment(1)
Its inherited from AbstractButton.Bethune
L
31

This SO thread sort of answers your question. If your JCheckBox is named "Foo", you would check its value with

Foo.isSelected()

To set its value, you would use

Foo.setSelected(true)
Lyon answered 30/5, 2012 at 17:38 Comment(5)
I don't have a Java compiler handy right now, so I can't say for sure, but I'm pretty sure that just doing Foo.setSelected(true) will update the checkbox to be checked on the form.Lyon
While it works initially in an if statement, any further changes made do not change the other box.Sheya
Never Mind, I foudn the method I was looking for.Sheya
What was it? Future generations might like to know.Lyon
It's the itemListen method. Found it in another question.Sheya
U
9

Do you mean how to check if Checkbox is selected or not, if yes then use isSelected

boolean isSelected = jCheckBox.isSelected();

if(isSelected ){
   jCheckBox.setSelected(false);
} else {
   jCheckBox.setSelected(true);
}
Understate answered 30/5, 2012 at 17:38 Comment(1)
add ItemListener to the JCheckBox,Lutenist
B
2

There is an awesome tutorial from Sun that you can read to complete your knowledge. If you want to know the current selection state of a JCheckbox, just use the method isSelected().

Boxboard answered 30/5, 2012 at 17:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.