如何将键盘焦点限制在节点内的控件上? [英] How do I restrict keyboard focus to controls within a Node?

查看:59
本文介绍了如何将键盘焦点限制在节点内的控件上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一种可重用的方法,用于将节点显示为轻量级对话框.将该节点添加到StackPane的顶部,然后使背景模糊.有两个问题:

I'm trying to implement a reusable method for displaying a node as a lightweight dialog. The node is added to the top of a StackPane while the background is then blurred. There are 2 problems:

1)堆栈窗格的后台节点中的控件仍然能够接收焦点.

1) Controls in the background node of the stackpane are still able to receive focus.

2)我如何关注顶级节点.我知道Node上有一个requestFocus方法,但是我需要将其提供给嵌套在Node内的控件.由于这是一种可重用的方法,因此我无法直接引用控件.

2) How do I give focus to the top-level node. I know there is a requestFocus method on Node, but I need to give it to a control nested within the node. Since this is intended to be a reusable method, I can't reference the controls directly.

(如果我可以通过找到现有的实施方式来回避整个问题,那将是最好的选择,但是我还没有找到第三者的解决方案)

(if I can sidestep the whole problem by finding an existing implementation, that would be best, but I haven't found a 3rd party solution yet)

谢谢

推荐答案

针对:
1)3个替代建议,
a-将一个键事件处理程序添加到对话框窗格中,以捕获 Tab 键的按下情况

For:
1) 3 alternative suggestions,
a- Add a key event handler to the dialogbox pane to catch the Tab key pressings

dialogbox.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {

    @Override
    public void handle(javafx.scene.input.KeyEvent event) {
        if (event.getCode() == KeyCode.TAB) {
            System.out.println("TAB pressed");
            event.consume(); // do nothing
        }
    }
});

b-暂时禁用主StackPane的所有子项,但最后添加的对话框子项除外.

b- Temporarily disable all children of the main StackPane except the lastly added dialogbox child.

// Assuming dialogbox is at last in the children list
for(int i=0; i<mainPane.getChildren().size()-1; i++){
     // Disabling will propagate to sub children recursively
    mainPane.getChildren().get(i).setDisable(true);
}

c- b 相同,但通过node.setFocusTraversable(false)(点球)手动禁用对控件的聚焦.当然,这不是您的选择.

c- Same as b but manually disable focusing to controls by node.setFocusTraversable(false) (spot kick). Surely this will not be your choice..

2)告诉对话框显示对话框后,哪个节点应该成为焦点(通过构造函数或setter方法).

2) Tell the dialogbox which node should take the focus after the dialog is shown (through constructor or setter method).

// init dialogbox and show it then
dialogbox.setFocusTo(node_in_dialogbox);

这篇关于如何将键盘焦点限制在节点内的控件上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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