1

我正在尝试使用创建我的自定义TreeTable CellFactoryCheckBox我知道有一个名为的默认 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();
            }
        });

在此处输入图像描述

4

1 回答 1

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);
            }
        });
于 2016-04-29T15:33:34.040 回答