簡體   English   中英

JavaFX TableView禁用CheckBox

[英]JavaFX TableView Disable CheckBox

我創建了一個TableView,其中包含一個復選框列(isSelected)和三個info列(name,surname,job)。 我想根據用戶信息禁用一些復選框。 例如,如果用戶名是“Peter”,則Peter附近的復選框將被禁用。 但我不能。 這是我的一些代碼:

Person.java

public class Person {

private final SimpleStringProperty name;
private final SimpleStringProperty surname;
private final SimpleStringProperty job;
private final BooleanProperty isSelected;

public Person(SimpleStringProperty name, SimpleStringProperty surname, SimpleStringProperty job,
        BooleanProperty isSelected) {
    super();
    this.name = name;
    this.surname = surname;
    this.job = job;
    this.isSelected = isSelected;
}

public SimpleStringProperty getName() {
    return name;
}

public SimpleStringProperty getSurname() {
    return surname;
}

public SimpleStringProperty getJob() {
    return job;
}

public BooleanProperty getIsSelected() {
    return isSelected;
}



}

Controller.java

public class Controller {

@FXML
private final TableView<Person> fxPersonTableView;

@FXML
private final TableColumn<Person, Boolean> fxSelectColumnCheckbox;

@FXML
private final TableColumn<Person, String> fxNameTableColumn;

@FXML
private final TableColumn<Person, String> fxSurnameTableColumn;

@FXML
private final TableColumn<Person, String> fxJobTableColumn;

private ObservableList<Person> persons;

private void createTableView(){

    fxSelectColumnCheckbox.setCellValueFactory(c -> c.getValue().getIsSelected());

    fxSelectColumnCheckbox.setCellFactory(CheckBoxTableCell.forTableColumn(c -> {
    //I guess here I will put some control like as name check and disable 
    //the checkbox in this row. But I can't reach the checkbox individually.
        return persons.get(c).getIsSelected();
    }));

    fxNameTableColumn.setCellValueFactory(c -> persons.get(c).getName());
    fxSurnameTableColumn.setCellValueFactory(c -> persons.get(c).getSurname());
    fxJobTableColumn.setCellValueFactory(c -> persons.get(c).getJob());
    fxPersonTableView.getSelectionModel.setSelectionMode(SelectionMode.MULTIPLE);
    fxPersonTableView.setItems(persons);


}



}

這對你有用。

fxNameTableColumn.setCellFactory(column -> {
                return new TableCell<Person, String>() {
                    @Override
                    protected void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        // Iterating through Table rows
                        TableRow<Person> currentRow = getTableRow();
                        if (item != null && !empty) {
                            if (item.equals("Peter")) {
                                // a row containing Peter name is found
                                // disable your check box  
                                setStyle(fxPersonTableView.getStyle());
                            } 
                        } else {
                            setStyle(fxPersonTableView.getStyle());
                        }
                    }
                };
            });

謝謝@Ammar,我已經解決了這個問題,請使用你的答案:

fxSelectColumnCheckbox.setCellFactory(column -> {
        return new CheckBoxTableCell<Person, Boolean>() {
            @Override
            public void updateItem(Boolean item, boolean empty) {
                super.updateItem(item, empty);

                TableRow<Person> currentRow = getTableRow();
                this.setDisable(false); // it is required to fit default state
                if (currentRow.getItem() != null && !empty) {
                    if (currentRow.getItem().getName().equals("Peter")) {
                        this.setDisable(true);
                        setStyle(fxPersonTableView.getStyle());
                    }
                } else {

                    setStyle(fxPersonTableView.getStyle());
                }
            }
        };
    });

暫無
暫無

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

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