I need to make a group of 2 radio buttons and then retrieve the value of the selected one.
Simply use a ToggleGroup
RadioButton radioButton1 = ...
RadioButton radioButton2 = ...
// TODO: add RadioButtons to scene
ToggleGroup toggleGroup = new ToggleGroup();
radioButton1.setToggleGroup(toggleGroup);
radioButton2.setToggleGroup(toggleGroup);
// listen to changes in selected toggle
toggleGroup.selectedToggleProperty().addListener((observable, oldVal, newVal) -> System.out.println(newVal + " was selected"));
You can also retrieve the selected radio button from the ToggleGroup
using
toggleGroup.getSelectedToggle()
in case you want to do this from the handler of a submit button or something like that...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With