Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Checkbox JavaFX8:是否选中TableView中的复选框并将其添加到所选项目?_Checkbox_Tableview_Javafx 8_Insert Update - Fatal编程技术网

Checkbox JavaFX8:是否选中TableView中的复选框并将其添加到所选项目?

Checkbox JavaFX8:是否选中TableView中的复选框并将其添加到所选项目?,checkbox,tableview,javafx-8,insert-update,Checkbox,Tableview,Javafx 8,Insert Update,我想创建一个工资单程序,这样当用户在TableView中勾选复选框时,名称(字符串)将被带到右侧的面板上,并带有文本字段以输入更多信息,例如: 我尝试按照MVC层次结构编写代码: PayrollMainApp.java public class PayrollMainApp extends Application { private Stage primaryStage; private BorderPane rootLayout; private Observab

我想创建一个工资单程序,这样当用户在TableView中勾选复选框时,名称(字符串)将被带到右侧的面板上,并带有文本字段以输入更多信息,例如:

我尝试按照MVC层次结构编写代码:

PayrollMainApp.java

public class PayrollMainApp extends Application {

    private Stage primaryStage;
    private BorderPane rootLayout;

    private ObservableList<Employee> selectEmployeeTable = FXCollections.observableArrayList();
    
    
    public PayrollMainApp(){
        selectEmployeeTable.add(new Employee(false,"Hans Muster"));
        selectEmployeeTable.add(new Employee(true,"Ruth Mueller"));
        selectEmployeeTable.add(new Employee(false,"Heinz Kurz"));
        selectEmployeeTable.add(new Employee(false,"Cornelia Meier"));
        selectEmployeeTable.add(new Employee(false,"Werner Meyer"));
        selectEmployeeTable.add(new Employee(false,"Lydia Kunz"));
        selectEmployeeTable.add(new Employee(false,"Anna Best"));
        selectEmployeeTable.add(new Employee(false,"Stefan Meier"));
        selectEmployeeTable.add(new Employee(false,"Martin Mueller"));
    }

    public ObservableList<Employee> getSelectEmployeeTable(){
        return selectEmployeeTable;
    }
    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("PayrollApp");

        initRootLayout();

        showEmployeeOverview();
    }

    /**
     * Initializes the root layout.
     */
    public void initRootLayout() {
        try {
            // Load root layout from fxml file.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(PayrollMainApp.class.getResource("view/RootLayout.fxml"));
            rootLayout = (BorderPane) loader.load();

            // Show the scene containing the root layout.
            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.show();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Shows the person overview inside the root layout.
     */
    public void showEmployeeOverview() {
        try {
            // Load person overview.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(PayrollMainApp.class.getResource("view/EmployeeOverview.fxml"));
            AnchorPane personOverview = (AnchorPane) loader.load();

            // Set person overview into the center of root layout.
            rootLayout.setCenter(personOverview);

            // Give the controller access to the main app
            EmployeeOverviewController controller = loader.getController();
            controller.setMainApp(this);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Returns the main stage.
     * @return
     */
    public Stage getPrimaryStage() {
        return primaryStage;
    }

    public static void main(String[] args) {
        launch(args);
    }
}
    public class Employee {

        private BooleanProperty checkedBox = new SimpleBooleanProperty(false);
        private StringProperty employeeName = new SimpleStringProperty();
        
        public Employee(){
            super();
        }
        public Employee(boolean checkedBox, String employeeName){
            this.checkedBox = new SimpleBooleanProperty(false);
            this.employeeName = new SimpleStringProperty(employeeName);
        }
        public BooleanProperty checkedBoxProperty(){
            return this.checkedBox;
        }
        public StringProperty employeeNameProperty(){
            return this.employeeName;
        }
    
        public java.lang.Boolean getSelectBox() {
            return this.checkedBoxProperty().get();
        }
    
        public StringProperty getEmployeeName() {
            return employeeName;
        }
        public void setSelectBox(final java.lang.Boolean checkedBox){
            this.checkedBoxProperty().set(checkedBox);
        }
        public void setEmployeeName(StringProperty employeeName) {
            this.employeeName = employeeName;
        }
    }
public class EmployeeOverviewController {    
        @FXML
        private TableView<Employee> selectEmployeeTable;
        @FXML
        private TableColumn<Employee, String> employeeNameColumn;
        @FXML
        private TableColumn<Employee, Boolean> checkBoxColumn;
    
        private PayrollMainApp mainApp;
    
        public EmployeeOverviewController() {
        }
    
        @FXML
        public void initialize() {
            checkBoxColumn.setCellValueFactory(cellData -> cellData.getValue().checkedBoxProperty());
            checkBoxColumn.setCellFactory(param -> new CheckBoxTableCell<Employee, Boolean>());
            employeeNameColumn.setCellValueFactory(cellData -> cellData.getValue().employeeNameProperty());
        }
    
    
        public void setMainApp(PayrollMainApp mainApp){
            this.mainApp = mainApp;
    
            //Add observable list data to the table
            selectEmployeeTable.setItems(mainApp.getSelectEmployeeTable());
        }
    }
public class SelectBoxCellFactory implements Callback {
    @Override
    public TableCell call(Object param) {
        CheckBoxTableCell<Employee,Boolean> checkBoxCell = new CheckBoxTableCell();
        return checkBoxCell;
    }
}
EmployeeOverviewController.java

public class PayrollMainApp extends Application {

    private Stage primaryStage;
    private BorderPane rootLayout;

    private ObservableList<Employee> selectEmployeeTable = FXCollections.observableArrayList();
    
    
    public PayrollMainApp(){
        selectEmployeeTable.add(new Employee(false,"Hans Muster"));
        selectEmployeeTable.add(new Employee(true,"Ruth Mueller"));
        selectEmployeeTable.add(new Employee(false,"Heinz Kurz"));
        selectEmployeeTable.add(new Employee(false,"Cornelia Meier"));
        selectEmployeeTable.add(new Employee(false,"Werner Meyer"));
        selectEmployeeTable.add(new Employee(false,"Lydia Kunz"));
        selectEmployeeTable.add(new Employee(false,"Anna Best"));
        selectEmployeeTable.add(new Employee(false,"Stefan Meier"));
        selectEmployeeTable.add(new Employee(false,"Martin Mueller"));
    }

    public ObservableList<Employee> getSelectEmployeeTable(){
        return selectEmployeeTable;
    }
    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("PayrollApp");

        initRootLayout();

        showEmployeeOverview();
    }

    /**
     * Initializes the root layout.
     */
    public void initRootLayout() {
        try {
            // Load root layout from fxml file.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(PayrollMainApp.class.getResource("view/RootLayout.fxml"));
            rootLayout = (BorderPane) loader.load();

            // Show the scene containing the root layout.
            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.show();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Shows the person overview inside the root layout.
     */
    public void showEmployeeOverview() {
        try {
            // Load person overview.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(PayrollMainApp.class.getResource("view/EmployeeOverview.fxml"));
            AnchorPane personOverview = (AnchorPane) loader.load();

            // Set person overview into the center of root layout.
            rootLayout.setCenter(personOverview);

            // Give the controller access to the main app
            EmployeeOverviewController controller = loader.getController();
            controller.setMainApp(this);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Returns the main stage.
     * @return
     */
    public Stage getPrimaryStage() {
        return primaryStage;
    }

    public static void main(String[] args) {
        launch(args);
    }
}
    public class Employee {

        private BooleanProperty checkedBox = new SimpleBooleanProperty(false);
        private StringProperty employeeName = new SimpleStringProperty();
        
        public Employee(){
            super();
        }
        public Employee(boolean checkedBox, String employeeName){
            this.checkedBox = new SimpleBooleanProperty(false);
            this.employeeName = new SimpleStringProperty(employeeName);
        }
        public BooleanProperty checkedBoxProperty(){
            return this.checkedBox;
        }
        public StringProperty employeeNameProperty(){
            return this.employeeName;
        }
    
        public java.lang.Boolean getSelectBox() {
            return this.checkedBoxProperty().get();
        }
    
        public StringProperty getEmployeeName() {
            return employeeName;
        }
        public void setSelectBox(final java.lang.Boolean checkedBox){
            this.checkedBoxProperty().set(checkedBox);
        }
        public void setEmployeeName(StringProperty employeeName) {
            this.employeeName = employeeName;
        }
    }
public class EmployeeOverviewController {    
        @FXML
        private TableView<Employee> selectEmployeeTable;
        @FXML
        private TableColumn<Employee, String> employeeNameColumn;
        @FXML
        private TableColumn<Employee, Boolean> checkBoxColumn;
    
        private PayrollMainApp mainApp;
    
        public EmployeeOverviewController() {
        }
    
        @FXML
        public void initialize() {
            checkBoxColumn.setCellValueFactory(cellData -> cellData.getValue().checkedBoxProperty());
            checkBoxColumn.setCellFactory(param -> new CheckBoxTableCell<Employee, Boolean>());
            employeeNameColumn.setCellValueFactory(cellData -> cellData.getValue().employeeNameProperty());
        }
    
    
        public void setMainApp(PayrollMainApp mainApp){
            this.mainApp = mainApp;
    
            //Add observable list data to the table
            selectEmployeeTable.setItems(mainApp.getSelectEmployeeTable());
        }
    }
public class SelectBoxCellFactory implements Callback {
    @Override
    public TableCell call(Object param) {
        CheckBoxTableCell<Employee,Boolean> checkBoxCell = new CheckBoxTableCell();
        return checkBoxCell;
    }
}
公共类EmployeeOverviewController{
@FXML
私有表查看selectEmployeeTable;
@FXML
private TableColumn employeeNameColumn;
@FXML
私有表列checkBoxColumn;
私人PayrollMainApp mainApp;
公共雇员概览控制器(){
}
@FXML
公共无效初始化(){
checkBoxColumn.setCellValueFactory(cellData->cellData.getValue().checkedBoxProperty());
setCellFactory(参数->新建CheckBoxTableCell());
employeeNameColumn.setCellValueFactory(cellData->cellData.getValue().employeeNameProperty());
}
公共void setMainApp(PayrollMainApp mainApp){
this.mainApp=mainApp;
//将可观察列表数据添加到表中
选择EmployeeTable.setItems(mainApp.getSelectEmployeeTable());
}
}
以及使复选框在表中可见的util类:

选择BoxCellFactory.java

public class PayrollMainApp extends Application {

    private Stage primaryStage;
    private BorderPane rootLayout;

    private ObservableList<Employee> selectEmployeeTable = FXCollections.observableArrayList();
    
    
    public PayrollMainApp(){
        selectEmployeeTable.add(new Employee(false,"Hans Muster"));
        selectEmployeeTable.add(new Employee(true,"Ruth Mueller"));
        selectEmployeeTable.add(new Employee(false,"Heinz Kurz"));
        selectEmployeeTable.add(new Employee(false,"Cornelia Meier"));
        selectEmployeeTable.add(new Employee(false,"Werner Meyer"));
        selectEmployeeTable.add(new Employee(false,"Lydia Kunz"));
        selectEmployeeTable.add(new Employee(false,"Anna Best"));
        selectEmployeeTable.add(new Employee(false,"Stefan Meier"));
        selectEmployeeTable.add(new Employee(false,"Martin Mueller"));
    }

    public ObservableList<Employee> getSelectEmployeeTable(){
        return selectEmployeeTable;
    }
    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("PayrollApp");

        initRootLayout();

        showEmployeeOverview();
    }

    /**
     * Initializes the root layout.
     */
    public void initRootLayout() {
        try {
            // Load root layout from fxml file.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(PayrollMainApp.class.getResource("view/RootLayout.fxml"));
            rootLayout = (BorderPane) loader.load();

            // Show the scene containing the root layout.
            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.show();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Shows the person overview inside the root layout.
     */
    public void showEmployeeOverview() {
        try {
            // Load person overview.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(PayrollMainApp.class.getResource("view/EmployeeOverview.fxml"));
            AnchorPane personOverview = (AnchorPane) loader.load();

            // Set person overview into the center of root layout.
            rootLayout.setCenter(personOverview);

            // Give the controller access to the main app
            EmployeeOverviewController controller = loader.getController();
            controller.setMainApp(this);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Returns the main stage.
     * @return
     */
    public Stage getPrimaryStage() {
        return primaryStage;
    }

    public static void main(String[] args) {
        launch(args);
    }
}
    public class Employee {

        private BooleanProperty checkedBox = new SimpleBooleanProperty(false);
        private StringProperty employeeName = new SimpleStringProperty();
        
        public Employee(){
            super();
        }
        public Employee(boolean checkedBox, String employeeName){
            this.checkedBox = new SimpleBooleanProperty(false);
            this.employeeName = new SimpleStringProperty(employeeName);
        }
        public BooleanProperty checkedBoxProperty(){
            return this.checkedBox;
        }
        public StringProperty employeeNameProperty(){
            return this.employeeName;
        }
    
        public java.lang.Boolean getSelectBox() {
            return this.checkedBoxProperty().get();
        }
    
        public StringProperty getEmployeeName() {
            return employeeName;
        }
        public void setSelectBox(final java.lang.Boolean checkedBox){
            this.checkedBoxProperty().set(checkedBox);
        }
        public void setEmployeeName(StringProperty employeeName) {
            this.employeeName = employeeName;
        }
    }
public class EmployeeOverviewController {    
        @FXML
        private TableView<Employee> selectEmployeeTable;
        @FXML
        private TableColumn<Employee, String> employeeNameColumn;
        @FXML
        private TableColumn<Employee, Boolean> checkBoxColumn;
    
        private PayrollMainApp mainApp;
    
        public EmployeeOverviewController() {
        }
    
        @FXML
        public void initialize() {
            checkBoxColumn.setCellValueFactory(cellData -> cellData.getValue().checkedBoxProperty());
            checkBoxColumn.setCellFactory(param -> new CheckBoxTableCell<Employee, Boolean>());
            employeeNameColumn.setCellValueFactory(cellData -> cellData.getValue().employeeNameProperty());
        }
    
    
        public void setMainApp(PayrollMainApp mainApp){
            this.mainApp = mainApp;
    
            //Add observable list data to the table
            selectEmployeeTable.setItems(mainApp.getSelectEmployeeTable());
        }
    }
public class SelectBoxCellFactory implements Callback {
    @Override
    public TableCell call(Object param) {
        CheckBoxTableCell<Employee,Boolean> checkBoxCell = new CheckBoxTableCell();
        return checkBoxCell;
    }
}
public类SelectBoxCellFactory实现回调{
@凌驾
公共TableCell调用(对象参数){
CheckBoxTableCell checkBoxCell=新的CheckBoxTableCell();
返回checkBoxCell;
}
}
以下是我迄今为止的输出:

我知道与之前的输出相比,这有一个表。老实说,我仍然拿不定主意使用哪个,因为我认为使用文本字段会让它看起来更好。但我现在所希望的是,这个设计不是不可能编码的


我真的希望你能帮助我。。。提前感谢您的帮助。

在右侧面板上使用
表格视图可能是最简单的方法。您可以从原始列表中创建一个:

FilteredList<Employee> selectedEmployees 
    = new FilteredList<>(selectEmployeeTable, Employee::getSelectBox);
FilteredList选择的员工
=new FilteredList(selectEmployeeTable,Employee::getSelectBox);
然后将其用于第二张桌子

如果您喜欢使用文本字段(在类似于
网格窗格
?)的地方),您仍然可以使用上面的筛选列表,但您需要向其注册一个侦听器,并在添加和删除项目时“手动”更新布局