- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaFX example to set action listeners to a CheckBox
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
- Related Articles
- Example to set action listeners (behavior) to a ChoiceBox in JavaFX
- How to add action listeners to ContextMenu in JavaFX?
- JavaFX example to set action to the "exit" MenuItem
- JavaFX example to set action (behavior) to the menu item
- How to set action to a RadioButton in JavaFX?
- How to set action to a slider using JavaFX?
- JavaFX example to set behavior to a button
- JavaFX example to set slider to the progress bar
- What is tri state checkBox, How to create a tri state checkBox in JavaFX?
- How to add an image to a button (action) in JavaFX?
- JavaFX example to add a tooltip to a radio button
- How to set “checked” for a checkbox with jQuery?
- JavaFX example to apply multiple transformations on a node
- How to set a background image to a JavaFX chart?
- How to set checkbox size in HTML/CSS?
