Donate HTML/CSS JavaFX Java Home
JavaFX Starter Template JavaFX Labels JavaFX Buttons JavaFX Toggle Buttons JavaFX Radio Buttons and Toggle Groups JavaFX Checkboxes JavaFX Choice Boxes JavaFX Combo Boxes JavaFX Text Areas JavaFX Text Fields JavaFX List Views JavaFX Image Views JavaFX Color Pickers JavaFX Date Pickers JavaFX Resizable Split Pane Layouts JavaFX VBox (Vertical Box Layouts) JavaFX HBox (Horizontal Box Layouts) JavaFX Flow Pane Layouts JavaFX Tile Pane Layouts JavaFX Web Views

JavaFX Checkboxes

Code Description:

The code provided below shows how to create a simple checkbox in JavaFX. Checkboxes have 3 possible states that they may exist in. They can be in a checked, unchecked, or indeterminate state. The runnable example code shows how the selected value can be found. If the checkbox is checked, we will display that it is using the text value. If not we will state that it is unchecked.

Checkbox Syntax:

CheckBox checkBox = new CheckBox("Checkbox Text");

Runnable JavaFX Checkbox Example Code:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Demo extends Application {

    public static void main(String[] args) {

    public void start(Stage primaryStage) throws Exception {
         VBox root = new VBox();
	CheckBox checkBox = new CheckBox("Click Me");
	checkBox.setOnAction(new EventHandler<ActionEvent>() {
	    public void handle(ActionEvent event) {
	        if (checkBox.isSelected()) {
		    checkBox.setText("I am checked");
		} else {
		    checkBox.setText("I am unchecked");
    primaryStage.setTitle("Checkbox Demo");
    primaryStage.setScene(new Scene(root, 200, 100));;


How to Run This Code:

Make sure that your Class name is the same as ours. In this case, we are using the Class name of "Demo". Alternatively, you can change the Class name in the provided code to match yours.

Note: You will NOT be able to copy and paste this code into your IDE unless you first set up JavaFX for your project. Check out this link if you need help setting up JavaFX:

JavaFX Comprehensive Setup Guide


JavaFX Checkbox Output Image

Important Methods:

  • isSelected() - Used to determine if the checkbox is selected or not.
  • setSelected() - Used to set the checkbox into a toggled state.
  • isIndeterminate() - Used to determine if the checkbox is considered neither selected or deselected.
  • setIndeterminate() - Used to set the checkbox to a state that is neither selected or deselected.
  • fire() - Used to programatically "press" a checkbox without any user interaction. Use this if you need to call the code in the event handler.
  • getText() - Used to get the text displayed on the side of the checkbox.
  • setDisable() - Used to make the checkbox grayed out and unclickable. EX: "checkbox.setDisable(true);"
  • setFont() - Set the font and font size for the checkbox text. EX: "checkbox.setFont(new Font("Courier New", 20));"
  • setPrefSize() - Set the preferred width and height for the checkbox.
  • setOnAction() - Set the code and event handler for when the checkbox is clicked or pressed.
  • setText() - Set the text displayed on the side of the checkbox.
  • setStyle() - Set an inline CSS-like style on the checkbox. EX: "checkbox.setStyle("-fx-background-color: green;");"
  • setTooltip() - Set a message that displays when a user hovers over the checkbox control for a certain amount of time. EX: "checkbox.setTooltip(new Tooltip("Press Me"));"
  • setVisible() - Show or hide a checkbox from the scene. EX: "checkbox.setVisible(false);"

Related Links:

JavaFX Buttons JavaFX Toggle Buttons JavaFX Radio Buttons and Toggle Groups