有四種方法CheckBoxTreeTableCell.forTreeTableColumn
。他們都期待回調,即ObservableValue<Boolean>
類型的TreeTableColumn
,或Boolean
類型的TreeTableColumn
。
在最後一種情況下,由於您已經提供了該列,因此在爲給定索引調用updateItem
方法來呈現複選框時,會在該位置找到其選定狀態。
雖然在與回調的方法中,發現對於給定的索引所選擇的狀態下,call
該索引製成。
這是這兩種情況下很簡單的用例。你可以看到回調如何使用索引進入收集和檢索狀態:
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();
}
其中:
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;
}
}