JavaFX example to set action listeners to a CheckBox

JavaFXObject Oriented ProgrammingProgramming

A checkbox is a type of selection control, which is square in shape with a tick mark int it, It has three states checked or, unchecked and, tristate/indeterminate (optional).

In JavaFX a checkbox is represented by the javafx.scene.control.CheckBox class. This class has three boolean properties namely allowIndeterminate, indeterminate, and, selected.

Setting Action to the CheckBox

The selected property of the CheckBox class specifies whether the current checkbox is selected. Its value will be true if checked and false if unchecked.

The selectedProperty() method returns a boolean property indicating whether the current checkbox is checked. If you want to perform certain actions in case a checkbox is selected. You can add a listener to this property as −

checkBox2.selectedProperty().addListener(
   //. . . . .
);

Example

Following JavaFX example creates three checkboxes and performs some action when each checkbox is checked.

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.scene.Group;
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.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class CheckBoxAction extends Application {
   public void start(Stage stage) {
      //Creating the check boxes
      CheckBox checkBox1 = new CheckBox("English");
      CheckBox checkBox2 = new CheckBox("Hindi");
      CheckBox checkBox3 = new CheckBox("Telugu");
      Label label = new Label("Select a language:");
      Font font = Font.font("verdana", 12);
      label.setFont(font);
      Text text1 = new Text(250.0, 45.0, "");
      text1.setFont(font);
      Text text2 = new Text(250.0, 75.0, "");
      text1.setFont(font);
      Text text3 = new Text(250.0, 105.0, "");
      text1.setFont(font);
      //Setting action to check boxes
      checkBox1.selectedProperty().addListener(
      (ObservableValue<? extends Boolean> ov, Boolean old_val, Boolean new_val) -> {
         text1.setText("Welcome to Tutorilaspoint");
      });
      checkBox2.selectedProperty().addListener(
      ObservableValue<? extends Boolean> ov, Boolean old_val, Boolean new_val) -> {
         text2.setText("टुटोरियल्स पॉइन्ट को आप का स्वागत है");
      });
      checkBox3.selectedProperty().addListener(
      (ObservableValue<? extends Boolean> ov, Boolean old_val, Boolean new_val) -> {
         text3.setText("ట్యూ టోరియల్స్ పాయింట్ కి స్వా గతిం");
      });
      //Adding the toggle button to the pane
      VBox vBox = new VBox(5);
      vBox.setPadding(new Insets(5, 5, 5, 50));
      vBox.getChildren().addAll(label, checkBox1, checkBox2, checkBox3 );
      Group root = new Group();
      root.getChildren().addAll(vBox, text1, text2, text3);
      //Setting the stage
      Scene scene = new Scene(root, 595, 150, Color.BEIGE);
      stage.setTitle("Check Box Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

raja
Published on 16-May-2020 11:22:56
Advertisements