There are four methods for CheckBoxTreeTableCell.forTreeTableColumn
. All of them expect a callback, being the TreeTableColumn
of type ObservableValue<Boolean>
, or just a TreeTableColumn
of type Boolean
.
In the last case, since you already provide the column, when the updateItem
method is called for a given index to render the checkbox, its selected state is found at that position.
While on the methods with the callbacks, to find the selected state for a given index, a call
to that index is made.
This is a very simple use case of both situations. You can see how the callback use the index to go into the collection and retrieve the status:
private final ObservableList<Person> data = FXCollections.observableArrayList(
new Person("Staff",false),
new Person("John",false),
new Person("Greg",true));
@Override
public void start(Stage primaryStage) {
TreeItem<Person> rootTree = new TreeItem<>(data(0));
rootTree.setExpanded(true);
data.stream().skip(1).forEach(person->rootTree.getChildren().add(new TreeItem<>(person)));
TreeTableView<Person> table=new TreeTableView<>(rootTree);
TreeTableColumn<Person,String> columnName=new TreeTableColumn<>("Name");
columnName.setCellValueFactory(cellData -> cellData.getValue().getValue().nameProperty());
columnName.setPrefWidth(100);
TreeTableColumn<Person,Boolean> columnWeight=new TreeTableColumn<>("Overweight");
// case TreeTableColumn (uncomment to run)
// columnWeight.setCellFactory(CheckBoxTreeTableCell.forTreeTableColumn(columnWeight));
// case Callback:
columnWeight.setCellFactory(
CheckBoxTreeTableCell.forTreeTableColumn(
(Integer param) -> data.get(param).overWeightProperty()));
columnWeight.setCellValueFactory(cellData -> cellData.getValue().getValue().overWeightProperty());
columnWeight.setPrefWidth(150);
table.getColumns().addAll(columnName, columnWeight);
table.setEditable(true);
Scene scene = new Scene(table, 400, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
where:
private class Person {
public Person(String name, boolean overWeight) {
this.name.set(name);
this.overWeight.set(overWeight);
}
private final StringProperty name = new SimpleStringProperty();
public String getName() {
return name.get();
}
public void setName(String value) {
name.set(value);
}
public StringProperty nameProperty() {
return name;
}
private final BooleanProperty overWeight = new SimpleBooleanProperty();
public boolean isOverWeight() {
return overWeight.get();
}
public void setOverWeight(boolean value) {
overWeight.set(value);
}
public BooleanProperty overWeightProperty() {
return overWeight;
}
}