How to get selected radio button from ToggleGroup

rjalfa picture rjalfa · Sep 6, 2015 · Viewed 65.8k times · Source

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

Answer

PsychedelicSubstance picture PsychedelicSubstance · Mar 26, 2017
 @FXML
 ToggleGroup right; //I called it right in SceneBuilder.

later somewhere in method.

RadioButton selectedRadioButton = (RadioButton) right.getSelectedToggle();
String toogleGroupValue = selectedRadioButton.getText();