2014-01-22 46 views
1

如何讓我的ScrolledComposite滾動條在其頂部顯示指定的控件?使用ScrolledComposite的setOrigin方法

這是我之前關於how to make a ScrolledComposite programmatically scroll to a child control的一個問題的後續。這裏給出的建議給了我一些關於如何在概念上嘗試它的想法,比如使用setOrigin方法,但是我無法成功地既沒有給出例子,也沒有我自己的功能。這是我到目前爲止已經構建:

我做了一個外殼,看起來像這樣: enter image description here 我從1-500一堆標籤,和我有一堆特定的按鈕,我想導致ScrolledComposite居中其各自的標籤上,就像這樣:

enter image description here

下面是代碼:

import java.util.HashMap; 

public class ScrollableTest2 extends Shell { 

    private static final int NUM_OF_LABELS = 500; 
    private static final int[] SEEKABLES = new int[] {0, 9, 23, 99, 175, 176, 177, 178, 350, 495, 499}; 

    Map<Integer, Control> controlMap = new HashMap<Integer, Control>(); 

    private Composite cmpButtons; 
    private Label vr; 
    private ScrolledComposite scroller; 
    private Composite cmpScrollInner; 

    /** 
    * Launch the application. 
    * @param args 
    */ 
    public static void main(String args[]) { 
     try { 
      Display display = Display.getDefault(); 
      ScrollableTest2 shell = new ScrollableTest2(display); 
      shell.layout(); 
      shell.open(); 
      while (!shell.isDisposed()) { 
       if (!display.readAndDispatch()) { 
        display.sleep(); 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * Create the shell. 
    * @param display 
    */ 
    public ScrollableTest2(Display display) { 
     super(display, SWT.SHELL_TRIM); 
     createContents(); 
    } 

    /** 
    * Create contents of the shell. 
    */ 
    protected void createContents() { 
     setSize(400, 400); 
     setText("SWT Application"); 
     GridLayout gridLayout = new GridLayout(); 
     gridLayout.numColumns = 3; 
     setLayout(gridLayout); 

     cmpButtons = new Composite(this, SWT.NONE); 
     cmpButtons.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1)); 
     cmpButtons.setLayout(new GridLayout(1, false)); 

     for (int seekable : SEEKABLES) { 
      Button button = new Button(cmpButtons, SWT.NONE); 
      button.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); 
      button.setText("Go To " + String.valueOf(seekable+1)); 

      final int index = seekable; 
      button.addSelectionListener(new SelectionAdapter() { 
       @Override 
       public void widgetSelected(SelectionEvent e) { 
        seekToLabel(index); 
       } 
      }); 
     } 

     vr = new Label(this, SWT.SEPARATOR | SWT.VERTICAL); 
     vr.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1)); 

     scroller = new ScrolledComposite(this, SWT.BORDER | SWT.V_SCROLL); 
     scroller.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1)); 
     scroller.setExpandHorizontal(true); 
     scroller.setExpandVertical(true); 

     cmpScrollInner = new Composite(scroller, SWT.NONE); 
     cmpScrollInner.setLayout(new GridLayout(1, false)); 

     scroller.setContent(cmpScrollInner); 

     for (int i = 0; i < NUM_OF_LABELS; i++) { 
      Label label = new Label(cmpScrollInner, SWT.NONE); 
      label.setText("Label " + String.valueOf(i+1)); 
     } 

     scroller.setMinSize(cmpScrollInner.computeSize(SWT.DEFAULT, SWT.DEFAULT)); 
     scroller.setContent(cmpScrollInner); 
     scroller.addControlListener(new ControlAdapter() { 
      public void controlResized(ControlEvent e) { 
       Rectangle r = scroller.getClientArea(); 
       scroller.setMinSize(cmpScrollInner.computeSize(r.width, 
         SWT.DEFAULT)); 
      } 
     }); 

    } 

    @Override 
    protected void checkSubclass() { 
     // Disable the check that prevents subclassing of SWT components 
    } 

    protected void seekToLabel(int index) { 
     Control showCntrl = controlMap.get(new Integer(index)); 
     if(showCntrl != null){ 
      scroller.setOrigin(showCntrl.getLocation()); 
     } 
    } 

} 

似乎的按鈕不去做任何事情,儘管我相信我正確地設置了聽衆。那麼我必須假定我濫用了setOrigin命令。我錯過了什麼?

+1

我想出了自己的答案,這只是一個非常愚蠢的錯誤。 –

回答

1

Neeeeevermind!原來,我只是忘了將控件添加到控件的Map中。一旦我這樣做,它完美的作品。

for (int i = 0; i < NUM_OF_LABELS; i++) { 
     Label label = new Label(cmpScrollInner, SWT.NONE); 
     label.setText("Label " + String.valueOf(i+1)); 
     controlMap.put(new Integer(i), label); 
    } 

畢竟這是一個愚蠢的錯誤。

相關問題