JavaFX TextArea中的Tab键导航 [英] Tab key navigation in JavaFX TextArea

查看:279
本文介绍了JavaFX TextArea中的Tab键导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在TextArea中按Tab键导航到下一个控件?

How do I make hitting the Tab Key in TextArea navigates to the next control ?

我可以为cath de key按下事件添加一个监听器,但我该怎么做make te TextArea控件失去焦点(不知道要聚焦的链中的下一个字段)?

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) {
        ...
    }
}


推荐答案

如果按TAB键,此代码遍历焦点,如果按CONTROL + TAB

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

textArea.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
        @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();
                }

            }
        }
    });

这篇关于JavaFX TextArea中的Tab键导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆