2013-08-16 100 views
0

使用SWT ScrollableComposite,是否有一種簡單的方法來設置滾動條的位置以使特定元素位於頂部?將ScrollableComposite位置設置爲位置

舉例來說,如果我有這樣充滿26個標籤,以便下去與英文字母的複合材料:

enter image description here

...那麼,說我要定我的觀點到「J」的標籤,並有這樣設置滾動條的位置:

enter image description here

(這是唯一的例子 - 如果我真的想要做什麼,我描述這裏,我將清除LY只是用了我的信,一個列表框或表來代替。)

這類似於跳轉到網頁中的特定標籤時,互聯網瀏覽器是如何工作的。

如果需要,這可能會用一堆手動測量計算來完成,但我希望有一些更簡單的存在。

回答

2

我相信你正在尋找方法,下面就ScrolledComposite

org.eclipse.swt.custom.ScrolledComposite.showControl(Control) //make it visible in view port 

    org.eclipse.swt.custom.ScrolledComposite.setOrigin(Point) //sets left corner coordinates, read SWT docs 

更新答:

public static void main(String[] args) { 
    Display display = new Display(); 

    Shell shell = new Shell(display); 
    shell.setLayout(new FillLayout()); 
    Map<String,Control> controlMap = new HashMap<String,Control>(); 
    final ScrolledComposite scrollComposite = new ScrolledComposite(shell, 
     SWT.V_SCROLL | SWT.BORDER); 

    final Composite parent = new Composite(scrollComposite, SWT.NONE); 
    for (int i = 0; i <= 50; i++) { 
     Label label = new Label(parent, SWT.NONE); 
     String index = String.valueOf(i); 
     controlMap.put(index, label); 
     label.setText(index); 
    } 
    GridLayoutFactory.fillDefaults().numColumns(1).applyTo(parent); 

    scrollComposite.setContent(parent); 
    scrollComposite.setExpandVertical(true); 
    scrollComposite.setExpandHorizontal(true); 
    scrollComposite.addControlListener(new ControlAdapter() { 
     public void controlResized(ControlEvent e) { 
     Rectangle r = scrollComposite.getClientArea(); 
     scrollComposite.setMinSize(parent.computeSize(r.width, 
      SWT.DEFAULT)); 
     } 
    }); 

    shell.open(); 

    Control showCntrl = controlMap.get(String.valueOf(5)); 
    if(showCntrl != null){ 
     scrollComposite.setOrigin(showCntrl.getLocation()); 
    } 
    while (!shell.isDisposed()) { 
     if (!display.readAndDispatch()) { 
     display.sleep(); 
     } 
    } 
    display.dispose(); 
    } 
+0

我還沒有得到這個工作有多少運氣。這可能是由於我在ScrolledComposite中設置了控件的不尋常方式。你能否提供一個簡單的例子來幫助我理解如何設置控件以實現功能? –

+0

嗨,我剛剛更新了示例代碼。它應該工作。 –