Tab key navigation in JavaFX TextArea

后端 未结 6 1435
暖寄归人
暖寄归人 2021-02-02 16:47

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

6条回答
  •  一生所求
    2021-02-02 17:30

    This code traverse focus if pressing TAB and insert tab if pressing CONTROL+TAB

    textArea.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler() {
            @Override
            public void handle(KeyEvent event) {
                if (event.getCode() == KeyCode.TAB) {
                    SkinBase skin = (SkinBase) textArea.getSkin();
                    if (skin.getBehavior() instanceof TextAreaBehavior) {
                        TextAreaBehavior behavior = (TextAreaBehavior) skin.getBehavior();
                        if (event.isControlDown()) {
                            behavior.callAction("InsertTab");
                        } else {
                            behavior.callAction("TraverseNext");
                        }
                        event.consume();
                    }
    
                }
            }
        });
    

提交回复
热议问题