CheckBox is used to define check box control.
CheckBox states
CheckBox control has three states.
a. Checked: Indeterminate property is false and selected property is true.
b. Unchecked: Indeterminate property is false and selected property is false.
c. Undefined: Indeterminate property is true. Undefined or indeterminate state of the CheckBox is rendered with a minus or dash sign.
CheckBox class provides following constructors to define CheckBox instance.
public CheckBox()
public CheckBox(String text)
'text' specifies a text string for its label.
Find the below working application.
checkBoxDemo.fxml
<?import javafx.scene.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.control.*?>
<VBox xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.widgets.controller.CheckBoxController"
fx:id="vBox1" spacing="20" alignment="center">
<HBox fx:id="lableHBox">
<Label fx:id="title" text="Select the vegetables" wrapText="true"/>
</HBox>
<HBox fx:id="vegetableChooserHBox">
<VBox>
<CheckBox fx:id="broccoli" text="broccoli"/>
<CheckBox fx:id="corn" text="corn"/>
<CheckBox fx:id="cucumber" text="cucumber"/>
</VBox>
<VBox>
<CheckBox fx:id="lettuce" text="lettuce"/>
<CheckBox fx:id="pumpkin" text="pumpkin"/>
<CheckBox fx:id="pumpkin" text="pumpkin"/>
</VBox>
<VBox>
<CheckBox fx:id="beetroot" text="beetroot"/>
<CheckBox fx:id="sprouts" text="sprouts"/>
<CheckBox fx:id="carrot" text="carrot"/>
</VBox>
</HBox>
<HBox fx:id="lableHBox">
<Label fx:id="selectedVegetables" text="" wrapText="true"/>
</HBox>
</VBox>
CheckBoxController.java
package com.sample.app.widgets.controller;
import java.net.URL;
import java.util.*;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.CheckBox;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
public class CheckBoxController implements Initializable {
@FXML
private VBox vBox1;
@FXML
private HBox vegetableChooserHBox;
@FXML
private Label selectedVegetables;
@FXML
private Label title;
@Override
public void initialize(URL location, ResourceBundle resources) {
DropShadow dropShadow = new DropShadow();
dropShadow.setOffsetX(5);
dropShadow.setOffsetY(5);
dropShadow.setColor(Color.GRAY);
vBox1.setEffect(dropShadow);
vBox1.setStyle("-fx-background-color:lightyellow");
vegetableChooserHBox.setSpacing(10);
vegetableChooserHBox.setPadding(new Insets(20, 20, 20, 80));
Font font = Font.font("Verdana", FontPosture.REGULAR, 25);
Iterator<Node> nodes = vegetableChooserHBox.getChildren().iterator();
vegetableChooserHBox.setSpacing(10);
List<CheckBox> checkBoxes = new ArrayList<>();
while (nodes.hasNext()) {
VBox vBox = (VBox) nodes.next();
vBox.setSpacing(10);
Iterator<Node> vBoxNodes = vBox.getChildren().iterator();
while (vBoxNodes.hasNext()) {
Node node = vBoxNodes.next();
if (node instanceof CheckBox) {
CheckBox checkBox = (CheckBox) node;
checkBoxes.add(checkBox);
checkBox.setFont(font);
checkBox.setOnAction((event) -> {
StringJoiner stringJoiner = new StringJoiner(",");
for (CheckBox checkBoxTemp : checkBoxes) {
if (checkBoxTemp.isSelected()) {
stringJoiner.add(checkBoxTemp.getText());
}
}
String info = "Selected items are : " + stringJoiner.toString();
selectedVegetables.setText(info);
});
}
}
}
title.setFont(Font.font("Verdana", FontPosture.REGULAR, 50));
title.setPadding(new Insets(20, 20, 20, 20));
selectedVegetables.setFont(Font.font("Verdana", FontPosture.ITALIC, 25));
selectedVegetables.setTextFill(Color.BLUE);
selectedVegetables.setPadding(new Insets(20, 20, 20, 20));
}
}
CheckBoxDemo.java
package com.sample.app.widgets;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class CheckBoxDemo extends Application {
private static final String FXML_FILE = "/checkBoxDemo.fxml";
private static final String STAGE_TITLE = "CheckBox Demo";
public static void main(String args[]) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = (Parent) FXMLLoader.load(this.getClass().getResource(FXML_FILE));
Scene scene = new Scene(root, 700, 600, Color.WHITE);
primaryStage.setTitle(STAGE_TITLE);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Output
Select vegetables, you can
see the selected items information in below of the window.
Previous Next Home
No comments:
Post a Comment