[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.platform.swt] Re: how to make a scrollable view ?

I think the key is to set the size of the ScrolledComposite's Content.

As the JavaDoc for ScrolledComposite states, there are two ways to use
ScrolledComposite. One is to set the size of the Content, and the other is
to set the ScrolledComposite to expand and set the ScrolledCompsite's
minimum size. The following class shows both methods, just change the
value of "expand".


/* Main.java */
package com.cormier.tools.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Main {
	final static boolean expand = true;
	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());

		ViewForm form = new ViewForm(shell, SWT.DIALOG_TRIM);
		form.setLayout(new FillLayout());

		ScrolledComposite scroll = new ScrolledComposite(form, SWT.H_SCROLL |
SWT.V_SCROLL | SWT.BORDER);
		scroll.setLayout(new FillLayout());
		scroll.setBackground(display.getSystemColor(SWT.COLOR_RED));
		scroll.setExpandHorizontal(expand);
		scroll.setExpandVertical(expand);
		form.setContent(scroll);

		Composite comp = new Composite(scroll, SWT.NONE);
		comp.setLayout(new RowLayout(SWT.VERTICAL));
		new CLabel(comp, SWT.NONE).setText("This is a test Label with really
long text, to give us plenty of scrolling room\n\n\n\n\n\n\n\n");
		new Text(comp, SWT.NONE).setText("This is a text control.");
		new Button(comp, SWT.NONE).setText("This is a test Button");
		comp.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
		if(expand)
			scroll.setMinSize(comp.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		else
			comp.setSize(comp.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		scroll.setContent(comp);

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