Swt FormToolkit焦点问题

发布于 2024-12-14 05:46:34 字数 1062 浏览 2 评论 0原文

我有一个奇怪的问题:有一个带有复合材料的SectionPart,它是从FormToolkit#createComposite(getSection()) 创建的。 Composite 包含一定数量的小部件,这些小部件垂直放置在另一个部件下面(如通常的形式)。当光标位于某个小部件内时,假设输入字段,我在空白区域的两个字段之间单击鼠标右键,然后焦点会自动跳转到该组合中的第一个字段。

我尝试将 SWT.NO_FOCUS 样式位设置为表单中的第一个小部件(通常它是 TableComboViewer),但它没有帮助(看起来,该位没有在 TableComboViewer 内部的 TableCombo 上设置)。

那么,有没有人遇到过类似的事情,或者有没有解决这个问题的方法,或者有任何线索可能是什么?

Upd1:设置 NO_FOCUS 样式有助于非 TableComboViewer 小部件(在这种情况下它们没有接收焦点)。如果 TableComboViewer TableCombo 小部件包含接收焦点的文本小部件,但即使我添加 NO_FOCUS 位,它也不会应用于文本样式。我检查了 TableCombo 的源代码,有一个方法 checkStyle,它执行以下操作:

private static int checkStyle (int style) {
 int mask = SWT.BORDER | SWT.READ_ONLY | SWT.FLAT | SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT;
 return SWT.NO_FOCUS | (style & mask);
}

我实际上不确定它的作用,因为我不太擅长按位运算,但似乎这就是问题所在,为什么我可以' t 设置 NO_FOCUS 标志。

但我不明白,为什么当我单击“Composite”时,它会尝试在其子项上设置“foxus”,我可以以某种方式抑制这种情况吗?

Upd2:原因大概找到了,据说是:

当视图被激活时,焦点将转移到表单,表单将其传递到第一个能够接受焦点的控件,即本例中的链接。

看来,这是不可能禁止的。

提前致谢, 亚历克斯·G

I have a strange issue: there is a SectionPart with composite, which is create from FormToolkit#createComposite(getSection()). Composite contains some number of widgets, which are positioned vertically one under other (as in a usual form). When the cursor is inside some widget, let's say input filed and I am clicking right between two fields on empty space, then focus automatically jumps to the first field in this composite.

I've tried to set SWT.NO_FOCUS style bit to the first widget in the form (usually it is a TableComboViewer) but it didn't helped (it seems, that this bit is not set on TableCombo, which is inside TableComboViewer).

So, have anybody faced something similar, or are there any workarounds for this problem or any clues what could it be?

Upd1: setting NO_FOCUS style helps for non TableComboViewer widgets (in this case they are not receiving focus). In case of TableComboViewer TableCombo widget contains Text widget, which receives focus, but even, if I add NO_FOCUS bit, it is not applied to Text style. I've checked source of TableCombo and there is a method checkStyle, which does following:

private static int checkStyle (int style) {
 int mask = SWT.BORDER | SWT.READ_ONLY | SWT.FLAT | SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT;
 return SWT.NO_FOCUS | (style & mask);
}

I am not actually sure what it does, cause I am not really good in bitwise operation, but seems, that this is the problem, why I can't set NO_FOCUS flag.

I don't understand though, why when I am clicking on Composite, it tries to set foxus on it's children, can I somehow suppress this?

Upd2: The reason is probably found, it is said, that:

When the view is activated, focus is transferred to the form, which passes it to the first control capable of accepting focus, our link in this case.

And it seems, that it is not possible to forbid this.

Thanks in advance,
AlexG

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌若浮生 2024-12-21 05:46:34

您的问题在于 Composite.setFocus().. 看看这个:

public boolean setFocus () {
    checkWidget ();
    Control [] children = _getChildren ();
    for (int i= 0; i < children.length; i++) {
        if (children [i].setFocus ()) return true;
    }
    return super.setFocus ();
} 

如您所见,这将尝试将焦点设置在组合中允许焦点的第一个控件上。 ..

[编辑 - 添加以下内容是为了澄清...]

如果不是所有 Composites 上安装的 MouseListener,上述方法不会有问题> 在FormToolkit.adapt(Composite composite)

    public void adapt(Composite composite) {
        composite.setBackground(colors.getBackground());
        composite.addMouseListener(new MouseAdapter() {
            public void mouseDown(MouseEvent e) {
                ((Control) e.widget).setFocus();
            }
        });
        if (composite.getParent() != null)
            composite.setMenu(composite.getParent().getMenu());
    }

我已经多次解决了这个问题,方法是在子类中使用自己的 FormToolkit.adapt(Composite composite)正确的事情 - 我只是将 setFocus()forceFocus() 交换。虽然这有时会给你带来其他问题......

You problem lies in Composite.setFocus().. have a look at this:

public boolean setFocus () {
    checkWidget ();
    Control [] children = _getChildren ();
    for (int i= 0; i < children.length; i++) {
        if (children [i].setFocus ()) return true;
    }
    return super.setFocus ();
} 

As you can see, this will try to set the focus on the first control in the composite that will allow for the focus...

[EDIT - the following is added to clarify...]

The above method would not be a problem if it wasn't for the MouseListener that is installed on all Composites in FormToolkit.adapt(Composite composite):

    public void adapt(Composite composite) {
        composite.setBackground(colors.getBackground());
        composite.addMouseListener(new MouseAdapter() {
            public void mouseDown(MouseEvent e) {
                ((Control) e.widget).setFocus();
            }
        });
        if (composite.getParent() != null)
            composite.setMenu(composite.getParent().getMenu());
    }

I have solved this problem on a number of occasions by having my own FormToolkit.adapt(Composite composite) in a sub-class that does the right thing - I just exchange setFocus() with forceFocus(). Though that can occasionally give you other problems...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文