JavaFX – CheckBox


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.


JavaFX Checkbox

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 −


CheckBox Output

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 −


CheckBox Output2

Advertisements

”;

Leave a Reply

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