2012-10-16 49 views
1

我们有一个自定义控件,基本上是一个带有标签和按钮的复合材料。目前当用户按下“Tab”时,焦点进入按钮。SWT中的可对焦复合材料

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

更新时间:我们的控件树是这个样子:

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

所有CustomPanel的是相同的复合子类。我们需要的是让选项卡在这些面板之间循环,并且不要“看见”按钮(这些是唯一可调焦的组件)

+0

这种方法的好处是什么?当用户选择下一个“Composite”时,他/她能够做什么而不关注“Widget”? – Baz

+0

@Baz我们将在另一个组件中显示一些数据并接受键盘输入。 – Eugene

回答

2

您可以使用Composite#setTabList(Control[])定义Composite的选项卡顺序。

这里是将在Button小号onethree之间标签忽略Button小号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(); 
} 

EDIT:上述代码可以很容易地被转换成适合您的要求。我自己无法测试,因为Composite s不是专注的,但你应该明白:

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

customPanel1.setTabList(new Control[] {}); 
customPanel2.setTabList(new Control[] {}); 
customPanel3.setTabList(new Control[] {}); 
+0

请参阅我的更新 - 希望这将清除我们的要求。 – Eugene

+0

@Eugene有用吗? – Baz

+0

谢谢。最后,我已经回到了这个模块,并且能够使用这个功能 - 基本上,在我的复合材料中重写setFocus时,我需要非常小心。 – Eugene