2014-10-22 33 views
2

我有一個對話框,下面的代碼。滾動條出現,但不會滾動到結尾。最終的內容不會出現太長。即使對話框被重新調整爲小型,也無法看到最後的內容。請建議如何讓滾動條適用於下面的場景。如何防止ScrolledComposite垂直無限擴大

public class SampleDialog extends TrayDialog { 

public SampleDialog(final Shell shell) { 
    super(shell); 
    this.shell = shell; 

} 


@Override 
public void create() { 
    super.create(); 

} 

@Override 
protected Control createDialogArea(final Composite parent) { 
    final GridLayout layout = new GridLayout(); 
    layout.numColumns = 1; 
    parent.setLayout(layout); 

    final ScrolledComposite sc1 = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.BORDER); 
     sc1.setExpandHorizontal(true); 
     sc1.setExpandVertical(true); 
     //sc1.setMinWidth(0); 

     final Composite composite = new Composite(sc1, SWT.NONE); 
     final GridLayout gridLayout = new GridLayout(); 
     gridLayout.numColumns = 2; 
     composite.setLayout(gridLayout); 

     sc1.setContent(composite); 

     //this.sc1.setMinSize(parent.computeSize(SWT.DEFAULT, SWT.DEFAULT)); 
     //this.sc1.setMinHeight(50); 
     //this.sc1.setAlwaysShowScrollBars(true); 

     sc1.addControlListener(new ControlAdapter() { 
      @Override 
      public void controlResized(final ControlEvent e) { 
       final Rectangle r = sc1.getClientArea(); 
       sc1.setMinSize(parent.computeSize(r.width, SWT.DEFAULT)); 
      } 
     }); 
    final GridData gridDataLabel = new GridData(); 
      final Label label = new Label(composite, SWT.NONE); 
      label.setText("Testing"); 
      label.setLayoutData(gridDataLabel); 
    for (i = 0 ; i <= n ; i++) {  // n can be 1 to any number -- decided at run time 
     createSampleCombo(composite); 
    } 
} 

public void createSampleCombo(Composite parent) { 
final Composite composite = new Composite(parent, SWT.NONE); 
     final GridLayout layout = new GridLayout(); 
     layout.numColumns = 3; 
     composite.setLayout(layout); 

     final GridData gridDataCombo = new GridData(); 
     gridDataCombo.horizontalAlignment = GridData.FILL; 
     gridDataCombo.grabExcessHorizontalSpace = true; 

     final Combo myCombo = new Combo(composite, SWT.NONE); 
     myCombo.setLayoutData(gridDataCombo); 
     gridDataCombo.minimumWidth = 500; 

     final GridData gridDataButton = new GridData(); 
     final Button browse = new Button(composite, SWT.PUSH); 
     browse.setText("Browse"); 
     browse.setLayoutData(gridDataButton); 
} 
} 

where: 
org.eclipse.jface.dialogs.TrayDialog; 
org.eclipse.swt.layout.GridLayout; 

when n = 10 the dialog looks like this

when n = 20 the dialog, scroll bar is on top

when n = 20 the dialog, and scroll bar is pulled down

第三圖像中

,最後的控制是不可見的,確定和取消按鈕也是不可見的。我需要滾動條向下,這樣我才能看到所有的內容。請建議如何可以做到這一點

回答

1

我最近剛做了一個類似的問題,我解決了它這種方式:首先,你應該避免ScrolledComposite從豎直擴展:

sc1.setExpandVertical(false); 

如果再resizeListener更改爲:

Rectangle clientArea = scrolledComposite.getClientArea(); 
Point minSize = scrolledComposite.getContent().computeSize(clientArea.width, SWT.DEFAULT); 
scrolledComposite.getContent().setSize(minSize); 

ScrolledComposite應該只從其容器的佈局中獲取允許的空間。

更多關於如何使用ScrolledComposites可以在這裏找到: http://www.codeaffine.com/2016/03/01/swt-scrolledcomposite/

+0

我改變了代碼,你建議並添加以下行(我錯過了),它經歷了! composite.setSize(400,400);謝謝!! – Krishnaveni 2014-10-22 07:26:58