2

I try to implement edit commit on focus changes from here http://docs.oracle.com/javafx/2/ui_controls/table-view.htm. And I have the following:

 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();
    }
}

I have POJO DTO and that why I use propertybuilder. And here I use this code:

 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());
        }
    }
);

What is my mistake?

EDIT 1
I found out that the handler is called only then, when we change focus to any other JavaFx component EXCEPT THE ROWS in the same table. How to fix it?

  • @Uluk Biy Could you take a look? –  Jun 12 '15 at 13:08
  • It is a bug in JavaFX 8. Curently there is no easy workaround for it. See http://stackoverflow.com/questions/29576577/tableview-doesnt-commit-values-on-focus-lost-event – Uluk Biy Jun 12 '15 at 13:15
  • @Uluk Biy Thank you! As I said - Maestro! –  Jun 12 '15 at 13:18

1 Answers1

0

I used this code https://docs.oracle.com/javafx/2/ui_controls/TableViewSample.java.html, and I checked with eclipse debug, for me it is working

You need to type inside the box and press [ENTER], switching another box using [TAB] may not trigger it

Dickens A S
  • 3,824
  • 2
  • 22
  • 45