我正在尝试使用创建我的自定义TreeTable
CellFactory
,CheckBox
我知道有一个名为的默认 CellFactory CheckBoxTreeTableCell.forTreeTableColumn()
,但我想使用我的自定义 cellFactory,以便在单击CheckBox
.
所以,我的问题是,当我单击复选框并调用commitEdit()
将 chagnes 保存到单元格中时,它不起作用!
这是我的自定义 cellFactory 类:
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TreeTableCell;
import smt.tsk.model.Task;
public class CheckboxCellFactory extends TreeTableCell<Task, Boolean>{
private CheckBox checkBox;
public CheckboxCellFactory() {
checkBox = new CheckBox();
checkBox.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("clicked: "+checkBox.isSelected());
//---I called this here to save changes into the cell after clicking on the CheckBox
commitEdit(checkBox.isSelected());
}
});
}
@Override
protected void updateItem(Boolean item, boolean empty) {
if (empty) {
setText(null);
setGraphic(null);
}else{
checkBox.setSelected(item);
setText(null);
setGraphic(checkBox);
}
}
}
我只是将 cellFactory 应用到我的列中,如下所示:
myColumn.setCellFactory(new Callback<TreeTableColumn<Task,Boolean>, TreeTableCell<Task,Boolean>>() {
@Override
public TreeTableCell<Task, Boolean> call(TreeTableColumn<Task, Boolean> e) {
return new CheckboxCellFactory();
}
});