Where I used to be able to get edits in a textfield in a table view to commit when the user changed focus outside the cell (e.g., by clicking on another cell, a button, etc.), this doesn't work any longer. Other than installing a Java update (1.8.0_51) I don't know what could have caused this…
Like all similar samples on the internet, the code I use is a simple changeListener to the focused property of the textfield:
textField.focusedProperty().addListener(
(ObservableValue<? extends Boolean> ov, Boolean oldVal, Boolean newVal) -> {
if (!newVal) {
commitEdit(textField.getText());
}
});
While the code still gets properly executed, the commit doesn't work. I did some digging and found that prior to the change listener being fired, the cell editing mode has already been turned off (cancelEdit has been fired). As a result the commitEdit doesn't do anything any longer.
To illustrate the (changed) behaviour you can basically use any sample code, for instance Hasan Kara's sample on GitHub: https://gist.github.com/haisi/0a82e17daf586c9bab52 (line 217 adds the focused property change listener)
Any ideas how to fix this???
While How to enable commit on focusLost for TableView/TreeTableView? indeed gives a possible fixes, these fixes hacks, very likely not to work when Java is updated. I need a working solution that is ready for production. I can't imagine there is no working solution possible without reverting to hacks (as many have indicated already, JavaFX not committing when you change focus on a cell is very bad behaviour indeed.) Furthermore, even Oracle's own examples make use of the focusChange listener, so there is clearly something broken in the more recent Java versions