如何防止ScrolledComposite无休止地垂直扩展

时间:2014-10-22 01:44:00

标签: java eclipse-plugin swt

我有一个包含以下代码的对话框。滚动条出现,但不会滚动到结尾。最后的内容不会出现太久了。即使将对话框重新调整为小,也无法看到最终的内容。请建议如何使滚动条适用于以下场景。

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

在第3张图像中,最后一个控件不可见,ok和cancel按钮也不可见。我需要滚动条向下,以便我可以看到所有内容。请建议如何做到这一点

1 个答案:

答案 0 :(得分: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/