How to create a check box using JavaFX?

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). Unlike radio buttons, you cannot combine checkboxes using Toggle Button.

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

In JavaFX, a checkbox can be −

  • Checked − The box will be with a tick mark indicating that it is selected. In this state the value of a selected property is true.

  • Unchecked − The box will be without a tick mark indicating that it is not selected. In this state the value of a selected property is false.

  • Undefined − This is an optional state you have this state by setting the allowIndeterminate property value to true. In this state (only) the indeterminate is true indicating the value of the checkbox is undefined.

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 CheckBoxExample 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");
      CheckBox checkBox4 = new CheckBox("Tamil");
      Label label = new Label("Select known languages:");
      Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
      label.setFont(font);
      //Setting layout
      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

raja
Published on 16-May-2020 07:43:37
Advertisements