I've been learning a bit about JavaFX 2 but I've hit a problem with editable table cells that I can't find a solution too. To save pasting in a lot of code I've created a project that contains only the TableView example from the JavaFX documentation: http://docs.oracle.com/javafx/2/ui_controls/table-view.htm
If you run up this code you see this behaviour:
The problem is that's three clicks to get to the point where the cell can be edited. What I would like to achieve is when the text field in created (e.g. step 2) focus switches to the text field directly.
My best guess regarding how to achieve this was to add textField.requestFocus()
to the end of the the startEditing()
method:
@Override
public void startEdit() {
super.startEdit();
if (textField == null) {
createTextField();
}
setGraphic(textField);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
textField.selectAll();
textField.requestFocus();
}
but this doesn't work. If I add a focus listener to the text field what I find is that it gets focus but then looses it again. Sticking a break point in the listener seems to indicate that the TableView is taking focus back before the text field is displayed.
The question therefore is how do I give focus to the text field when editing starts?
Could you please replace the line textField.requestFocus();
with:
Platform.runLater(new Runnable() {
@Override
public void run() {
textField.requestFocus();
}
});
and try again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With