In this JavaFX example, we will see how to use the JavaFX CheckBox control with an example.
The checkBox is a tri-state selection control box showing a checkmark or tick mark when checked. The control has two states by default: checked and unchecked. The setAllowIndeterminate enables the third state: indeterminate.JavaFX CheckBox Example
The example shows or hides the title of the window depending on whether the check box is selected.import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage stage) {
initUI(stage);
}
private void initUI(Stage stage) {
var root = new HBox();
root.setPadding(new Insets(10, 0, 0, 10));
var cbox = new CheckBox("Show title");
cbox.setSelected(true);
cbox.setOnAction((ActionEvent event) -> {
if (cbox.isSelected()) {
stage.setTitle("CheckBox");
} else {
stage.setTitle("");
}
});
root.getChildren().add(cbox);
var scene = new Scene(root, 300, 200);
stage.setTitle("CheckBox");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Output:
Related JavaFX Examples
- JavaFX GridPane Example
- JavaFX ColorPicker Example
- JavaFX DatePicker Example
- JavaFX MenuBar Example
- JavaFX Radio Button Example
- JavaFX TabPane Example
- JavaFX Accordion Example
- JavaFX Login Form Validation Example
- JavaFX Form Validation - Registration Form Validation Example
- JavaFX Line Chart Example
- JavaFX Area Chart Example
- JavaFX Scatter Chart Example
- JavaFX Bar Chart Example
- JavaFX Pie Chart Example
- JavaFX Select and Multi-Select Example
- JavaFX Check Box Example
- Java Calculator Project
Comments
Post a Comment
Leave Comment