×

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 Transformation

In the JavaFX application, in order to apply various transformation effects, we use Transform class. It has the ability to create the transformation on the given object. This transformation changes...

3 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 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 Bubble Chart

In the JavaFX application, in order to create Bubble Chart, the BubbleChart class is used. The Bubble Chart allows us to plot three dimensional data. All methods needed for this...

4 minutes read.

JavaFX Sphere

In the JavaFX application, in order to draw a Sphere, the Sphere class is used. The Sphere allows us to form a 3D shape having circular faces. All methods needed...

4 minutes read.

JavaFX HBox

In the JavaFX application, in order to set HBox as a layout, the HBox class is used. The HBox layout allows us to arrange the components in one row. All...

3 minutes read.

JavaFX Sequential Transition

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

4 minutes read.

JavaFX Image Input

In the JavaFX application, in order to apply various image input effect, we use the ImageInput class. It has the ability to fill the specified shape with a given image...

4 minutes read.

JavaFX Rotation

In the JavaFX application, in order to apply various rotation effects, we use this class. It has the ability to rotate the given object. It rotates the given node along...

3 minutes read.

JavaFX PasswordField

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

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 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 StackPane

In the JavaFX application, in order to set Stack Pane as a layout, the StackPane class is used. The StackPane layout allows us to arrange the components in the stack...

4 minutes read.

JavaFX ID selector

In the JavaFX application, in order to apply various css effect using the id selector, we use the various css classes available in the JavaFX. This is generally used to...

4 minutes read.

JavaFX Line Chart

In the JavaFX application, in order to create a Line Chart, LineChart class is used. The LineChart allows us to plot markers point according to the entities on number axes....

4 minutes read.

JavaFX ProgressBar

In the JavaFX application, in order to show the progress of work ProgressBar is used. It shows how much work is done. In order to use ProgressBar in JavaFX, javafx.scene.control.ProgressBar...

4 minutes read.

JavaFX Light Distant

In the JavaFX application, in order to apply various Light Distant effect from a single source, we use this class. It has the ability to apply light from a distant...

4 minutes read.

JavaFX Area Chart

In the JavaFX application, in order to create Area Chart, the AreaChart class is used. The Area Chart allows us to plot data along the XY plane. All methods needed...

3 minutes read.

JavaFX Scale Transition

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

4 minutes read.

JavaFX Light Spot

In the JavaFX application, in order to apply various Light Spot effect from a single source, we use this class. It has the ability to apply light from all the...

4 minutes read.