Kensoft PH
  • Download
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
  • Download
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
No Result
View All Result
Home Java

How to use CheckBox in JavaFX | 100% Perfect For Beginners

July 23, 2022 - Updated on December 7, 2022
in Java
Reading Time: 4 mins read
0
CheckBox In JavaFX Thumbnail
124
VIEWS
Share on FacebookShare on TwitterShare via Email

Contents

  • How to use CheckBox in JavaFX
    • Create the JavaFX CheckBox
    • Add the JavaFX CheckBox to the Scene Graph
      • Output
    • Setting Text in JavaFX CheckBox
      • Output
    • Determining CheckBox State
    • Setting Style or CSS
      • Output
      • YouTube Video
    • Set Graphic or Icon JavaFX CheckBox Example
  • YouTube Video

How to use CheckBox in JavaFX

The checkBox in JavaFX is one of the most common controls used in JavaFX applications. They allow users to select multiple items from a list of choices. JavaFX CheckBox is a graphical control or node that allows the user to select multiple options, and it has three states: unchecked, checked, and undefined. The undefined state is also known as an indeterminate state. I will show you what that looks like later.

In this tutorial, you will learn how to create a simple JavaFX CheckBox control. A checkBox in JavaFX is part of the choice buttons, and it is very simple to create and understand how it works. It works like the JavaFX ToggleButton and JavaFX RadioButton. The following example will walk you through creating a checkbox in JavaFX, and many more examples to follow.

Create the JavaFX CheckBox

It is very easy to create the JavaFX CheckBox using its constructor. You can initially assign a name to the JavaFX CheckBox. See the following code snippet below to learn more.

// Creating the JavaFX CheckBox
CheckBox cb = new CheckBox("This is a CheckBox");

The text in the checkbox constructor is the name or will be displayed in the JavaFX CheckBox control.

Add the JavaFX CheckBox to the Scene Graph

You must use a JavaFX Layout to display the CheckBox in your application. Layout in JavaFX is a container for the nodes, and adding the layout in your JavaFX application is very easy, and you also need to use its constructor to create it. The following code snippet below shows you how to add or display your JavaFX CheckBox to the Scene graph.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.io.IOException;

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        // Create the layout
        StackPane root = new StackPane();
        // Create the CheckBox and add to scene graph
        CheckBox cb = new CheckBox("This is a CheckBox");
        root.getChildren().add(cb);
        Scene scene = new Scene(root, 400, 400);
        stage.setScene(scene);
        stage.setTitle("Understanding the CheckBox");
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

Output

JavaFX CheckBox

Setting Text in JavaFX CheckBox

To set the name or text in the checkbox is really simple to do, you need to use the setText() method to change the displayed name or text in the JavaFX CheckBox. Changing the text in your checkbox depends on your application requirements. You can change the text anytime, but it depends on the requirements. Proceed to the following code snippet below to learn more about setting the text in your JavaFX CheckBox.

CheckBox cb = new CheckBox("This is a CheckBox");
cb.setText("CheckBox");

Output

CheckBox in JavaFX

Determining CheckBox State

There are three types of states in the JavaFX CheckBox. As mentioned above, these are the unselected, selected, and indeterminate states. The states mean that the unselected and selected are not selected and selected. Otherwise, the indeterminate state means that it is neither selected nor not selected. Please proceed to the following code snippet below to learn more about how to identify the states in CheckBox.

CheckBox cb = new CheckBox("CheckBox");
Label state = new Label("State");
cb.setAllowIndeterminate(true);
cb.setIndeterminate(true);
if(cb.isIndeterminate()){
    state.setText("State: UNDEFINED");
}else if(cb.isSelected()){
    state.setText("State: CHECKED");
}else{
    state.setText("State: UNCHECKED");
}

Setting Style or CSS

You can add CSS to your JavaFX application. Adding CSS to an application makes it more beautiful and has a higher chance to be loved by your users. There are two different ways to add a style to your JavaFX application: using inline code or creating an external CSS file. Use the setStyle() method if you want to add an inline-code for setting the style. If you want to learn more about how to create an external CSS from your JavaFX application, then watch the YouTube video below.

The following code snippet below will teach you how to use the setStyle() method to set the style in your application.

CheckBox cb = new CheckBox("CheckBox");
cb.setStyle("-fx-background-color: black; -fx-text-fill: white; -fx-font-size: 30; -fx-font-family: 'Agency FB'");

Output

CheckBox in JavaFX Style

YouTube Video

Set Graphic or Icon JavaFX CheckBox Example

You will learn how to set graphic or icon in your CheckBox. You need to use the setGraphic() method to set the icon or graphic in your JavaFX CheckBox. See the following code snippet below to learn more about how to add an icon to your JavaFX nodes.

// adding a graphic outside from your project
ImageView iv = new ImageView(String.valueOf(new File("C:\\Users\\Kensoft\\Desktop\\Checkbox-in-javafx-style.jpg")));
cb.setGraphic(iv);

// adding a graphic from your project resources
ImageView iv = new ImageView("/res/Checkbox-in-javafx-style.jpg");
cb.setGraphic(iv);

YouTube Video

Previous Post

RadioButton in JavaFX | 100% Perfect for beginners

Next Post

How to install IntelliJ IDEA on Windows 10 and 11 | Perfect

KENSOFT

KENSOFT

What’s up! Kent is my name. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java

Related tutorials

JavaFX DirectoryChooser: 100% Perfect Step-By-Step Guide
Java

JavaFX DirectoryChooser: 100% Perfect Step-By-Step Guide

March 26, 2023
2
File Chooser in JavaFX Tutorial
Java

File Chooser in JavaFX: 100% Perfect Step-by-Step Guide

March 4, 2023
18
JavaFX TabPane Tutorial
Java

How to use the TabPane in JavaFX | 100% Perfect Tutorial

March 2, 2023
11
Next Post
How to install IntelliJ IDEA on Windows 10

How to install IntelliJ IDEA on Windows 10 and 11 | Perfect

How to use the ChoiceBox in JavaFX | 100% Perfect tutorial

How to use the ChoiceBox in JavaFX | 100% Perfect tutorial

JavaFX ComboBOx

How to use the ComboBox in JavaFX | 100% Perfect Tutorial

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Maximum upload file size: 20 MB. You can upload: image, audio, video, document, text. Drop files here

  • Trending
  • Comments
  • Latest
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

April 11, 2021 - Updated on July 3, 2022
MySQL database using XAMPP

How to connect Java to MySQL database using Xampp server | 100% best for beginners

October 27, 2020 - Updated on January 23, 2023
JavaFX 17

How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

November 15, 2021 - Updated on December 13, 2021
change the stage icon in JavaFX

How to change the stage icon in JavaFX | 100% best for beginners

May 23, 2021 - Updated on September 24, 2022
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

send SMS in java

How to send SMS in Java | 100% best example

3DES in Java and AES in Java

How to use AES and 3DES in Java | 100% best for beginners

JavaFX Splash Screen

How to create JavaFX Splash Screen | 100% best for beginners

JavaFX DirectoryChooser: 100% Perfect Step-By-Step Guide

JavaFX DirectoryChooser: 100% Perfect Step-By-Step Guide

March 26, 2023
File Chooser in JavaFX Tutorial

File Chooser in JavaFX: 100% Perfect Step-by-Step Guide

March 4, 2023
JavaFX TabPane Tutorial

How to use the TabPane in JavaFX | 100% Perfect Tutorial

March 2, 2023
How to use the JavaFX ToolBar

How to use the JavaFX ToolBar | 100% Perfect Tutorial

January 24, 2023

Latest Tutorials

JavaFX DirectoryChooser: 100% Perfect Step-By-Step Guide

JavaFX DirectoryChooser: 100% Perfect Step-By-Step Guide

March 26, 2023
File Chooser in JavaFX Tutorial

File Chooser in JavaFX: 100% Perfect Step-by-Step Guide

March 4, 2023
JavaFX TabPane Tutorial

How to use the TabPane in JavaFX | 100% Perfect Tutorial

March 2, 2023

Popular Tutorials

  • Failed to automatically set up a JavaFX Platform

    Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

    0 shares
    Share 0 Tweet 0
  • How to connect Java to MySQL database using Xampp server | 100% best for beginners

    0 shares
    Share 0 Tweet 0
  • How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

    0 shares
    Share 0 Tweet 0
Facebook Instagram Youtube Github LinkedIn Discord
Kensoft PH

What’s up! Kent is my name. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java, which I use to create computer applications. In a company, I created applications and a website.

Website

Privacy Policy

Terms and Condition

Sitemap

Services

Guest Post

Fiverr

Freelancer

Upwork

Categories

Website Status

Check the status

Latest Tutorials

JavaFX DirectoryChooser: 100% Perfect Step-By-Step Guide

JavaFX DirectoryChooser: 100% Perfect Step-By-Step Guide

March 26, 2023
File Chooser in JavaFX Tutorial

File Chooser in JavaFX: 100% Perfect Step-by-Step Guide

March 4, 2023
JavaFX TabPane Tutorial

How to use the TabPane in JavaFX | 100% Perfect Tutorial

March 2, 2023

© 2023 Made With Love By KENSOFT PH

No Result
View All Result
  • Download
  • Java Quiz
  • Contact
  • About

© 2023 Made With Love By KENSOFT PH

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.