javafx Use Groups on Radio Buttons


Example

A ToggleGroup is used to manage the RadioButtons so that just one in each group can be selected at each time.

Create a simple ToggleGroup like following:

ToggleGroup group = new ToggleGroup();

After creating a Togglegroup it can be assigned to the RadioButtons by using setToggleGroup(ToggleGroup). Use setSelected(Boolean) to pre-select one of the RadioButtons.

RadioButton radioButton1 = new RadioButton("stackoverlow is awesome! :)");
radioButton1.setToggleGroup(group);
radioButton1.setSelected(true);

RadioButton radioButton2 = new RadioButton("stackoverflow is ok :|");
radioButton2.setToggleGroup(group);

RadioButton radioButton3 = new RadioButton("stackoverflow is useless :(");
radioButton3.setToggleGroup(group);