How do I make hitting the Tab Key in TextArea navigates to the next control ?
I could add a listener to cath de key pressed event, but how do I make te TextArea control to lose it focus (without knowing the next field in the chain to be focused) ?
@FXML protected void handleTabKeyTextArea(KeyEvent event) {
if (event.getCode() == KeyCode.TAB) {
...
}
}
If a different solution for the Tab - Focus problem. The default behaviour of the TextArea for the CTRL+TAB key is a move of focus to the next control. So I replaced the TAB key event with a CTRL+TAB key event, and when the user hits CTRL+TAB a tab character is inserted in the TextArea.
My question: is it OK to fire an event in the event filter? And is it OK to replace the text of the KeyEvent with the FOCUS_EVENT_TEXT, in order to have an indication if it is the an event generated by the user, or from the event created in the event filter.
The event filter:
The event handler:
I use the traverse-methods
This code traverse focus if pressing TAB and insert tab if pressing CONTROL+TAB
Inspired by the previous answers and for a very similar case, I built the following class:
I just have not seen the necessity of handling tab in the context of a TextField so I removed this part.
Then this class can be very easily used as described by User:
And the whole thing works like a charm :)
As of Java 9 (2017), most answers in this page don't work, since you can't do
skin.getBehavior()
anymore.This works:
This also works:
Both simulate pressing
CTRL+TAB
when the user pressesTAB
. The default behaviour of the TextArea forCTRL+TAB
is moving the focus to the next control. Please note the second code is based on Johan De Schutter's answer.I had the same issue and I like the traverse-methods that Tom uses. But I also want to insert a tab when ctrl+tab is pressed.
The call
doesn´t work with JavaFX8. A look in the TextAreaBehaviour class showed me that there now is a "TraverseOrInsertTab" action.
But however, I think this kind of action calling is quite unstable across several java versions because it relies on a string that is passed.
So instead of the callAction() method, I used