×

JavaFX CheckBox

In the JavaFX application, to get the user information in the form of choices from the user, a form is created using checkboxes that allow the user to select two or more choices for specific fields. In JavaFX application, javafx.scene.control.CheckBox class is used to include checkboxes in an application.

JavaFX CheckBox:

Example:

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;  


  
public class CheckboxUI extends Application {  
  
    @Override  
    public void start(Stage primaryStage) throws Exception {  
    Label l = new Label(" Choose the languages you know : ");  
        CheckBox newc1 = new CheckBox(" English ");  
        CheckBox newc2 = new CheckBox(" Hindi ");  
        CheckBox newc3 = new CheckBox(" Marathi ");  
        CheckBox newc4 = new CheckBox(" Spanish ");  
        HBox root = new HBox();  
        root.getChildren().addAll(l,newc1,newc2,newc3,newc4);  
        root.setSpacing(5);  
        Scene scene=new Scene(root,600,400);  
        primaryStage.setScene(scene);  
        primaryStage.setTitle(" JavaFX CheckBox Example");  
        primaryStage.show();      }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the checkbox in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.Scene, javafx.stage.Stage, javafx.scene.control.CheckBox, javafx.scene.control.Label, javafx.scene.layout.HBox;

Then we have created one class named CheckboxUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold the button with Checkbox and Labels, an HBox object is created which is then passed to the Scene class object.

Label for Entering languages you know is created using the constructor with label text. Also, checkboxes are created for various languages which are then added to HBox. The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, "JavaFX CheckBox Example". Also, it displays checkboxes for various languages like English, Marathi, Hindi, and Spanish. We can select two or more languages.

JavaFX CheckBox

Retrieving the data in the CheckBoxes:

In the JavaFX application, all the data entered in the checkboxes can be retrieved using the isSelected() method.

Example:

import javafx.application.Application;  
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;


import javafx.scene.layout.HBox;
import javafx.stage.Stage;  


  
public class CheckboxUI extends Application 
{  
static  String items= "";


    @Override  
    public void start(Stage primaryStage) throws Exception 
   {  
       
       Label l = new Label (" Choose the languages you know : ");  
        CheckBox newc1 = new CheckBox (" English ");  
        CheckBox newc2 = new CheckBox (" Hindi ");  
        CheckBox newc3 = new CheckBox (" Marathi ");  
        CheckBox newc4 = new CheckBox (" Spanish ");  
        Button button= new Button (" Submit ");


        button.setOnAction(e -> 
        {
          
        if (newc1.isSelected())
        {
        items += " English\n ";
        }
           
        if (newc2.isSelected())
        {
        items += " Hindi\n ";
        }
            
        if (newc3.isSelected())
        {
        items += " Marathi\n ";
        }
             
        if (newc4.isSelected())
        {
        items += " Spanish\n ";
        }  
        System.out.println(" Languages known: "+items);


        }
        );
        
        HBox root = new HBox();  
        root.getChildren().addAll(l,newc1,newc2,newc3,newc4,button);  
       
        root.setSpacing(5);  
        Scene scene=new Scene(root,600,400);  
        primaryStage.setScene(scene);  
        primaryStage.setTitle(" JavaFX CheckBox Example");  
        primaryStage.show();     
        }


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

Output:

In order to create the checkbox in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.Scene, javafx.stage.Stage, javafx.scene.control.CheckBox, javafx.scene.control.Label, javafx.scene.control.Button, javafx.scene.layout.HBox.

Then we have created one class named CheckboxUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold the button with Checkbox and Labels, an HBox object is created which is then passed to the Scene class object.

Label for Entering languages you know is created using the constructor with label text. Also, checkboxes are created for various languages which are then added to HBox. One submit button is created. On clicking on the button it will display all languages known. Using the isSelected() method it is checked whether the particular checkbox is clicked or not and accordingly content is displayed. The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, "JavaFX CheckBox Example". Also, it displays checkboxes for various languages like English, Marathi, Hindi, and Spanish. We can select two or more languages. When we select English, Marathi, Hindi and click on the submit button, it will display output in console as Language known: English, Hindi, Marathi.

JavaFX CheckBox

Related Topics

JavaFX Fill Transition

In the JavaFX application, in order to generate the fill transition, we have to apply fill transition on any particular shape. It has the ability to fill the given shape...

4 minutes read.

JavaFX Pause Transition

In the JavaFX application, in order to generate the pause transition, we have to apply pause transition on any particular shape. It has the ability to take a particular pause...

4 minutes read.

JavaFX Menu

In the JavaFX application, in order to create a menu, menu items, and menu bar, Menu, MenuItem, and MenuBar class is used. The menu allows us to choose options among...

4 minutes read.

JavaFX Rectangle

In the JavaFX application, in order to draw a rectangle, the Rectangle class is used. The Rectangle allows us to join any four points with x and y coordinates in...

4 minutes read.

JavaFX - Basic Structure of Application

Every JavaFX application is mainly divided into three main components namely Scene, Stage, and nodes. For using these components we need to import javafx.application.Application class in every javafx application. Life...

4 minutes read.

JavaFX Parallel Transition

In the JavaFX application, in order to generate the parallel transition, we have to apply parallel transition on any particular shape. It has the ability to execute multiple transactions parallelly....

4 minutes read.

JavaFX File Chooser

In the JavaFX application, in order to open or save the file, FileChooser is used. It allows user to choose the file from system location and save it in a...

4 minutes read.

JavaFX Blend

In the JavaFX application, in order to apply various blend effect, we use Blend class. It has the ability to blend the colors of the shape. All methods needed for...

4 minutes read.

JavaFX Translate Transition

In the JavaFX application, in order to generate the translate transition, we have to apply translate transition on any particular shape. It has the ability to translate the shape along...

3 minutes read.

JavaFX Button Styling

 In the JavaFX application, we can apply CSS styling to buttons in various ways. We can change the size of the button as well as border colour, text colour can...

3 minutes read.

JavaFX Shadow

In the JavaFX application, in order to apply various Shadow effect from a single source, we use this class. It has the ability to create the shadow of the given...

3 minutes read.

JavaFX Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the colors in these shapes using the setFill()...

6 minutes read.

JavaFX Event Handling

JavaFX gives way to handle various events in web application, desktop applications and graphical applications. In the JavaFX application, in order to allow user to interact with application various events...

4 minutes read.

JavaFX Line

In the JavaFX application, in order to draw a line, the Line class is used. The line allows us to join any two points with x and y coordinates in...

3 minutes read.

JavaFX Tutorial

Introduction of JavaFX: The most popular applications called Rich Internet Application are build using the JavaFX. This application can run across many platforms. Various devices such as mobile, computers, and tablets...

4 minutes read.

JavaFX TextField

In the JavaFX application, to get the user information as input from the user, a form is created using textfields which allows the user to enter input for specific fields....

4 minutes read.

JavaFX Pie Chart

In the JavaFX application, in order to create Pie Chart, the PieChart class is used. The Pie Chart allows us to plot data along with the sectors of the circle....

3 minutes read.

JavaFX Slider

In the JavaFX application, in order to slide over a range of data having values, Slider is used. It provides a pane with a Slider to move over a range...

4 minutes read.

JavaFX Stroke Transition

In the JavaFX application, in order to generate the stroke transition, we have to apply stroke transition on any particular shape. It has the ability to give different color strokes...

4 minutes read.

JavaFX Path Transition

In the JavaFX application, in order to generate the path transition, we have to apply path transition on any particular shape. It has the ability to traverse the given shape...

3 minutes read.