Javafx:自定义复选框 treetablecell 不提交更改
Javafx: custom checkbox treetablecell doesn't commit the changes
我正在尝试使用 CheckBox
创建我的自定义 TreeTable
CellFactory
,我知道有一个名为 CheckBoxTreeTableCell.forTreeTableColumn()
的默认 CellFactory,但我想使用我的自定义 cellFactory 这样我就可以在点击 CheckBox
.
后做一些事情
所以,我的问题是,当我单击复选框并调用 commitEdit()
将 chagnes 保存到单元格中时,它不起作用!
这是我的自定义 cellFactory class:
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();
}
});
我找到了解决方案,我只需要更新对象,在我的例子中 Task
,它存在于 TreeTableRow
中,如下所示:
checkBox.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
boolean c = checkBox.isSelected();
TreeTableRow<Task> row = getTreeTableRow();
Task tsk = row.getItem();
tsk.setStat(c);
}
});
我正在尝试使用 CheckBox
创建我的自定义 TreeTable
CellFactory
,我知道有一个名为 CheckBoxTreeTableCell.forTreeTableColumn()
的默认 CellFactory,但我想使用我的自定义 cellFactory 这样我就可以在点击 CheckBox
.
后做一些事情
所以,我的问题是,当我单击复选框并调用 commitEdit()
将 chagnes 保存到单元格中时,它不起作用!
这是我的自定义 cellFactory class:
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();
}
});
我找到了解决方案,我只需要更新对象,在我的例子中 Task
,它存在于 TreeTableRow
中,如下所示:
checkBox.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
boolean c = checkBox.isSelected();
TreeTableRow<Task> row = getTreeTableRow();
Task tsk = row.getItem();
tsk.setStat(c);
}
});