Skip to content

SWT Tutorial : Form Layout Example

package com.deveshcodelab.examples.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class SWTSnippet14 extends Composite {
	public SWTSnippet14(Composite parent) {
		super(parent, SWT.NONE);
		FormLayout layout = new FormLayout();
		setLayout(layout);
		Text t = new Text(this, SWT.MULTI);
		FormData data = new FormData();
		data.top = new FormAttachment(0, 0);
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(100);
		data.bottom = new FormAttachment(75);
		t.setLayoutData(data);
		Button ok = new Button(this, SWT.NONE);
		ok.setText("Ok");

		Button cancel = new Button(this, SWT.NONE);
		cancel.setText("Cancel");
		data = new FormData();
		data.top = new FormAttachment(t);
		data.right = new FormAttachment(cancel);
		ok.setLayoutData(data);

		data = new FormData();
		data.top = new FormAttachment(t);
		data.right = new FormAttachment(100);
		cancel.setLayoutData(data);
	}

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new GridLayout(1, false));

		Composite composite = new SWTSnippet14(shell);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

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

Leave a Reply

Your email address will not be published. Required fields are marked *