JavaFX Checkbox TableCell setSelected

时间:2014-06-26 14:15:49

标签: javafx-2 javafx-8

我怎么能.setSelected(true)到Checkbox TableCell?这样就会在出现时进行检查。我在下面列出了我的尝试:

TableColumn decemberCol = new TableColumn();
decemberCol.setCellFactory(new PropertyValueFactory<>("december"));
decemberCol.setCellFactory(new Callback<TableColumn<Month, Boolean>, TableCell<Month, Boolean>>() {
        @Override
        public TableCell<Month, Boolean> call(TableColumn<Month, Boolean> getSelectedProperty) {
            return new CheckBoxTableCell<>();     
        }
    });
ObservableList<Month> data = FXCollections.observableArrayList();  
data.add(new Month(true));

...然后将其加载到tableView中

月级:

public Month(Boolean december){
     this.december = new SimpleBooleanProperty(december);
}

public Boolean getDecember(){
    return december.get();
}

public void setDecember(Boolean newValue){
    december.set(newValue);
}

1 个答案:

答案 0 :(得分:1)

使用静态方法CheckBoxTableCell.forTableColumn(...),它将CheckBoxTableCell绑定(双向)到列的单元格值。您可能还需要在Month类中定义属性访问器方法:

TableColumn decemberCol = new TableColumn();
decemberCol.setCellFactory(new PropertyValueFactory<>("december"));
decemberCol.setCellFactory(CheckBoxTableCell.forTableColumn(decemberCol));
ObservableList<Month> data = FXCollections.observableArrayList();  
data.add(new Month(true));

public Month(Boolean december){
     this.december = new SimpleBooleanProperty(december);
}

public Boolean getDecember(){
    return december.get();
}

public void setDecember(Boolean newValue){
    december.set(newValue);
}

public BooleanProperty decemberProperty() {
    return december ;
}