What is tri state checkBox, How to create a tri state checkBox in JavaFX?


A checkbox is a type of selection control, which is square in shape with a tick mark int it. Generally, a checkbox has two states checked and unchecked.

Depending on the GUI (technology) we can also have a third state named undefined/undetermined, which indicates the state of the current checkbox is neither checked nor unchecked.

JavaFX supports tristate checkboxes, the javafx.scene.control.CheckBox class represents a checkbox and it contains three boolean properties −

  • allowIndeterminate − This property specifies whether a checkbox should have all the three states. You can set the value to this property using the setAllowIndeterminate() method.

  • indeterminate − This property specifies whether a checkbox is in undefined states. You can set the value to this property using the setIndeterminate() method.

  • selected − This property specifies whether the current checkbox is selected. You can set the value to this property using the setSelected() method.

To toggle through all the three states −

  • Instantiate the CheckBox class.

  • Invoke the setAllowIndeterminate() method by passing the boolean value true as an argument.

  • Invoke the setIndeterminate() method by passing the boolean value true as an argument.

  • Add the created checkbox to the parent node.

Example

import javafx.application.Application;
import javafx.geometry.Insets;
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.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class CheckBox_Undefined_State extends Application {
   public void start(Stage stage) {
      //Creating the check boxes
      CheckBox checkBox1 = new CheckBox("Salary");
      CheckBox checkBox2 = new CheckBox("Over Time Allowence");
      CheckBox checkBox3 = new CheckBox("Bonus");
      CheckBox checkBox4 = new CheckBox("Night Shift Allowence");
      Label label = new Label("Select Recieved Allowences:");
      Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
      label.setFont(font);
      //Setting the indeterminate state true
      checkBox2.setAllowIndeterminate(true);
      checkBox2.setIndeterminate(true);
      checkBox4.setAllowIndeterminate(true);
      checkBox4.setIndeterminate(true);
      //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, checkBox4);
      //Setting the stage
      Scene scene = new Scene(vBox, 595, 150, Color.BEIGE);
      stage.setTitle("Check Box Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

 



Updated on: 16-May-2020

435 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements