JavaFX | Checkbox

CheckBox is a part of JavaFX package. CheckBox is a box with a tick on it when selected and empty when not selected. At first, checkboxes may seem similar to radio buttons, but there exists the difference between them that checkboxes cannot be combined into toggle groups, which means we cannot select multiple options at the same time.
States of CheckBox:
Constructor of the class are:
- CheckBox() : Creates a check box with an empty string for its label.
- CheckBox(String t) : Creates a check box with the given text as its label.
Commonly used methods:
| method | explanation |
|---|---|
| isIndeterminate() | Gets the value of the property indeterminate. |
| isSelected() | Gets the value of the property selected. |
| selectedProperty() | Indicates whether this CheckBox is checked. |
| setIndeterminate(boolean v) | Sets the value of the property indeterminate. |
| setSelected(boolean v) | Sets the value of the property selected. |
Below programs illustrate the use of CheckBox in JavaFX package:
- Program to create checkbox and add it to stage: This program creates a multiple CheckBox indicated by the name c. The CheckBox will be created inside a scene, which in turn will be hosted inside a stage. The indeterminate state of the checkbox would be initially set to true using the setIndeterminate() function. The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the CheckBox and the label inside the scene. Finally the show() method is called to display the final results.
importjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.collections.*;importjavafx.stage.Stage;publicclassCheckbox_1extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage s)   Â{       Â// set title for the stage       Âs.setTitle("creating CheckBox");       Â// create a tile pane       ÂTilePane r =newTilePane();       Â// create a label       ÂLabel l =newLabel("This is a check box");       Â// string array       ÂString st[] = {"Arnab","Andrew","Ankit"};       Â// add label       Âr.getChildren().add(l);       Âfor(inti =0; i < st.length; i++) {           Â// create a checkbox           ÂCheckBox c =newCheckBox(st[i]);           Â// add label           Âr.getChildren().add(c);           Â// set IndeterMinate           Âc.setIndeterminate(true);       Â}       Â// create a scene       ÂScene sc =newScene(r,150,200);       Â// set the scene       Âs.setScene(sc);       Âs.show();   Â}   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(args);   Â}}Output:
- Java Program to create check box and add event handler to it: This program creates a multiple CheckBox indicated by the name c. An Event handler will be created to handle the events ( toggle the label associated with textbox to depict the state of checkbox). The event would be set to the checkbox using setOnAction() function. The CheckBox will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the CheckBox and the label inside the scene. Finally, the show() method is called to display the final results.
importjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.collections.*;importjavafx.stage.Stage;publicclassCheckbox_2extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage s)   Â{       Â// set title for the stage       Âs.setTitle("creating CheckBox");       Â// create a tile pane       ÂTilePane r =newTilePane();       Â// create a label       ÂLabel l =newLabel("This is a check box");       Â// string array       ÂString st[] = {"Arnab","Andrew","Ankit"};       Â// add label       Âr.getChildren().add(l);       Âfor(inti =0; i < st.length; i++) {           Â// create a checkbox           ÂCheckBox c =newCheckBox(st[i]);           Â// add checkbox           Âr.getChildren().add(c);           ÂLabel l1 =newLabel(st[i] +" not selected");           Â// create a string           ÂString s1 = st[i];           Â// create a event handler           ÂEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {               Âpublicvoidhandle(ActionEvent e)               Â{                   Âif(c.isSelected())                       Âl1.setText(s1 +" selected ");                   Âelse                       Âl1.setText(s1 +" not selected ");               Â}           Â};           Â// set event to checkbox           Âc.setOnAction(event);           Â// add label           Âr.getChildren().add(l1);       Â}       Â// create a scene       ÂScene sc =newScene(r,150,200);       Â// set the scene       Âs.setScene(sc);       Âs.show();   Â}   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(args);   Â}}Output:
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/CheckBox.html




