【问题标题】:ScrolledComposite no scrollbar in JFace dialogJFace 对话框中的 ScrolledComposite 没有滚动条
【发布时间】:2015-09-27 23:50:47
【问题描述】:

我目前正在尝试扩展 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;
    }
}

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

【问题讨论】:

    标签: java swt jface


    【解决方案1】:

    ScrolledComposite的布局数据中设置heightHint

    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 后执行此操作)

    【讨论】:

    • 这个解决方案使内容不超过500的高度,但不幸的是没有出现滚动条:-(
    • 添加了 setMinSize 要求
    猜你喜欢
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    相关资源
    最近更新 更多