Adamo Adamo - 4 months ago 15
Java Question

Java - JRadioButton isSelected

I have simple check like this:

boolean isPressed;


if ((jRadioButton1.isSelected() == false) || (jRadioButton2.isSelected() == false) || (jRadioButton3.isSelected() == false)) {
isPressed = false;
} else {
isPressed = true;
}
System.out.println(isPressed);


Why I always get false, even if I mark RadioButton in panel ?

Answer

Your logic implies that if either of your radio buttons isn't pressed then isPressed is false.

Try switching the logic around

if ((jRadioButton1.isSelected() == true) || (jRadioButton2.isSelected() == true) || (jRadioButton3.isSelected() == true)) {
    isPressed = true;
} else {
    isPressed = false;
}
System.out.println(isPressed);

EDIT: Can be simplified to

isPressed = (jRadioButton1.isSelected() || jRadioButton2.isSelected() || jRadioButton3.isSelected()) 
Comments