JavaFX 8:TableView和Add-on中的Checkbox进入所选项目?

时间:2016-01-03 18:27:10

标签: checkbox tableview javafx-8 insert-update

我想创建一个工资单程序,当用户勾选TableView中的CheckBox时,名称(字符串)将被转移到右侧的面板上,并使用TextFields输入更多信息,例如:

this

到目前为止,我尝试按照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);
    }
}

Employee.java

    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;
        }
    }

EmployeeOverviewController.java

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());
        }
    }

用于使checkBox在表中可见的util类:

SelectBoxCellFactory.java

public class SelectBoxCellFactory implements Callback {
    @Override
    public TableCell call(Object param) {
        CheckBoxTableCell<Employee,Boolean> checkBoxCell = new CheckBoxTableCell();
        return checkBoxCell;
    }
}

到目前为止,这是我的输出:

here

我知道它与之前的输出相比有一个表格。老实说,我仍然犹豫不决,因为我觉得使用TextField会让它看起来更好。但我现在所希望的是,这种设计并非不可能编码...

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

1 个答案:

答案 0 :(得分:0)

对于右侧面板,使用TableView可能最容易。您可以从原始列表中创建FilteredList

FilteredList<Employee> selectedEmployees 
    = new FilteredList<>(selectEmployeeTable, Employee::getSelectBox);

然后将其用于第二个表格。

如果您更喜欢使用文本字段(看起来像GridPane?),您仍然可以使用上面的过滤列表,但是您需要使用它注册一个监听器并更新布局&#34;用手#34;添加和删​​除项目时。