Should I set ScrolledComposite content twice? How to use ScrolledComposite correctly?

256 views Asked by At

How to use ScrolledComposite correctly?

The following is slightly modified Snipped166:

 import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet166 {

public static void main(String[] args) {
    Display display = new Display();
    Image image1 = display.getSystemImage(SWT.ICON_WORKING);
    Image image2 = display.getSystemImage(SWT.ICON_QUESTION);
    Image image3 = display.getSystemImage(SWT.ICON_ERROR);

    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    final ScrolledComposite scrollComposite = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.BORDER);

    final Composite parent = new Composite(scrollComposite, SWT.NONE);
    for(int i = 0; i <= 50; i++) {
        Label label = new Label(parent, SWT.NONE);
        if (i % 3 == 0) label.setImage(image1);
        if (i % 3 == 1) label.setImage(image2);
        if (i % 3 == 2) label.setImage(image3);
    }
    RowLayout layout = new RowLayout(SWT.HORIZONTAL);
    layout.wrap = false;
    parent.setLayout(layout);

    scrollComposite.setContent(parent);
    scrollComposite.setExpandVertical(true);
    scrollComposite.setExpandHorizontal(true);
    scrollComposite.addControlListener(new ControlAdapter() {
        @Override
        public void controlResized(ControlEvent e) {
            Rectangle r = scrollComposite.getClientArea();
            scrollComposite.setMinSize(parent.computeSize(r.width, SWT.DEFAULT));
        }
    });

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}
}

I would like it to display long horizontal row of signs with horizontal scroll bars.

Unfortunately it doesn't draw scroll bars.

Also I don't understand, why this example should be so complex? Why can't I just put something big onto ScrolledComposite and roll?

Also I don't understand the need of setContent() method since content is always set in constructor in SWT.

UPDATE

I found, that I can set size manually and scrolls will appear then.

public class Snippet166_mod01 {

public static void main(String[] args) {
    Display display = new Display();
    Image image1 = display.getSystemImage(SWT.ICON_WORKING);
    Image image2 = display.getSystemImage(SWT.ICON_QUESTION);
    Image image3 = display.getSystemImage(SWT.ICON_ERROR);

    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    final ScrolledComposite scrollComposite = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.BORDER);

    final Composite parent = new Composite(scrollComposite, SWT.NONE);
    for(int i = 0; i <= 50; i++) {
        Label label = new Label(parent, SWT.NONE);
        if (i % 3 == 0) label.setImage(image1);
        if (i % 3 == 1) label.setImage(image2);
        if (i % 3 == 2) label.setImage(image3);
    }
    RowLayout layout = new RowLayout(SWT.HORIZONTAL);
    layout.wrap = false;
    parent.setLayout(layout);

    // how to calculate actual size?
    parent.setSize(1000, 100);


    // what this is for?
    scrollComposite.setContent(parent);

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}
}

So, I wonder, how to set size automatically? Is it possible to set the size of parent control exactly to the width of 51 images inside?

1

There are 1 answers

0
Baz On

You also have to tell the ScrolledComposite its min-size:

scrolled.setMinSize(group.computeSize(SWT.DEFAULT, SWT.DEFAULT));
scrolled.setExpandHorizontal(true);
scrolled.setExpandVertical(true);

Do this after adding all your children to the group.