JavaFX Tutorial Index

JavaFX Tutorial JavaFX Installation JavaFX Application Structure JavaFX Architecture

JavaFX Applications Charts

JavaFX Bar Chart JavaFX Bubble Chart JavaFX Pie Chart JavaFX Scatter Chart JavaFX Area Chart JavaFX Line Chart

JavaFX 2D Shapes

JavaFX Arc JavaFX Circle JavaFX Ellipse JavaFX Line JavaFX Polygon JavaFX Rectangle JavaFX Color JavaFX Gradient Color

JavaFX 3D Shapes

JavaFX Box JavaFX Cylinder JavaFX Sphere

JavaFX Animations

JavaFX Rotate Transition JavaFX Translate Transition JavaFX Fade Transition JavaFX Fill Transition JavaFX Parallel Transition JavaFX Path Transition JavaFX Pause Transition JavaFX Scale Transition JavaFX Sequential Transition JavaFX Stroke Transition

JavaFX CSS

JavaFX ID Selector JavaFX Inline Styles JavaFX Selectors

JavaFX Effect

JavaFX Blend JavaFX Bloom JavaFX Color Adjust JavaFX Color Input JavaFX Drop Shadow JavaFX Gaussian Blur JavaFX Glow JavaFX Image Input JavaFX Inner Shadow JavaFX Light Distant JavaFX Light Point JavaFX Light Spot JavaFX Lighting JavaFX Motion Blur JavaFX Reflection JavaFX Shadow

JavaFX Layouts

JavaFX Layouts JavaFX BorderPane JavaFX GridPane JavaFX StackPane JavaFX HBox JavaFX TilePane

JavaFX Event Handling

JavaFX Event Handling JavaFX Event Filters JavaFX Convenience Methods

JavaFX Transformation

JavaFX Transformation JavaFX Scaling JavaFX Rotation JavaFX Translation JavaFX Shear

JavaFX UI

JavaFX Menu JavaFX Button JavaFX Button Styling JavaFX CheckBox JavaFX File Chooser JavaFX HyperLink JavaFX Label JavaFX UI Controls JavaFX PasswordField JavaFX ProgressBar JavaFX RadioButton JavaFX ScrollBar JavaFX Slider JavaFX TextField

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