JavaFX TableView禁用CheckBox

时间:2017-06-02 06:42:56

标签: java checkbox javafx tableview

我创建了一个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);


}



}

2 个答案:

答案 0 :(得分:0)

这对你有用。

    $a = (float)4.2;
    $b = (float)1.4*3; // But 2.1*2 works


    if ($b === $a) {
        die("Equal 1");
    }
    else if ($b == $a) {
        die("Equal 2");
    }
    else {
        die("Not Equal");
    }

答案 1 :(得分:0)

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