SWT中的可聚焦复合材料

时间:2012-10-16 21:36:15

标签: eclipse swt

我们有一个自定义控件,它本质上是一个带有标签和按钮的复合材料。目前,当用户按下“Tab”时,焦点将转到按钮上。

如何让复合材料获得焦点并将按钮从焦点中排除?例如。用户应该能够选中所有自定义控件,而不是停在按钮上。

更新:我们的控件树如下所示:

  • 主窗格
    • CustomPanel1
      • 标签
      • 按钮
    • CustomPanel2
      • 标签
      • 按钮
    • CustomPanel3
      • 标签
      • 按钮

所有CustomPanel都属于同一个Composite子类。我们需要的是选项卡在这些面板之间循环而不是“看到”按钮(这些是唯一可聚焦的组件)

1 个答案:

答案 0 :(得分:2)

您可以使用Composite#setTabList(Control[])定义Composite的Tab键顺序。

以下是一个小示例,它会在Button onethree之间切换,忽略Button s twofour

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));

    Composite content = new Composite(shell, SWT.NONE);
    content.setLayout(new GridLayout(2, true));
    content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    final Button one = new Button(content, SWT.PUSH);
    one.setText("One");

    final Button two = new Button(content, SWT.PUSH);
    two.setText("Two");

    final Button three = new Button(content, SWT.PUSH);
    three.setText("Three");

    final Button four = new Button(content, SWT.PUSH);
    four.setText("Four");

    Control[] controls = new Control[] {one, three};

    content.setTabList(controls);

    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

编辑:可以轻松转换上面的代码以满足您的要求。我自己无法测试,因为Composite s不能集中精力,但你应该明白这个想法:

mainPane.setTabList(new Control[] {customPanel1, customPanel2, customPanel3 });

customPanel1.setTabList(new Control[] {});
customPanel2.setTabList(new Control[] {});
customPanel3.setTabList(new Control[] {});
相关问题