Moath Wafeeq
Moath Wafeeq

Reputation: 45

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);

Upvotes: 1

Views: 4279

Answers (2)

Farbod
Farbod

Reputation: 36

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();

Upvotes: 2

FredK
FredK

Reputation: 4084

Why do you want the button name? It isn't of much use. You can, however, get the all of the button references using bg.getElements(), and you can get the text of each of those references with button.getText(), both of which are of much more use than the name you gave the button. If you REALLY want the name, you can create the buttons and then call radiobutton1.setName("radiobutton1") and then use getName() on the reference.

Upvotes: 1

Related Questions