user5928703 user5928703 - 2 years ago 82
Java Question

How to make a switch statement with JRadio Buttons in Java

Beginner at Java here, kinda of confused here, so I know how to make switch statements and I know how to use JRadio buttons. Just having trouble putting it all together to do a switch statement using radio buttons. I do have the JRadio buttons that I want to use in a JRadio button group called payFrequency.

Update: example of code I am trying to use, so I know this below is incorrect, I would advise to provide and example of what I was trying to do.( payFrequency is the button group that the other radio button are in dont know if that information is relevant.)

case jRadioButton1.isSelected():
sal1= (sal1a + sal1b) * 2.15;
case jRadioButton2.isSelected():
sal1= (sal1a + sal1b) * 4.3;
case jRadioButton3.isSelected():
sal1= (sal1a + sal1b) * 4.3;
sal1= sal1a + sal1b;

Answer Source

JDK7+ supports switch with all primitives and String objects. So no you cannot use a radio button in a switch statement. But you can use myRadioButton.getText() in your switch which will return the text label of the radio button. Then you can take appropriate actions for each case inside your switch.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download