━━━━ ◇ ━━━━
Java/JavaFx

JavaFX 21. CheckBox

1. Main.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package application;
    
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.fxml.FXMLLoader;
 
 
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}
 
cs


2. Main.fxml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
 
 
<AnchorPane prefHeight="500.0" prefWidth="500.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="application.MainController">
   <children>
      <CheckBox fx:id="cb1" layoutX="80.0" layoutY="50.0" mnemonicParsing="false" onAction="#checkEvent" text="Dog" />
      <CheckBox fx:id="cb2" layoutX="80.0" layoutY="100.0" mnemonicParsing="false" onAction="#checkEvent" text="Cat" />
      <CheckBox fx:id="cb3" layoutX="80.0" layoutY="150.0" mnemonicParsing="false" onAction="#checkEvent" text="Cow" />
      <CheckBox fx:id="cb4" layoutX="80.0" layoutY="200.0" mnemonicParsing="false" onAction="#checkEvent" text="Rat" />
      <Label fx:id="lblCount" layoutX="106.0" layoutY="296.0" prefHeight="50.0" prefWidth="333.0" />
      <Label fx:id="lblList" layoutX="250.0" layoutY="51.0" prefHeight="173.0" prefWidth="191.0" />
   </children>
</AnchorPane>
 
cs


3. MainController.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package application;
 
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
 
public class MainController {
    @FXML
    private CheckBox cb1;
    
    @FXML
    private CheckBox cb2;
    
    @FXML
    private CheckBox cb3;
    
    @FXML
    private CheckBox cb4;
    
    @FXML
    private Label lblList;
    
    @FXML
    private Label lblCount;
    
    public void checkEvent(ActionEvent event){
        int count = 0;
        String message = "";
        if(cb1.isSelected()){
            count++;
            message += cb1.getText() + "\n";
        }
        if(cb2.isSelected()){
            count++;
            message += cb2.getText() + "\n";
        }
        if(cb3.isSelected()){
            count++;
            message += cb3.getText() + "\n";
        }
        if(cb4.isSelected()){
            count++;
            message += cb4.getText() + "\n";
        }
        lblCount.setText("Items Selected : " + count);
        lblList.setText(message);
    }
}
 
cs



COMMENT