Javafx:自定义复选框treetablecell不提交更改

时间:2016-04-29 10:01:54

标签: javafx-2 treetablecelleditor

我正在尝试使用TreeTable创建自定义CellFactory CheckBox,我知道有一个名为CheckBoxTreeTableCell.forTreeTableColumn()的默认CellFactory,但我想使用自定义的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();
            }
        });

enter image description here

1 个答案:

答案 0 :(得分:1)

我找到了解决方案,我只需更新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);
            }
        });