2012-09-14 33 views
2

我有四個合成按順序排列。在複合容器上啓用焦點偵聽器

每個組合都有一個複選框,標籤和2個按鈕。現在這些複合材料相繼排列。

我想要關注這些項目,即當我使用標籤從一個複合材料到另一個複合材料時,當前複合材料應該看起來突出顯示。理想情況下,我希望它表現得像一個列表,當你選擇一個項目,然後突出顯示。這可能嗎?

我知道組​​合充當其他小部件,控件的容器。我的要求是我有5個條目的列表,並且列表中的每個項目都有一個複選框,標籤和兩個按鈕。我也希望它在選擇時專注於它。

另外,請讓我知道替代解決方案爲我在上面描述的相同的用戶界面。

+0

能否請您提供一個[SSCCE(http://www.sscce.org),以幫助我們瞭解你的挑戰? – Baz

回答

1

要使標籤從複合材料變爲複合材料,請將每個複合材料的標籤列表設置爲一個控制器,您希望在標籤後對其進行聚焦。例如,複選框:

composite.setTabList(new Control[]{checkButton}); 

要突出顯示,您的想象力就是極限。您可以更改背景,添加一些邊框,並將其命名。只要組合中的某個控件獲得焦點,您就必須更新它。

這是一個完整的例子:

public static void main(String[] args) { 
    Display display = new Display(); 
    Shell shell = new Shell(display); 
    shell.setLayout(new FillLayout(SWT.VERTICAL)); 

    createElement(shell); 
    createElement(shell); 
    createElement(shell); 
    createElement(shell); 

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

private static void createElement(final Composite parent) { 
    final Composite composite = new Composite(parent, SWT.BORDER); 
    composite.setLayout(new GridLayout(4, false)); 
    final Button checkButton = new Button(composite, SWT.CHECK); 
    new Label(composite, SWT.NONE); 
    final Button button1 = new Button(composite, SWT.PUSH); 
    final Button button2 = new Button(composite, SWT.PUSH); 
    Listener listener = new Listener() { 
     @Override 
     public void handleEvent(Event event) { 
      for (Control control : parent.getChildren()) { 
       control.setBackground(null); 
      } 
      composite.setBackground(composite.getDisplay().getSystemColor(SWT.COLOR_RED)); 
      if (event.widget == button1 || event.widget == button2) { 
       checkButton.setFocus(); 
      } 
     } 
    }; 
    checkButton.addListener(SWT.FocusIn, listener); 
    button1.addListener(SWT.FocusIn, listener); 
    button2.addListener(SWT.FocusIn, listener); 
    composite.setTabList(new Control[]{checkButton}); 
}