JavaFX CheckBox
JavaFX CheckBox is a control that lets the user turn an independent option on or off. It is commonly used for preferences, optional features, filters, and settings where more than one option may be selected.
In this tutorial, you will learn how to create a JavaFX CheckBox, determine whether it is selected, set its state programmatically, listen for selection changes, use its indeterminate state, disable it, and work with multiple check boxes.
Create a JavaFX CheckBox with a Label
To initialize a CheckBox control, you can use CheckBox class as shown below.
CheckBox checkBox = new CheckBox("Some Label");
The text passed to the constructor is displayed beside the selection box. You can also create an unlabeled CheckBox with new CheckBox() and assign text later with setText().
Following is a basic example to demonstrate how to initialize check box in JavaFX using CheckBox class and show it on a scene.
JavaFxCheckBoxTutorial2.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
public class JavaFxCheckBoxTutorial2 extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
try {
// set title
primaryStage.setTitle("JavaFX Check Box - tutorialkart.com");
//javafx check box
CheckBox checkBox = new CheckBox("TutorialKart");
// tile pane
TilePane tilePane = new TilePane();
// add check box to the tile pane
tilePane.getChildren().add(checkBox);
//set up scene
Scene scene = new Scene(tilePane, 400, 100);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
}
Run this Java application and you should see a CheckBox as shown below. You can click on the box or label to toggle the selection.

Check Whether a JavaFX CheckBox Is Selected
Use the isSelected() method to read the current state of a CheckBox. It returns true when the control is selected and false when it is not selected.
In this example, we will set up an action listener to the CheckBox. The callback function is called when there is a change in the selection (selected or not) for the CheckBox.
JavaFxCheckBoxTutorial.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
public class JavaFxCheckBoxTutorial extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
try {
// set title
primaryStage.setTitle("JavaFX Check Box - tutorialkart.com");
//javafx check box
CheckBox checkBox = new CheckBox("TutorialKart");
//add action listener to check box
checkBox.setOnAction(action -> {
System.out.println("Is CheckBox Selected: "+checkBox.isSelected());
});
// tile pane
TilePane tilePane = new TilePane();
// add check box to the tile pane
tilePane.getChildren().add(checkBox);
//set up scene
Scene scene = new Scene(tilePane, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
}
Run this Java application, and click on the check box multiple times. Every time there is a change in the state of the check box, the callback function is called and we are printing if the check box is selected or not.

Console output would be as shown below.

Set the JavaFX CheckBox Selection Programmatically
Call setSelected(true) to select a CheckBox in code. Pass false to clear its selection.
CheckBox notificationsCheckBox = new CheckBox("Enable notifications");
// Start with the option selected.
notificationsCheckBox.setSelected(true);
boolean notificationsEnabled = notificationsCheckBox.isSelected();
System.out.println("Notifications enabled: " + notificationsEnabled);
// Clear the selection later.
notificationsCheckBox.setSelected(false);
Setting the initial selection is useful when a form should display a saved preference or a default option.
Listen to the JavaFX CheckBox Selected Property
An action handler is suitable for responding to user actions. To observe every change to the selection property, including changes made by application code, add a listener to selectedProperty().
CheckBox checkBox = new CheckBox("Show advanced options");
checkBox.selectedProperty().addListener((observable, oldValue, newValue) -> {
System.out.println("Previous state: " + oldValue);
System.out.println("Current state: " + newValue);
});
The newValue parameter contains the current Boolean selection state. This approach is useful when another control or application setting must remain synchronized with the CheckBox.
Bind Another JavaFX Control to a CheckBox
JavaFX properties can be bound together. In the following example, a text field is disabled until the CheckBox is selected.
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
CheckBox customNameCheckBox = new CheckBox("Use a custom name");
TextField nameField = new TextField();
nameField.disableProperty().bind(
customNameCheckBox.selectedProperty().not()
);
When the CheckBox is not selected, the text field is disabled. Selecting the CheckBox enables it automatically without requiring a separate action handler.
Use the Indeterminate State of JavaFX CheckBox
A JavaFX CheckBox normally has two states: selected and unselected. It can also support an indeterminate state, which represents a value that is mixed, unknown, or only partially selected.
Enable three-state behavior with setAllowIndeterminate(true).
CheckBox selectAllCheckBox = new CheckBox("Select all items");
selectAllCheckBox.setAllowIndeterminate(true);
selectAllCheckBox.setIndeterminate(true);
if (selectAllCheckBox.isIndeterminate()) {
System.out.println("Only some items are selected.");
} else if (selectAllCheckBox.isSelected()) {
System.out.println("All items are selected.");
} else {
System.out.println("No items are selected.");
}
A common use is a parent “Select all” CheckBox. It can appear indeterminate when some child items are selected but others are not.
Disable or Hide a JavaFX CheckBox
Use setDisable(true) when the CheckBox should remain visible but should not accept user input. Use setVisible(false) when the control should not be displayed.
checkBox.setDisable(true);
checkBox.setVisible(false);
Disabling a CheckBox does not clear its selected state. If the stored value must also be reset, call setSelected(false) explicitly.
Read Values from Multiple JavaFX CheckBoxes
Check boxes are independent, so the user may select any number of them. The following example reads three CheckBox values when a button is clicked.
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
CheckBox emailCheckBox = new CheckBox("Email");
CheckBox smsCheckBox = new CheckBox("SMS");
CheckBox appCheckBox = new CheckBox("App notification");
Button saveButton = new Button("Save preferences");
saveButton.setOnAction(event -> {
System.out.println("Email: " + emailCheckBox.isSelected());
System.out.println("SMS: " + smsCheckBox.isSelected());
System.out.println("App: " + appCheckBox.isSelected());
});
Use separate CheckBox controls when choices are independent. When the user must select only one option from a group, use JavaFX radio buttons with a ToggleGroup instead.
JavaFX CheckBox Methods and Properties
| Method or property | Purpose |
|---|---|
isSelected() | Returns whether the CheckBox is selected. |
setSelected(boolean) | Selects or clears the CheckBox programmatically. |
selectedProperty() | Provides the observable Boolean selection property. |
setOnAction(handler) | Runs a handler when the user performs an action on the control. |
setAllowIndeterminate(boolean) | Enables or disables three-state behavior. |
isIndeterminate() | Returns whether the CheckBox is in its indeterminate state. |
setDisable(boolean) | Enables or disables user interaction. |
setText(String) | Changes the label displayed beside the CheckBox. |
JavaFX CheckBox Questions
How do I check whether a JavaFX CheckBox is selected?
Call checkBox.isSelected(). The method returns true for a selected CheckBox and false for an unselected one.
How do I select a JavaFX CheckBox by default?
Call checkBox.setSelected(true) after creating the control.
What is the difference between setOnAction and selectedProperty?
setOnAction() responds to an action performed on the control. A listener on selectedProperty() observes changes to the selected value, including changes made programmatically.
How do I create a three-state JavaFX CheckBox?
Call setAllowIndeterminate(true). You can inspect the third state with isIndeterminate() or assign it with setIndeterminate(true).
Should I use CheckBox or RadioButton in JavaFX?
Use CheckBox controls when options can be selected independently. Use RadioButton controls in a ToggleGroup when only one option should be selected.
JavaFX CheckBox Tutorial Summary
In this JavaFX Tutorial, we learned how to create a JavaFX CheckBox, read and change its selection, handle actions, observe its selected property, bind it to another control, use the indeterminate state, and process multiple independent options.
TutorialKart.com