Simon Eismann
Simon Eismann

Reputation: 293

ScrolledComposite no scrollbar in JFace dialog

I am currently trying to extend a JFace dialog to contain a scrolledComposite. My code currently looks like this:

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;
    }
}

The problem is, that like this the size of the window just spans the full screen height, instead of being scrollable if it goes above a certain size. How do i get to scroll if the content exceeds the size?

Upvotes: 0

Views: 1293

Answers (1)

greg-449
greg-449

Reputation: 111216

Set a heightHint in the layout data for the ScrolledComposite:

GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
data.heightHint = 500;
sc.setLayoutData(data);

Use the computed size of the contents for the minimum size:

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

(do this after all the content has been added to content)

Upvotes: 3

Related Questions