1

I intend to commit the changes on a TableCell upon clicking somewhere else on the TableView. Here is my implementation of createTextField method from the Oracle tutorial.

private void createTextField() {
    textField.setText(getString());
    textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
    textField.addEventFilter(KeyEvent.KEY_PRESSED, (KeyEvent t) -> {
        if (t.getCode() == KeyCode.ENTER || t.getCode() == KeyCode.TAB) {
            commitEdit(textField.getText());
        } else if (t.getCode() == KeyCode.ESCAPE) {
            cancelEdit();
        }
    });

    textField.focusedProperty().addListener((ObservableValue<? extends Boolean> ov, Boolean t0, Boolean t1) -> {
        if(!t1) 
             commitEdit(textField.getText());
    });
}

The updated string doesnot reflect when i click on some other tablecell. I am using Java 8b123.

Sudip Saha
  • 300
  • 2
  • 6
  • 21
  • By clicking on any other TabeCell the edit is discarded. Any idea why is that happening ? – Sudip Saha May 29 '14 at 02:39
  • The default behavior is for `cancelEdit()` to be called on a focus change. For a detailed description, see http://stackoverflow.com/a/25291137/132374. – Jon Onstott Oct 27 '15 at 21:09

1 Answers1

0

Instead of:

textField.focusedProperty().addListener((ObservableValue<? extends Boolean> ov, Boolean t0, Boolean t1) -> {
    if(!t1) 
         commitEdit(textField.getText());
});

Put :

getTableRow().focusedProperty().addListener((ObservableValue<? extends Boolean> ov, Boolean t0, Boolean t1) -> {
    if(!t1) 
         commitEdit(textField.getText());
});
  • Could you please elaborate more your answer adding a little more description about the solution you provide? – abarisone Apr 29 '15 at 08:30
  • By the code we just add a listener on the focus property of the row. That means if this property changes (in our case if we loose the focus on the row, by clicking somewhere else) we call the method "commitEdit" with the textarea value (getText()) – Trust'yce Apr 30 '15 at 14:56