How to create a JavaFX Radio Button

JavaFX Radio Button is used to make a selection from a list of options. A radio button can be selected or unselected. Usually two or more radio buttons are used in a group where only one radio button can be selected at a time. To create a Radio Button, We use the RadioButton class available in the javafx.scene.control package.

RadioButton radioButton1 = new RadioButton();
radioButton1.setText('radioButton1');

or

RadioButton radioButton2 = new RadioButton('radioButton2');

Let’s create a radio button

package javafxcontrols;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class JavaFxControls extends Application {

     @Override
    public void start(Stage primaryStage) throws Exception {
        
        RadioButton radioButton1 = new RadioButton("Option 1");
        
        VBox vbox = new VBox(radioButton1);
        Scene scene = new Scene(vbox, 200, 100);
        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX Radio Button");
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }

}

outout:

radio button

JavaFX Radio Button is used in a group. Only one radio button in a group can be selected. To create a group, We use the ToggleGroup class available in the javafx.scene.control package. setToggleGroup(); method is used to set the ToggleGruop of a Radio Button

final ToggleGroup group = new ToggleGroup();
RadioButton radioButton1 = new RadioButton('radioButton1');
radioButton1.setToggleGroup(group);

Les’t code:

package javafxcontrols;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class JavaFxControls extends Application {

     @Override
    public void start(Stage primaryStage) throws Exception {
        
        final ToggleGroup group = new ToggleGroup();
        
        RadioButton option1 = new RadioButton("Option 1");
        option1.setToggleGroup(group);
        
        RadioButton option2 = new RadioButton("Option 2");
        option2.setToggleGroup(group);
        
        RadioButton option3 = new RadioButton("Option 3");
        option3.setToggleGroup(group);
        
        RadioButton option4 = new RadioButton("Option 4");
        option4.setToggleGroup(group);
        
        VBox vbox = new VBox(option1,option2,option3,option4);
        Scene scene = new Scene(vbox, 300, 100);
        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX Radio Button in a Group");
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }

}

output:

radio button group

You can explicitly make a radio button selected by using the setSelected method and specifying its value as true. If you need to check whether a particular radio button was selected by a user, apply the isSelected method.