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 applicationpublicvoidstart(Stage s){// set title for the stages.setTitle("creating CheckBox");// create a tile paneTilePane r =newTilePane();// create a labelLabel l =newLabel("This is a check box");// string arrayString st[] = {"Arnab","Andrew","Ankit"};// add labelr.getChildren().add(l);for(inti =0; i < st.length; i++) {// create a checkboxCheckBox c =newCheckBox(st[i]);// add labelr.getChildren().add(c);// set IndeterMinatec.setIndeterminate(true);}// create a sceneScene sc =newScene(r,150,200);// set the scenes.setScene(sc);s.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(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 applicationpublicvoidstart(Stage s){// set title for the stages.setTitle("creating CheckBox");// create a tile paneTilePane r =newTilePane();// create a labelLabel l =newLabel("This is a check box");// string arrayString st[] = {"Arnab","Andrew","Ankit"};// add labelr.getChildren().add(l);for(inti =0; i < st.length; i++) {// create a checkboxCheckBox c =newCheckBox(st[i]);// add checkboxr.getChildren().add(c);Label l1 =newLabel(st[i] +" not selected");// create a stringString s1 = st[i];// create a event handlerEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){if(c.isSelected())l1.setText(s1 +" selected ");elsel1.setText(s1 +" not selected ");}};// set event to checkboxc.setOnAction(event);// add labelr.getChildren().add(l1);}// create a sceneScene sc =newScene(r,150,200);// set the scenes.setScene(sc);s.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:
 
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/CheckBox.html
				
					



