在javafx的tableview中读取Checkbox属性

时间:2018-06-02 04:23:46

标签: java javafx

我是JavaFx的新手。我已经设计了一个桌子现场建造者,在第1列我有一个按钮,在第2列我有一个复选框。单击按钮时,我想获取checkbox属性。但问题是我总是把输出作为假。这是我的代码

Model.java

private boolean Active;     
    public boolean isActive() {
        return Active;
    }   
    public void setActive(boolean active) {
        Active = active;
    }

在我的控制器类中,我设计了下面的initialize()方法

Controller.java

@FXML
private TableView<Model> tableBuilding;
@FXML
private TableColumn<Model,Boolean> colActive;
@FXML
private TableColumn<Model,Model> colAction; 
 @Override
        public void initialize(URL arg0, ResourceBundle arg1) {
//Containing the button         
colAction.setCellFactory(col -> {
                Button ShowButton = new Button("Show");

                TableCell<Model, Model> cell = new TableCell<Model, Model>() {
                    @Override
                    //Updating with the number of row 
                    public void updateItem(Model building, boolean empty) {
                        super.updateItem(building, empty);
                        if (empty) {
                            setGraphic(null);
                        } else {
                            setGraphic(ShowButton);
                        }
                    }              
                };                                          
                ShowButton.setOnAction(event -> {

                TableRow row = cell.getTableRow();
                    Model building=(Model) row.getItem();


                    ObservableList<Model> data = tableBuilding.getItems();

                    for (Model item : data){
                        //check the boolean value of each item to determine checkbox state
                        System.out.println(item.isActive());
                    }

                });

                return cell ;
            });
//Containing the checkbox
colActive.setCellValueFactory(new PropertyValueFactory<Model,Boolean>("Active"));
        colActive.setCellFactory(tc -> new CheckBoxTableCell<>());   

}

此处System.out.println(item.isActive());始终以输出形式重新调整为false。如何获取该复选框的实际属性?有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

我相信你可以稍微简化你的控制器。尝试将控制器代码更改为:

public class Controller {

    @FXML
    private TableView<Model> tableBuilding;
    @FXML
    private TableColumn<Model, Boolean> colActive;
    @FXML
    private TableColumn<Model, Model> colAction;

    @FXML
    public void initialize() {

        // Sample List
        ObservableList<Model> models = FXCollections.observableArrayList();
        models.addAll(
                new Model(),
                new Model(),
                new Model()
        );

        tableBuilding.setItems(models);

        //Containing the button
        colAction.setCellFactory(col -> {
            Button ShowButton = new Button("Show");

            TableCell<Model, Model> cell = new TableCell<>() {
                @Override
                //Updating with the number of row
                public void updateItem(Model building, boolean empty) {
                    super.updateItem(building, empty);
                    if (empty) {
                        setGraphic(null);
                    } else {
                        setGraphic(ShowButton);
                    }
                }
            };

            ShowButton.setOnAction(event -> {

                TableRow row = cell.getTableRow();
                Model building = (Model) row.getItem();

                System.out.println(building.getActive());

            });

            return cell;
        });

        //Containing the checkbox
        colActive.setCellValueFactory(new PropertyValueFactory<>("active"));
        colActive.setCellFactory(col -> new CheckBoxTableCell<>());

    }
}

具体来说,最后两行代码;您不需要指定自己的TableCell,因为CheckBoxTableCell会自行处理Model的更新。

但是,需要更改Model类以将Active字段更新为true属性;否则,TableView无法正确更新。控制器colActive.setCellValueFactory(new PropertyValueFactory<Model,Boolean>("Active"));中的行告诉单元格的值绑定到名为Model的{​​{1}}类中的JavaFX属性,但是没有这样的属性。

以下是定义属性的正确方法:

Active

有趣的是,这似乎是遵循正确的Java编码实践很重要的一种情况。 public class Model { private SimpleBooleanProperty active = new SimpleBooleanProperty(true); public boolean getActive() { return active.get(); } public SimpleBooleanProperty activeProperty() { return active; } public void setActive(boolean active) { this.active.set(active); } } 属性在这里也应该是小写的,不是大写的。