How to get selected radio button from ToggleGroup
Asked Answered
B

5

31

I an working on JavaFX 8 and SceneBuilder. I created some radio buttons in the FXML File and specified a toggleGroup name to a radio button list in that. So, now I want to get the toggleGroup's selected radio button in my controller, do I need to make all the radio buttons again as fields in the controller, or just the toggleGroup object will get me the selected radio button (the text of that radio button only, not the button object).

Birdiebirdlike answered 6/9, 2015 at 15:2 Comment(0)
A
53
 @FXML
 ToggleGroup right; //I called it right in SceneBuilder.

later somewhere in method.

RadioButton selectedRadioButton = (RadioButton) right.getSelectedToggle();
String toogleGroupValue = selectedRadioButton.getText();
Amytal answered 26/3, 2017 at 10:42 Comment(2)
Quick and simple!Forebode
Much simpler Jack, very nice!Allspice
T
19

Let's say you have a toggle group and three radio buttons belonging to that group.

ToggleGroup group = new ToggleGroup();

RadioButton rb1 = new RadioButton("RadioButton1");
rb1.setUserData("RadioButton1");
rb1.setToggleGroup(group);
rb1.setSelected(true);

RadioButton rb2 = new RadioButton("RadioButton2");
rb2.setUserData("RadioButton2");
rb2.setToggleGroup(group);

RadioButton rb3 = new RadioButton("RadioButton3");
rb3.setUserData("RadioButton3");
rb3.setToggleGroup(group);

When you select a radio button from that toggle group, the following changed(...) method will be called.

group.selectedToggleProperty().addListener(new ChangeListener<Toggle>(){
    public void changed(ObservableValue<? extends Toggle> ov, Toggle old_toggle, Toggle new_toggle) {

         if (group.getSelectedToggle() != null) {

             System.out.println(group.getSelectedToggle().getUserData().toString());
             // Do something here with the userData of newly selected radioButton

         }

     } 
});
Tungus answered 6/9, 2015 at 17:21 Comment(3)
I was asking if I am reqd. to make the 3 RadioButtons again in the controller (after having made them in FXML), or I can make just the ToggleGroup object only and access the selected button somehow.Birdiebirdlike
@Rounaq_intel, try just adding the togglegroup to the controller and adding the listener in above answer.Ianthe
Thanks, just what I was looking for.Digitalism
A
12

This was never properly or thoroughly answered, so I thought I would post the solution I got.

When you create radio buttons in SceneBuilder, then ALSO use SceneBuilder to assign them to a group. The way you access that group via the Controller is to first create a variable of type ToggleGroup in the Controller and name it the exact same name as the one you created in SceneBuilder. Then you can use it. Here is a pseudocode example of how I did it:

// your imports
public class Controller
{
    @FXML ToggleGroup   myGroup; //I called it myGroup in SceneBuilder as well.

    public void myGroupAction(ActionEvent action)
    {
      System.out.println("Toggled: " + myGroup.getSelectedToggle().getUserData().toString());
    }

    public void initialize()
    {
      //whatever initialize code you have here
    }
}

Although the text returned from the getUserData property is lengthy. Here is a way to get just the name of the Radio Button:

myGroup.selectedToggleProperty().addListener(new ChangeListener<Toggle>()
    {
    @Override
    public void changed(ObservableValue<? extends Toggle> ov, Toggle t, Toggle t1)
        {
        RadioButton chk = (RadioButton)t1.getToggleGroup().getSelectedToggle(); // Cast object to radio button
        System.out.println("Selected Radio Button - "+chk.getText());
        }
    });

Hope this helps someone down the road ...

Allspice answered 14/7, 2016 at 1:24 Comment(0)
L
2
public class MyControler {
    @FXML
    private ToggleGroup myToggleGroup;

    @FXML
    public void initialize(){
        //the simplest way to print current value (text of the selected radio button)
        System.out.format("Selected Radio Button: " + ((RadioButton)myToggleGroup.getSelectedToggle()).getText());

        //register on change event
        myToggleGroup.selectedToggleProperty().addListener(new ChangeListener<Toggle>()
        {
            @Override
            public void changed(ObservableValue<? extends Toggle> observable, Toggle oldToggle, Toggle newToggle)
            {
                //print new selected value after change
                System.out.println("Selected Radio Button: " + ((RadioButton)newToggle).getText());
            }
        });
    }

    //sample method to set radio button (unselect all if group don't contain value)
    private void setRadioButtons(ToggleGroup toggleGroup, String value) {
        for (Toggle t : toggleGroup.getToggles()) 
            t.setSelected(((RadioButton) t).getText().equals(value));
    }

    //sample method to set radio button (select existing value or do nothing)
    private void setRadioButton(ToggleGroup toggleGroup, String value) {
        for (Toggle t : toggleGroup.getToggles()) 
            if(((RadioButton) t).getText().equals(value)) 
                t.setSelected(true);
    }
}
Lysimachus answered 17/2, 2020 at 20:9 Comment(0)
O
0

The solution I got is:

ToggleGroup toggleGroup = new ToggleGroup();

Then set toggleGroup to the radio button; let's say r1 and r2

r1.setToggleGroup(toggleGroup);

r2.setToggleGroup(toggleGroup);

toggleGroup.setUserData(toggleGroup.getSelectedToggle().toString());

To print the data of RadioButton:

System.out.println("Selected Radio Button is " + toggleGroup.getUserData().toString());
Obrien answered 2/6, 2020 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.