簡體   English   中英

在JavaFX的TableView中讀取Checkbox屬性

[英]Readding Checkbox property in a tableview in javafx

我是JavaFx的新手。 我設計了一個表thorugh scenebuilder,其中在第1列中有一個按鈕,在第2列中有一個復選框。 當我單擊按鈕時,我想要獲取復選框屬性。 但是問題是我總是會得到錯誤的輸出。 這是我的代碼

模型

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調整為輸出。 如何獲得該復選框的實際屬性? 誰能幫我嗎?

我相信您可以稍微簡化控制器。 嘗試將控制器代碼更改為:

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")); 告訴單元格的值綁定到名為ActiveModel類中的JavaFX屬性,但是沒有此類屬性。

這是定義屬性的正確方法:

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

有趣的是,這似乎是遵循正確的Java編碼實踐很重要的情況之一。 此處的Active屬性也應為小寫字母,不能大寫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM