
- JavaFX - Environment
- JavaFX - Installation Using Netbeans
- JavaFX - Installation Using Eclipse
- JavaFX - Installation using Visual Studio Code
- JavaFX - Architecture
- JavaFX - Application
- JavaFX 2D Shapes
- JavaFX - 2D Shapes
- JavaFX - Drawing a Line
- JavaFX - Drawing a Rectangle
- JavaFX - Drawing a Rounded Rectangle
- JavaFX - Drawing a Circle
- JavaFX - Drawing an Ellipse
- JavaFX - Drawing a Polygon
- JavaFX - Drawing a Polyline
- JavaFX - Drawing a Cubic Curve
- JavaFX - Drawing a Quad Curve
- JavaFX - Drawing an Arc
- JavaFX - Drawing an SVGPath
- JavaFX Properties of 2D Objects
- JavaFX - Stroke Type Property
- JavaFX - Stroke Width Property
- JavaFX - Stroke Fill Property
- JavaFX - Stroke Property
- JavaFX - Stroke Line Join Property
- JavaFX - Stroke Miter Limit Property
- JavaFX - Stroke Line Cap Property
- JavaFX - Smooth Property
- Operations on 2D Objects
- JavaFX - 2D Shapes Operations
- JavaFX - Union Operation
- JavaFX - Intersection Operation
- JavaFX - Subtraction Operation
- JavaFX Path Objects
- JavaFX - Path Objects
- JavaFX - LineTo Path Object
- JavaFX - HLineTo Path Object
- JavaFX - VLineTo Path Object
- JavaFX - QuadCurveTo Path Object
- JavaFX - CubicCurveTo Path Object
- JavaFX - ArcTo Path Object
- JavaFX Color and Texture
- JavaFX - Colors
- JavaFX - Linear Gradient Pattern
- JavaFX - Radial Gradient Pattern
- JavaFX Text
- JavaFX - Text
- JavaFX Effects
- JavaFX - Effects
- JavaFX - Color Adjust Effect
- JavaFX - Color input Effect
- JavaFX - Image Input Effect
- JavaFX - Blend Effect
- JavaFX - Bloom Effect
- JavaFX - Glow Effect
- JavaFX - Box Blur Effect
- JavaFX - GaussianBlur Effect
- JavaFX - MotionBlur Effect
- JavaFX - Reflection Effect
- JavaFX - SepiaTone Effect
- JavaFX - Shadow Effect
- JavaFX - DropShadow Effect
- JavaFX - InnerShadow Effect
- JavaFX - Lighting Effect
- JavaFX - Light.Distant Effect
- JavaFX - Light.Spot Effect
- JavaFX - Point.Spot Effect
- JavaFX - DisplacementMap
- JavaFX - PerspectiveTransform
- JavaFX Transformations
- JavaFX - Transformations
- JavaFX - Rotation Transformation
- JavaFX - Scaling Transformation
- JavaFX - Translation Transformation
- JavaFX - Shearing Transformation
- JavaFX Animations
- JavaFX - Animations
- JavaFX - Rotate Transition
- JavaFX - Scale Transition
- JavaFX - Translate Transition
- JavaFX - Fade Transition
- JavaFX - Fill Transition
- JavaFX - Stroke Transition
- JavaFX - Sequential Transition
- JavaFX - Parallel Transition
- JavaFX - Pause Transition
- JavaFX - Path Transition
- JavaFX Images
- JavaFX - Images
- JavaFX 3D Shapes
- JavaFX - 3D Shapes
- JavaFX - Creating a Box
- JavaFX - Creating a Cylinder
- JavaFX - Creating a Sphere
- Properties of 3D Objects
- JavaFX - Cull Face Property
- JavaFX - Drawing Modes Property
- JavaFX - Material Property
- JavaFX Event Handling
- JavaFX - Event Handling
- JavaFX - Using Convenience Methods
- JavaFX - Event Filters
- JavaFX - Event Handlers
- JavaFX UI Controls
- JavaFX - UI Controls
- JavaFX - ListView
- JavaFX - Accordion
- JavaFX - ButtonBar
- JavaFX - ChoiceBox
- JavaFX - HTMLEditor
- JavaFX - MenuBar
- JavaFX - Pagination
- JavaFX - ProgressIndicator
- JavaFX - ScrollPane
- JavaFX - Separator
- JavaFX - Slider
- JavaFX - Spinner
- JavaFX - SplitPane
- JavaFX - TableView
- JavaFX - TabPane
- JavaFX - ToolBar
- JavaFX - TreeView
- JavaFX - Label
- JavaFX - CheckBox
- JavaFX - RadioButton
- JavaFX - TextField
- JavaFX - PasswordField
- JavaFX - FileChooser
- JavaFX - Hyperlink
- JavaFX - Tooltip
- JavaFX - Alert
- JavaFX - DatePicker
- JavaFX - TextArea
- JavaFX Charts
- JavaFX - Charts
- JavaFX - Creating Pie Chart
- JavaFX - Creating Line Chart
- JavaFX - Creating Area Chart
- JavaFX - Creating Bar Chart
- JavaFX - Creating Bubble Chart
- JavaFX - Creating Scatter Chart
- JavaFX - Creating Stacked Area Chart
- JavaFX - Creating Stacked Bar Chart
- JavaFX Layout Panes
- JavaFX - Layout Panes
- JavaFX - HBox Layout
- JavaFX - VBox Layout
- JavaFX - BorderPane Layout
- JavaFX - StackPane Layout
- JavaFX - TextFlow Layout
- JavaFX - AnchorPane Layout
- JavaFX - TilePane Layout
- JavaFX - GridPane Layout
- JavaFX - FlowPane Layout
- JavaFX CSS
- JavaFX - CSS
- Media with JavaFX
- JavaFX - Handling Media
- JavaFX - Playing Video
- JavaFX Useful Resources
- JavaFX - Quick Guide
- JavaFX - Useful Resources
- JavaFX - Discussion
JavaFX - CheckBox
Checkbox is a user interface component that allows the user to select or deselect an option. It is most commonly used to create multiple-choice questions, preferences, filters, and many more. The figure below shows a filter feature with multiple options, allowing users to choose a category and brand according to their preferences.

Creating CheckBox in JavaFX
In JavaFX, the checkbox is represented by a class named CheckBox. This class belongs to the javafx.scene.control package. By instantiating this class, we can create a checkbox in JavaFX. Constructors of the CheckBox class are listed below −
CheckBox() − It is the default constructor that constructs a CheckBox without any option name.
CheckBox(String str) − It constructs a new CheckBox with the specified option.
The most commonly used constructor of the CheckBox class is its parameterized constructor. It accepts a text representing the option name of the CheckBox. Once the checkbox is created, define a layout pane, such as Vbox or Hbox by passing the CheckBox object to its constructor. Then, create a Scene and pass the object of layout pane as a parameter value to its constructor. Next, set the stage and title of the JavaFX application. Finally, call the main() method to launch the application.
Example
Following is the program that will create the CheckBox using JavaFX. Save this code in a file with the name CheckBoxDemo.java.
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.CheckBox; import javafx.scene.control.Label; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.geometry.Pos; import javafx.geometry.Insets; public class CheckBoxDemo extends Application { @Override public void start(Stage stage) throws Exception { // Creating a Label Label label = new Label("Click the box to select: "); // Creating three CheckBoxes CheckBox checkBx1 = new CheckBox("Item1"); checkBx1.setTextFill(Color.GREEN); checkBx1.setSelected(false); CheckBox checkBx2 = new CheckBox("Item2"); checkBx2.setTextFill(Color.BLUE); checkBx2.setSelected(false); CheckBox checkBx3 = new CheckBox("Item3"); checkBx3.setTextFill(Color.SKYBLUE); checkBx3.setSelected(false); // Create a Label to display the selection Label selectLabel = new Label(); selectLabel.setTextFill(Color.RED); // Adding listeners to the CheckBoxes checkBx1.setOnAction(e -> selectLabel.setText("You selected: " + (checkBx1.isSelected() ? "Item1" : "") )); checkBx2.setOnAction(e -> selectLabel.setText("You selected: " + (checkBx2.isSelected() ? "Item2" : "") )); checkBx3.setOnAction(e -> selectLabel.setText("You selected: " + (checkBx2.isSelected() ? "Item3" : "") )); // Create a VBox and add the CheckBoxes and Label VBox vbox = new VBox(); vbox.setAlignment(Pos.CENTER); vbox.setPadding(new Insets(10)); vbox.setSpacing(10); vbox.getChildren().addAll(label, checkBx1, checkBx2, checkBx3, selectLabel); // Create a scene and add the VBox Scene scene = new Scene(vbox, 400, 300); // Set the scene and show the stage stage.setScene(scene); stage.setTitle("CheckBox in JavaFX"); stage.show(); } public static void main(String[] args) { launch(args); } }
Compile and execute the saved Java file from the command prompt using the following commands −
javac --module-path %PATH_TO_FX% --add-modules javafx.controls CheckBoxDemo.java java --module-path %PATH_TO_FX% --add-modules javafx.controls CheckBoxDemo
Output
On executing, the above program generates a JavaFX window displaying three checkboxes as shown below −

Creating CheckBox using default constructor in JavaFX
As we discussed earlier, we can create CheckBox in JavaFX either by using its default constructor or its parameterized constructor. In the next example, we will use the default constructor and pass the option text with the help of a built-in method named setText(). Save this code in a file with the name JavafxCheckbox.java.
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.scene.paint.Color; import javafx.stage.Stage; import javafx.geometry.Pos; import javafx.geometry.Insets; public class JavafxCheckbox extends Application { @Override public void start(Stage stage) throws Exception { // Creating a Label Label label = new Label("Click the box to select: "); // Creating three CheckBoxes without label text CheckBox checkBx1 = new CheckBox(); checkBx1.setTextFill(Color.GREEN); checkBx1.setSelected(true); // adding lable to the check box1 checkBx1.setText("Item1"); CheckBox checkBx2 = new CheckBox(); // adding lable to the check box2 checkBx2.setText("Item2"); checkBx2.setTextFill(Color.BLUE); checkBx2.setSelected(false); // Create a Label to display the selection Label selectLabel = new Label(); selectLabel.setTextFill(Color.RED); // Adding listeners to the CheckBoxes checkBx1.setOnAction(e -> selectLabel.setText("You selected: " + (checkBx1.isSelected() ? "Item1" : "") )); checkBx2.setOnAction(e -> selectLabel.setText("You selected: " + (checkBx2.isSelected() ? "Item2" : "") )); // Create a HBox and add the CheckBoxes and Label HBox box = new HBox(); box.setAlignment(Pos.CENTER); box.setPadding(new Insets(10)); box.setSpacing(10); box.getChildren().addAll(label, checkBx1, checkBx2, selectLabel); // Create a scene and add the HBox Scene scene = new Scene(box, 400, 300); // Set the scene and show the stage stage.setScene(scene); stage.setTitle("CheckBox in JavaFX"); stage.show(); } public static void main(String[] args) { launch(args); } }
To compile and execute the saved Java file from the command prompt, use the following commands −
javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxCheckbox.java java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxCheckbox
Output
On executing the above program, it will generate a JavaFX window displaying two checkboxes as shown below −
