Search code examples
javaeclipseuser-interfacebuttongroup

get the name of the selected radio button from button group


I want to return selected radio button name from button group in java :

ButtonGroup bg = new ButtonGroup();
bg.add(radiobutton1);
bg.add(radiobutton2);
bg.add(radiobutton3);

Solution

  • I think you can loop through the radio buttons, for each button if isSelected() is true then call button.getText() to get the name.

    for (AbstractButton button : bg.getElements())
        if (button.isSelected())
            return button.getText();