vendredi 2 juin 2017

JavaFX TableView Disable CheckBox

I've created a TableView which contains a checkbox column(isSelected), and three info column(name, surname, job). I want to disable some checkbox according to user info. For example, if user name is "Peter", the checkbox near the Peter will be disabled. But I couldn't. Here is some of my code:

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


}



}




Aucun commentaire:

Enregistrer un commentaire