我怎么能.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);
}
答案 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 ;
}