Updated March 18, 2023
Introduction on JavaFX Radio Button
JavaFX package contains radio buttons that are meant to create an item series where only one item can be selected at a time. It is almost similar to the Toggle Button in JavaFX, except that it cannot be unselected once anyone is selected. The working of the Radio Button is in a way that once it is pressed and released, an Action Event will be sent, which can be handled with the help of an Event Handler. Since this button is not in any toggle group, it has to be added in any of the toggle groups in order to set functionality that the user should not choose more than 1 item at a time.
Constructors of JavaFX Radio Button
RadioButton in JavaFX package is under the class javafx.scene.control.RadioButton.
There are two constructors for JavaFX.
1. RadioButton(): A radio button will be created with an empty string for its label.
// create a radiobutton
RadioButton rb = new RadioButton();
2. RadioButton(Strings): A radio button will be created with a string s as its label.
// create a radiobutton
RadioButton rb = new RadioButton(" A ");
Methods of JavaFX Radio Button
5 methods of JavaFX Radio Button are mentioned below:
1. getText(): The text label for the JavaFX radio button will be returned.
2. isSelected(): Returns whether the radio button is chosen or not.
3. fire(): The state of the button will be returned if it is not related to any ToggleGroup or if it is not selected.
4. setSelected(boolean v): Sets whether the radio button is chosen or not.
5. setToggleGroup(ToggleGroup g): The radio button’s toggle group will be set.
How to Create a Radio Button in JavaFX?
There are several steps to create a RadioButton.
1. Set the title for the stage created
s.setTitle("Radio Button Sample");
2. Create a checkbox
In order to create a checkbox, the following syntax can be used.
RadioButton RB = new RadioButton (" Both happy and sad ");
If a default constructor is needed, the following syntax can be used.
RadioButton RB = new RadioButton ();
3. Create the horizontal box
Two types of boxes are available- Vertical box and Horizontal box.
In this step, create a Horizontal box as follows.
HBox HB = new HBox (RB);
4. Add Checkbox created to the Scene Graph
After the HBox creation, add the checkbox to the scene graph using the below steps.
//create a scene
Scene sc = new Scene(hb, 300, 200);
//set the scene
s.setScene(sc);
//displays the result
s.show();
Program to implement JavaFX Radio Button
Now, let us see some of the JavaFX programs that implement Radio Button.
Program #1
Java Program to display one radio button
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.scene.control.RadioButton;
//sample class that extends the application
public class JavaFXRadioButtonExample extends Application {
//starting of the application
@Override
public void start(Stage s) {
//setting title for the stage
s.setTitle("Radio Button Sample");
//creation of radio butto
RadioButton rb = new RadioButton("Both A and B");
//Creation of horizontal box
HBox hbox = new HBox(rb);
//scene creation
Scene sc = new Scene(hbox, 250, 100);
//setting the scene
s.setScene(sc);
//displaying th result
s.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Output:
If the button is selected, the result will be as displayed below.
Explanation:
- Here, 1 radio button, “Both A and B”, present.
- The scene gets created, and it is added to the Scenegraph.
- At last, results will be displayed.
- That is, a black dot will be present in the button if it is selected.
Program #2
Java Program to display multiple radio buttons
import javafx.application.Application;
import javafx.scene.control.RadioButton;
import javafx.scene.layout.VBox;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.ToggleGroup;
//sample class
public class JavaFXRadioButtonExample extends Application {
public static void main(String[] args) {
launch(args);
}
//starting of an application
@Override
public void start(Stage s) {
//create a toggle group
ToggleGroup gp = new ToggleGroup();
//create radiobuttons
RadioButton b1 = new RadioButton("Happy");
RadioButton b2 = new RadioButton("Sad");
RadioButton b3 = new RadioButton("Angry");
RadioButton b4 = new RadioButton("Mixed Emotion");
RadioButton b5 = new RadioButton("None of the above");
b1.setToggleGroup(gp);
b2.setToggleGroup(gp);
b3.setToggleGroup(gp);
b4.setToggleGroup(gp);
b5.setToggleGroup(gp);
//create vertical box
VBox vb=new VBox();
vb.setSpacing(10);
vb.getChildren().addAll(b1,b2,b3,b4,b5);
//create scene
Scene sc=new Scene(vb,400,300);
//set the scene and add it to the scene graph
s.setScene(sc);
s.setTitle("Radio Button Sample");
s.show();
}
}
Output:
Explanation:
- Even though 5 radio buttons are present such as Happy, Sad, Angry, Mixed Emotion, and None of the above, only one can be selected at the same time.
- Once the radio buttons are created, a Scene gets created, and it is added to the Scenegraph.
- At last, results will be displayed.
Program #3
Java Program to display multiple radio buttons
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.*;
import javafx.event.EventHandler;import javafx.scene.control.*;
//sample class
public class JavaFXRadioButtonExample extends Application {
// application launches here
public void start(Stage s)
{
// Title for the stage is set
s.setTitle("CheckBox iks created")
// label is created
Label lb1 = new Label("Radiobutton example ");
// toggle group is created
ToggleGroup tgp = new ToggleGroup();
String strng1 = "Happy";
String strng2 = "Sad";
String strng3 = "No emotions";
// radiobuttons is created
RadioButton rb1 = new RadioButton(strng1);
RadioButton rb2 = new RadioButton(strng2);
RadioButton rb3 = new RadioButton(strng3);
// add radiobuttons
rb1.setToggleGroup(tgp);
rb2.setToggleGroup(tgp);
rb3.setToggleGroup(tgp);
//create the vertical box
VBox vb=new VBox();
vb.getChildren().add(lb1);
vb.getChildren().add(rb1);
vb.getChildren().add(rb2);
vb.getChildren().add(rb3);
vb.setSpacing(10);
// vb.getChildren().addAll(rb1,rb2,rb3);
// Scene creation and adding it to the scene graph
Scene sc = new Scene(vb, 150, 200);
s.setScene(sc);
s.show();
}
public static void main(String args[])
{
// launch the application
launch(args);
}
}
Output:
Explanation:
- In this example, 3 radio buttons are present.
- Unlike example 2, here, instead of the addAll() method, add() method is used.
- Then, the scene gets created, and it is added to the Scenegraph.
- At last, results will be displayed.
Conclusion
In JavaFX, in order to select only one option at that time, Radio Buttons are used. Only if it is added to any toggle group it is possible. In detail, syntax, constructors, methods, and creation of JavaFX Radio buttons are explained in this document.
Recommended Articles
This is a guide to a JavaFX Radio Button. Here we discuss the introduction, Methods of JavaFX Radio Button, How to Create a RadioButton, and Program to implement. You can also go through our other suggested articles to learn more–