2014-11-05 44 views
1

我一直在嘗試使用JavaFX CheckBoxTreeTableCell提供的單元工廠,但我無法弄清楚如何使它工作。如何使用JavaFX CheckBoxTreeTableCell?

的Javadoc似乎並不匹配的實現。例如:

公共靜態< S,T >回調< TreeTableColumn < S,T >,TreeTableCell < S,T > > forTreeTableColumn(回撥< 整數,ObservableValue <布爾> > getSelectedProperty)

...

參數:

getSelectedProperty - 即,給定類型的對象TreeTableColumn < S,T >,將返回一個ObservableValue <布爾>表示給定項目是否被選中或未被回調。

什麼是整數代表什麼?有沒有人有任何示例代碼如何打算使用?

感謝, 羅布

回答

0

有四種方法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; 
    } 
} 
1

我想你想使用forTreeTableColumn(TreeTableColumn列)。

final TreeTableView<Integer> integerTreeTableView = new TreeTableView<>(); 
    final TreeTableColumn<Integer, Boolean> column = new TreeTableColumn<>(); 
    column.setCellFactory(CheckBoxTreeTableCell.forTreeTableColumn(column)); 
    column.setCellValueFactory(param -> { 
       if(param.getValue().getValue() > 5) { 
        return new SimpleBooleanProperty(true); 
       } else { 
        return new SimpleBooleanProperty(false); 
       } 
      }); 
    integerTreeTableView.getColumns().add(column);