Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a view scrollable in an Eclipse RCP application?

What I tried so far:

In createPartControl:

ScrolledComposite sc = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL);
        sc.setLayoutData(new GridData(GridData.FILL_BOTH));
        sc.setExpandVertical(true);
        sc.setExpandHorizontal(true);
        sc.setSize(ApplicationWorkbenchWindowAdvisor.WIDTH, ApplicationWorkbenchWindowAdvisor.HEIGHT);
        final TabFolder tabFolder = new TabFolder(sc, SWT.TOP);

but this does not work. My problem is that if I resize my program window the scrollbars does not appear in my view. Any ideas?

like image 652
Adam Arold Avatar asked Oct 17 '11 09:10

Adam Arold


2 Answers

Typically in Eclipse views, I want my controls to grab all available space, and only show scrollbars, if otherwise a control would shrink below a usable size.

The other answers are perfectly valid, but I wanted to add a full example of a createPartControl method (Eclipse e4).

@PostConstruct
public void createPartControl(Composite parent) {
    ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
    Composite composite = new Composite(sc, SWT.NONE);
    sc.setContent(composite);

    composite.setLayout(new GridLayout(2, false));

    Label label = new Label(composite, SWT.NONE);
    label.setText("Foo");

    Text text = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
    GridDataFactory.fillDefaults().grab(true, true).hint(400, 400).applyTo(text);

    sc.setExpandHorizontal(true);
    sc.setExpandVertical(true);
    sc.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}

Note that .fillDefaults() implies .align(SWT.FILL, SWT.FILL).

I commonly use this pattern, so I created the following little helper method:

public static ScrolledComposite createScrollable(Composite parent, Consumer<Composite> scrollableContentCreator) {
    ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
    Composite composite = new Composite(sc, SWT.NONE);
    sc.setContent(composite);

    scrollableContentCreator.accept(composite);

    sc.setExpandHorizontal(true);
    sc.setExpandVertical(true);
    sc.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    return sc;
}

Thanks to Java 8 lambdas, you can now implement new scrollable composites in a very compact way:

    createScrollable(container, composite -> {
        composite.setLayout(new FillLayout());
        // fill composite with controls
    });
like image 100
Max Hohenegger Avatar answered Sep 22 '22 10:09

Max Hohenegger


The Javadoc of ScrolledComposite describes the two ways to use it, including example code. To sum them up:

  1. You either set the size of the control/composite contained in your ScrolledComposite on the control/composite itself
  2. Or you tell your ScrolledComposite the minimum size to use for its content.

Currently, you're doing neither. You're setting the size on the ScrolledComposite, but unless you don't use a layout manager, that doesn't make much sense. In any case, see the above link for some official example code.

like image 27
Frettman Avatar answered Sep 20 '22 10:09

Frettman