Updated March 18, 2023
Introduction to JavaFX CheckBox
In JavaFX CheckBox package, CheckBox is a box that is said to be selected if a tick exists in it, else it is considered empty. Even though it is similar to a radio button, it can’t be united into toggle groups. That is, multiple options can’t be chosen at the same time. JavaFX Checkbox is under the package javafx.scene.control and has a class Checkbox. In this, 3 states are present such as Checked, Unchecked and Undefined.
These states, constructors, and examples of the JavaFX checkbox will be discussed in the following sections.
CheckBox States: In JavaFX CheckBox, 3 states are present. They are,
- Checked: Indeterminate -> false
Checked -> true
- Unchecked: Indeterminate -> false
Checked -> False
- Undefined: Indeterminate -> true
JavaFX CheckBox Constructors
There are two constructors for JavaFX CheckBox.
1. CheckBox(): A checkbox will be created with an empty string for its label.
// create a checkbox
CheckBox cb = new CheckBox();
2. CheckBox(String s): A checkbox will be created with a string as its label.
// create a checkbox
CheckBox cb = new CheckBox(st[a]);
Methods
The following are some of the commonly used methods:
- isIndeterminate(): Indeterminate property’s value will be returned.
- isSelected(): The selected property’s value will be returned.
- setIndeterminate(boolean v): Indeterminate property’s value will be set.
- setSelected(boolean v): Selected property’s value will be set.
- selectedProperty(): Returns whether the CheckBox is checked or not.
How to Create a Checkbox?
There are several steps to create a checkbox.
1. Set the title for the stage created
s.setTitle("CheckBox Sample");
2. Create a checkbox
In order to create a checkbox, the following syntax can be used.
CheckBox cb = new CheckBox("Red");
If a default constructor is needed, the following syntax can be used.
CheckBox cb = new CheckBox();
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(cb);
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();
Examples to Implement JavaFX CheckBox
Now let us see some of the JavaFX programs.
Examples #1
Java program to demonstrate displaying one Checkbox.
Code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
//Sample class that extends the base class Application
public class JavaFXCheckboxExample extends Application {
//start the application
@Override
public void start(Stage s)
{
//set the title
s.setTitle("CheckBox Sample");
//create a checkbox with text as Red
CheckBox cb = new CheckBox("Red");
//horizontal box creation
HBox hb = new HBox(cb);
//create a scene
Scene sc = new Scene(hb, 300, 200);
//set the scene
s.setScene(sc);
//displays the result
s.show();
}
//main method
public static void main(String[] args) {
//start the application
Application.launch(args);
}
}
Output:
It can be seen that the checkbox Red is not ticked here. If it is ticked, the output will be as below.
Explanation
- A program that contains a Checkbox Red
- The scene gets created and it is added to the Scenegraph.
- At last, results will be displayed.
Examples #2
Java program to demonstrate displaying multiple Checkboxes.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
//sample program that extends the base class Application
public class JavaFXCheckboxExample extends Application {
//main method
public static void main(String[] args) {
//launch the application
launch(args);
}
//start the application
@Override
public void start(Stage s) {
//create label
Label lb = new Label("Please tick your favorite type of dish ");
//4 checkboxes
CheckBox cb1 = new CheckBox("North Indian Veg Dish");
CheckBox cb2 = new CheckBox("North Indian Non Veg dish");
CheckBox cb3 = new CheckBox("South Indian Veg Dish");
CheckBox cb4 = new CheckBox("South Indian Non veg Dish");
//horizontal box
HBox r = new HBox();
//add the label, checkboxes
r.getChildren().addAll(lb,cb1,cb2,cb3,cb4);
r.setSpacing(5);
//create scene
Scene sc=new Scene(r,700,100);
//set the scene
s.setScene(sc);
//set the title
s.setTitle("CheckBox Sample");
//display the result
s.show();
}
}
Output:
Explanation
- In this program, 4 checkboxes are present such as North Indian Veg Dish, North Indian Non-Veg dish, South Indian Veg Dish, South Indian Nonveg Dish.
- Once the checkboxes are created, Scene gets created and it is added to the Scenegraph.
- At last, results will be displayed.
Examples #3
Java program to demonstrate displaying multiple Checkbox with the help of event handlers.
Code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.*;
import javafx.collections.*;
//sample class
public class JavaFXCheckbox extends Application {
// application launches here
public void start(Stage s)
{
// Title for the stage is set
s.setTitle("CheckBox iks created");
// tile pane is created
TilePane tp = new TilePane();
// label is created
Label lb = new Label("check box samples");
// string array
String str[] = { "Veg", "Non veg", "Desserts" };
// add label
tp.getChildren().add(lb);
for (int i = 0; i < str.length; i++) {
//checkbox is created
CheckBox cb = new CheckBox(str[i]);
tp.getChildren().add(cb);
Label lbl = new Label(str[i] + " not selected");
String strng = str[i];
// an event handler creation
EventHandler<ActionEvent> ev = new EventHandler<ActionEvent>() {
public void handle(ActionEvent e)
{
if (cb.isSelected())
lbl.setText(strng + " selected ");
else
lbl.setText(strng + " not selected ");
}
};
// In checkbox, event is set
cb.setOnAction(ev);
tp.getChildren().add(lbl);
}
// Scene creation and adding it to the scene graph
Scene sc = new Scene(tp, 150, 200);
s.setScene(sc);
s.show();
}
public static void main(String args[])
{
// launch the application
launch(args);
}
}
Output:
Explanation
- In this program, 3 checkboxes are present.
- The result will display a text-based on whether the checkbox is selected or not.
Conclusion
In JavaFX CheckBox, in order to choose options, several icons are used. A checkbox is one in which users can choose multiple options at the same time. It uses both parameterized and non-parameterized constructors. Several methods offer different purposes such as determining whether the box is checked or not, setting the Selected Property’s value, etc.
Recommended Articles
This is a guide to JavaFX CheckBox. Here we discuss constructors, methods, and several steps to create a checkbox in JavaFX along with the examples and implementation. You may also look at the following articles to learn more –