I'm trying to create my custom TreeTable
CellFactory
using CheckBox
, I know there is a default CellFactory called CheckBoxTreeTableCell.forTreeTableColumn()
, but I want to use my custom cellFactory so I can do somethings after clicking on the CheckBox
.
So, my problem is that when I click on the checkbox and call commitEdit()
to save the chagnes into the cell it doesn't work !
Here is my custom 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);
}
}
}
And simply I applied the cellFactory to my column like this:
myColumn.setCellFactory(new Callback<TreeTableColumn<Task,Boolean>, TreeTableCell<Task,Boolean>>() {
@Override
public TreeTableCell<Task, Boolean> call(TreeTableColumn<Task, Boolean> e) {
return new CheckboxCellFactory();
}
});
I found the solution, I just have to update the object, in my case Task
, that exists inside TreeTableRow
like this:
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);
}
});