• Как сделать чекбокс с выбором всех чекбоксов в TableView на JavaFX и FXML?

    Z0RAN
    @Z0RAN
    Мне кажется, я нашёл, то что требуется.
    5e561f3e9bfcc272783963.png

    класс Main:
    package sample;
    
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    import java.io.IOException;
    
    public class Main extends Application{
    
        @Override
        public void start(Stage primaryStage) throws IOException {
            Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
            primaryStage.setTitle("Tabel Test");
            primaryStage.setScene(new Scene(root, 600, 400));
            primaryStage.show();
        }
    
        public static void main (String[] args) {
            launch();
        }
    }


    класс Controller:
    package sample;
    
    import java.net.URL;
    import java.util.ResourceBundle;
    
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.scene.control.CheckBox;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    import javafx.scene.control.cell.PropertyValueFactory;
    
    public class Controller {
    
        @FXML
        private ResourceBundle resources;
    
        @FXML
        private URL location;
    
        @FXML
        private TableView<Names> tabel;
    
        @FXML
        private TableColumn<Names, Boolean> col1;
    
        @FXML
        private CheckBox checkBox;
    
        @FXML
        private TableColumn<Names, String> col2;
    
        private static boolean chck;
    
        @FXML
        void checkBoxInitialize(ActionEvent event) {
            chck = checkBox.isSelected() ? true : false;
            for (Names n : data) {
                n.setCheck(chck);
            }
        }
    
        ObservableList<Names> data = FXCollections.observableArrayList();
    
        @FXML
        void initialize() {
            col1.setCellValueFactory(new PropertyValueFactory<Names, Boolean>("check"));
            col2.setCellValueFactory(new PropertyValueFactory<Names, String>("name"));
    
            for (int i = 1; i < 10; i++) {
                data.add(new Names("Name " + i));
            }
            tabel.setItems(data);
        }
    }


    класс Names:
    package sample;
    
    import javafx.scene.control.CheckBox;
    
    public class Names {
        private CheckBox check;
        private String name;
    
        public CheckBox getCheck() {
            return check;
        }
    
        public void setCheck(Boolean x) {
            if(x) {
                check.setSelected(true);
            } else {
              check.setSelected(false);
            }
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Names(String name) {
            check = new CheckBox();
            this.name = name;
        }
    }


    файл FXML (sample.fxml):
    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.control.CheckBox?>
    <?import javafx.scene.control.TableColumn?>
    <?import javafx.scene.control.TableView?>
    <?import javafx.scene.layout.AnchorPane?>
    
    <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
        <children>
            <TableView fx:id="tabel" editable="true" layoutX="32.0" layoutY="47.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                <columns>
                    <TableColumn fx:id="col1" prefWidth="26.0" resizable="false">
                        <graphic>
                            <CheckBox fx:id="checkBox" mnemonicParsing="false" onAction="#checkBoxInitialize" prefHeight="17.0" prefWidth="23.0" />
                        </graphic>
                    </TableColumn>
                    <TableColumn fx:id="col2" minWidth="0.0" prefWidth="572.0" text="Имя" />
                </columns>
            </TableView>
        </children>
    </AnchorPane>
    Ответ написан
    Комментировать