Southpaw Hare
Southpaw Hare

Reputation: 1593

Using setOrigin method of ScrolledComposite

How can I make my ScrolledComposite scroll to show a specified control within it at the top?

This is a followup to a previous question of mine about how to make a ScrolledComposite programmatically scroll to a child control. The advice given there gave me some ideas on how to attempt it conceptually, such as using the setOrigin method, but I could not successfully get neither the given example nor my own to quite function. Here is what I've constructed so far:

I have made a shell that looks like this: enter image description here I have a bunch of labels from 1-500, and I have a bunch of specific buttons that I want to cause the ScrolledComposite to center on its respective label, like so:

enter image description here

Here is the code:

import java.util.HashMap;

public class ScrollableTest2 extends Shell {

    private static final int NUM_OF_LABELS = 500;
    private static final int[] SEEKABLES = new int[] {0, 9, 23, 99, 175, 176, 177, 178, 350, 495, 499};

    Map<Integer, Control> controlMap = new HashMap<Integer, Control>();

    private Composite cmpButtons;
    private Label vr;
    private ScrolledComposite scroller;
    private Composite cmpScrollInner;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String args[]) {
        try {
            Display display = Display.getDefault();
            ScrollableTest2 shell = new ScrollableTest2(display);
            shell.layout();
            shell.open();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the shell.
     * @param display
     */
    public ScrollableTest2(Display display) {
        super(display, SWT.SHELL_TRIM);
        createContents();
    }

    /**
     * Create contents of the shell.
     */
    protected void createContents() {
        setSize(400, 400);
        setText("SWT Application");
        GridLayout gridLayout = new GridLayout();
        gridLayout.numColumns = 3;
        setLayout(gridLayout);

        cmpButtons = new Composite(this, SWT.NONE);
        cmpButtons.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
        cmpButtons.setLayout(new GridLayout(1, false));

        for (int seekable : SEEKABLES) {
            Button button = new Button(cmpButtons, SWT.NONE);
            button.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
            button.setText("Go To " + String.valueOf(seekable+1));

            final int index = seekable;
            button.addSelectionListener(new SelectionAdapter() {
                @Override
                public void widgetSelected(SelectionEvent e) {
                    seekToLabel(index);
                }
            });
        }

        vr = new Label(this, SWT.SEPARATOR | SWT.VERTICAL);
        vr.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1));

        scroller = new ScrolledComposite(this, SWT.BORDER | SWT.V_SCROLL);
        scroller.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        scroller.setExpandHorizontal(true);
        scroller.setExpandVertical(true);

        cmpScrollInner = new Composite(scroller, SWT.NONE);
        cmpScrollInner.setLayout(new GridLayout(1, false));

        scroller.setContent(cmpScrollInner);

        for (int i = 0; i < NUM_OF_LABELS; i++) {
            Label label = new Label(cmpScrollInner, SWT.NONE);
            label.setText("Label " + String.valueOf(i+1));
        }

        scroller.setMinSize(cmpScrollInner.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        scroller.setContent(cmpScrollInner);
        scroller.addControlListener(new ControlAdapter() {
            public void controlResized(ControlEvent e) {
                Rectangle r = scroller.getClientArea();
                scroller.setMinSize(cmpScrollInner.computeSize(r.width,
                        SWT.DEFAULT));
            }
        });

    }

    @Override
    protected void checkSubclass() {
        // Disable the check that prevents subclassing of SWT components
    }

    protected void seekToLabel(int index) {
        Control showCntrl = controlMap.get(new Integer(index));
        if(showCntrl != null){
            scroller.setOrigin(showCntrl.getLocation());
        }
    }

}

The buttons do not seem to do anything at all, even though I believe that I am setting up the listeners correctly. I have to assume, then, that I am misusing the setOrigin command. What am I missing?

Upvotes: 1

Views: 819

Answers (1)

Southpaw Hare
Southpaw Hare

Reputation: 1593

Neeeeevermind! Turns out, I simply forgot to add the control to the Map of controls. Once I did that, it works perfectly.

    for (int i = 0; i < NUM_OF_LABELS; i++) {
        Label label = new Label(cmpScrollInner, SWT.NONE);
        label.setText("Label " + String.valueOf(i+1));
        controlMap.put(new Integer(i), label);
    }

It was a stupid mistake after all.

Upvotes: 1

Related Questions