Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use radio buttons groups in javafx?

Tags:

java

javafx-8

I need to make a group of 2 radio buttons and then retrieve the value of the selected one.

like image 387
Fayrouz Ouerghi Avatar asked Dec 06 '22 15:12

Fayrouz Ouerghi


1 Answers

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

like image 89
fabian Avatar answered Dec 08 '22 03:12

fabian