ScrollledComposite在JFace对话框中没有滚动条

时间:2015-09-27 23:50:48

标签: java swt jface

我目前正在尝试扩展JFace对话框以包含scrolledComposite。我的代码目前看起来像这样:

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class MWE extends MessageDialog {

    public static void main(String[] args) {
        Dialog d = new MWE(new Display().getActiveShell());
        d.open();
    }

    public MWE(Shell parentShell) {
        super(parentShell, "MWE", null, "",
                MessageDialog.ERROR, new String[] {  "OK" }, 0);
    }

    @Override
    public Control createDialogArea(Composite parent) {
        ScrolledComposite sc = new ScrolledComposite(parent, SWT.V_SCROLL);
        sc.setExpandHorizontal(true);
        sc.setExpandVertical(true);
        sc.setMinSize(300, 300);
        Composite content = new Composite(sc, SWT.NONE);
        sc.setContent(content);
        content.setLayout(new GridLayout());
        for (int i = 0; i < 100; i++) {
            Label l = new Label(content, SWT.NONE);
            l.setText("lorem ipsum");
        }
        return parent;
    }
}

问题是,像这样窗口的大小只是跨越整个屏幕高度,而不是如果超过一定大小就可滚动。如果内容超出大小,我如何滚动?

1 个答案:

答案 0 :(得分:2)

heightHint

的布局数据中设置ScrolledComposite
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
data.heightHint = 500;
sc.setLayoutData(data);

使用最小尺寸的计算内容大小:

sc.setMinSize(content.computeSize(SWT.DEFAULT, SWT.DEFAULT));

(将所有内容添加到content后)执行此操作