Coding Tips (JavaScript/CSS/VBA/Win32)

Useful code snippets, tips and some Windows applications

A Basic SWT Tutorial

The purpose of this tutorial is to show steps required to write a simple Dialog-based SWT application. The tutorial will be heavy on code.

Let's start with the basic code structure:


public class SimpleDialogTemplate {

	public static Display display;
	public static Shell shell;

	public static void main(String[] args) {
		SimpleDialogTemplate sdt = new SimpleDialogTemplate();
		sdt.main();
	}

	public void main() {
		display = new Display();
		shell = new Shell(display);
		shell.setSize(500, 300);
		shell.setText("Simple SWT Dialog");

		shell.open();
		//pack() statement tries to reduce the size of the dialog to the absolute minimum required.
		//shell.pack();

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

		display.dispose();

	}
}

The above code creates a bare-bones dialog app that does nothing and looks like this:

A shell is a top-level composite (frame or window) that may have no parent composite; instead, it has a Display as a parent, often set by default.
A composite is a control that can contain other controls.

Next - Adding Menu