I learned a little about JavaFX 2, but I ran into a problem with editable table cells, and I also can not find a solution. To save the insert in a lot of code, I 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 this code, you see the following:
- Clicking a row selects that row.
- Clicking on a cell in the selected row converts the cell into a text field so that it is ready for editing.
- Clicking in the text box allows you to edit the content.
The problem is that 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 the focus created (for example, step 2) directly switches to the text field.
My best guess on how to achieve this was to add textField.requestFocus()
to the end of the startEditing()
method:
@Override public void startEdit() { super.startEdit(); if (textField == null) { createTextField(); } setGraphic(textField); setContentDisplay(ContentDisplay.GRAPHIC_ONLY); textField.selectAll(); textField.requestFocus(); }
but it does not work. If I add a focus listener to the text box, we find that it receives focus, but then loses it again. The interruption of the breakpoint in the listener seems to indicate that the TableView is paying attention before the text field is displayed.
The question is, how can I focus on the text field when starting editing?
source share