-2

I have a group of radio buttons that doesn't includes any text on frame 1.

Every button has a j label l, which means that when I choose one radio button, I want to change the icon of a specific j label.

I want to know which button is selected to send it to frame 2 by a constructor.

So I want send the variable of the current radio button that's clicked.

How can i do that?

public String getSelectedButtonText(ButtonGroup buttonGroup) {
    for (
        Enumeration<AbstractButton> buttons = buttonGroup.getElements();
        buttons.hasMoreElements();
    ) {
        AbstractButton button = buttons.nextElement();

        if (button.isSelected()) {
            return button.getText();
        }
    }

    return null;
}
Chris Martin
  • 30,334
  • 10
  • 78
  • 137

1 Answers1

0
JRadioButton radioA = new JRadioButton(0);
JRadioButton radioB = new JRadioButton(0);
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(radioA);
buttonGroup.add(radioB);

JLabel label = new JLabel("The label");

When you click the button to go to the next frame....

if(radioA.isSelected()) {
  label.setText("Radio A is Selected");
  gotoNextFrame();
}
else if(radioB.isSelected()) {
  label.setText("Radio B is Selected");
  gotoNextFrame();
}

More - How do I get which JRadioButton is selected from a ButtonGroup

Community
  • 1
  • 1
new Objekt
  • 414
  • 3
  • 8