Share the post "Force TAB Key In Java FX TextArea To Focus On Next Control And Ctrl+TAB To Insert TAB"
Here is code to have the TextArea control in Java FX move to the next control when the TAB key is pressed.
Somtimes we do not want tabs inside our TextArea control. Hence, when the TAB key is pressed, its natural action should be to move focus to the next available control.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
textarea.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() { @Override public void handle(KeyEvent ke) { if (ke.getCode() == KeyCode.TAB) { // traverse focus if pressing TAB and insert tab if pressing CONTROL+TAB SkinBase skin = (SkinBase) textarea.getSkin(); if (skin.getBehavior() instanceof TextAreaBehavior) { TextAreaBehavior behavior = (TextAreaBehavior) skin.getBehavior(); if (ke.isControlDown()) { behavior.callAction("InsertTab"); } else { behavior.callAction("TraverseNext"); } ke.consume(); } } } }); |
If CONTROL+TAB is pressed and the TextArea control has focus, a TAB character will be inserted within the control.