View RSS Feed

My Java Tips

SWT Composite

Rate this Entry
by , 11-10-2011 at 05:53 PM (2815 Views)
Composite are controls which are capable of containing other controls. Controls are placed inside a composite in the same manner that Controls are placed on a shell. The position of each widget inside a composite is relative to the composite, so if the composite is moved on the shell, the Controls inside the composite retain their relative positions.


The styles supported by composites are NO_BACKGROUND, NO_FOCUS, NO_MERGE_PAINTS, NO_REDRAW_RESIZE, NO_RADIO_GROUP, EMBEDDED, DOUBLE_BUFFERED. Please note that NO_BACKGROUND, NO_FOCUS, NO_MERGE_PAINTS, and NO_REDRAW_RESIZE styles are intended for use with Canvas. They can be used with Composite if you are drawing your own, but their behavior is undefined if they are used with subclasses of Composite other than Canvas.

The following are the important constructors of Composite class.

Composite(Composite parent, int style)
Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.

The following are the important methods of Composite class.

void changed(Control[] changed)
Clears any data that has been cached by a Layout for all widgets that are in the parent hierarchy of the changed control up to and including the receiver.

protected void checkSubclass()
Checks that this class can be subclassed.

Layout getLayout()
Returns layout which is associated with the receiver, or null if one has not been set.

Control[] getTabList()
Gets the (possibly empty) tabbing order for the control.

void layout()
If the receiver has a layout, asks the layout to lay out (that is, set the size and location of) the receiver's children.

void layout(boolean changed)
If the receiver has a layout, asks the layout to lay out (that is, set the size and location of) the receiver's children.

void layout(boolean changed, boolean all)
If the receiver has a layout, asks the layout to lay out (that is, set the size and location of) the receiver's children.

void layout(Control[] changed)
Forces a lay out (that is, sets the size and location) of all widgets that are in the parent hierarchy of the changed control up to and including the receiver.

boolean setFocus()
Causes the receiver to have the keyboard focus, such that all keyboard events will be delivered to it.

void setLayout(Layout layout)
Sets the layout which is associated with the receiver to be the argument which may be null.

void setLayoutDeferred(boolean defer)
If the argument is true, causes subsequent layout operations in the receiver or any of its children to be ignored.

void setTabList(Control[] tabList)
Sets the tabbing order for the specified controls to match the order that they occur in the argument list.

Example

The example below creates a Composite control. You can find that with composite control you can scroll a child control automatically.

Java Code:
import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;

public class SWTComposite {
	public static void main(String[] args) {
		Display display = new Display();
		final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.H_SCROLL
				| SWT.V_SCROLL);
		final Composite composite = new Composite(shell, SWT.BORDER);
		composite.setSize(700, 600);
		final Color red = display.getSystemColor(SWT.COLOR_RED);
		composite.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				e.gc.setBackground(red);
				e.gc.fillOval(5, 5, 690, 590);
			}
		});
		final ScrollBar hBar = shell.getHorizontalBar();
		hBar.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				Point location = composite.getLocation();
				location.x = -hBar.getSelection();
				composite.setLocation(location);
			}
		});
		final ScrollBar vBar = shell.getVerticalBar();
		vBar.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				Point location = composite.getLocation();
				location.y = -vBar.getSelection();
				composite.setLocation(location);
			}
		});
		shell.addListener(SWT.Resize, new Listener() {
			public void handleEvent(Event e) {
				Point size = composite.getSize();
				Rectangle rect = shell.getClientArea();
				hBar.setMaximum(size.x);
				vBar.setMaximum(size.y);
				hBar.setThumb(Math.min(size.x, rect.width));
				vBar.setThumb(Math.min(size.y, rect.height));
				int hPage = size.x - rect.width;
				int vPage = size.y - rect.height;
				int hSelection = hBar.getSelection();
				int vSelection = vBar.getSelection();
				Point location = composite.getLocation();
				if (hSelection >= hPage) {
					if (hPage <= 0)
						hSelection = 0;
					location.x = -hSelection;
				}
				if (vSelection >= vPage) {
					if (vPage <= 0)
						vSelection = 0;
					location.y = -vSelection;
				}
				composite.setLocation(location);
			}
		});
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

}

Submit "SWT Composite" to Facebook Submit "SWT Composite" to Digg Submit "SWT Composite" to del.icio.us Submit "SWT Composite" to StumbleUpon Submit "SWT Composite" to Google

Categories
SWT / JFace

Comments