JavaFX:从不调用TableView onEditCommit处理程序

时间:2015-06-12 09:38:22

标签: java javafx

我尝试从此处http://docs.oracle.com/javafx/2/ui_controls/table-view.htm实施焦点更改的编辑提交。我有以下内容:

 class EditingCell extends TableCell<Object, String> {

    private TextField textField;

    public EditingCell() {
    }

    @Override
    public void startEdit() {
        System.out.println("START EDIT");
        if (!isEmpty()) {
            super.startEdit();
            createTextField();
            setText(null);
            setGraphic(textField);
            textField.selectAll();
        }
    }

    @Override
    public void cancelEdit() {
        System.out.println("CANCEL EDIT");
        super.cancelEdit();
        setText((String) getItem());
        setGraphic(null);
    }

    @Override
    public void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        if (empty) {
            setText(null);
            setGraphic(null);
        } else {
            if (isEditing()) {
                if (textField != null) {
                    textField.setText(getString());
                }
                setText(null);
                setGraphic(textField);
            } else {
                setText(getString());
                setGraphic(null);
            }
        }
    }

    private void createTextField() {
        textField = new TextField(getString());
        textField.setMinWidth(this.getWidth() - this.getGraphicTextGap()* 2);
        textField.focusedProperty().addListener(new ChangeListener<Boolean>(){
            @Override
            public void changed(ObservableValue<? extends Boolean> arg0, 
                Boolean arg1, Boolean arg2) {
                System.out.println("Test1");
                    if (!arg2) {
                        commitEdit(textField.getText());
                    }
            }
        });
    }

    private String getString() {
        return getItem() == null ? "" : getItem().toString();
    }
}

我有POJO DTO,这就是我使用propertybuilder的原因。在这里我使用这段代码:

 Callback<TableColumn, TableCell> cellFactory =
     new Callback<TableColumn, TableCell>() {
        public TableCell call(TableColumn p) {
            return new EditingCell();
        }
 };
 ....
TableColumn textColumn = new TableColumn<>("Text");
textColumn.setEditable(true);
textColumn.setCellFactory(cellFactory);
textColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Answer,String>, ObservableValue<String>>() {
    @Override
    public ObservableValue<String> call(TableColumn.CellDataFeatures<Answer, String> item) {
        try {
            return new JavaBeanStringPropertyBuilder().bean(item.getValue()).name("text").build();
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(TaskDirItemController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
});
textColumn.setOnEditCommit(//THIS HANDLER IS NEVER CALLED
    new EventHandler<CellEditEvent<Answer, String>>() {
        @Override
        public void handle(CellEditEvent<Answer, String> t) {
            System.out.println("New Value:"+t.getNewValue());
            Answer answer=(Answer)t.getTableView().getItems().get(t.getTablePosition().getRow());
        }
    }
);

我的错误是什么?

编辑1
我发现只有当我们将焦点更改为同一个表中除了ROWS之外的任何其他JavaFx组件时,才会调用该处理程序。如何解决?

1 个答案:

答案 0 :(得分:0)

我使用了这段代码https://docs.oracle.com/javafx/2/ui_controls/TableViewSample.java.html, 我用eclipse调试检查,对我来说它正在工作

您需要在框内键入并按[ENTER],使用[TAB]切换另一个框可能不会触发它