My Pages

Wednesday, May 26, 2010

SWT - Standard Widget Toolkit

SWT is the UI library used by Eclipse. SWT provides some rich UI components on several platforms. The native widgets of the OS are accessed by the SWT framework via JNI. If a widget is not available on a certain platform, SWT emulates the unavailable widget.

The key components of SWT applications are the classes "Display" and "Shell". A Shell represents a window. Displays are responsible from managing event loops and controlling communication between the UI thread and other threads. Every SWT application requires at least one Display and one or more Shell instances. The main shell gets as a default parameter a Display as construtor argument.

First SWT Application :

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;

public class HelloWorld
{
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
Label label = new Label(shell, SWT.CENTER);
label.setText("Hello, World");
label.setBounds(shell.getClientArea());
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
}

For running the above application, you need to have swt.jar in your classpath.
You can find the swt jar in eclipse's plugin directory.
After adding swt.jar in classpath,the application can be executed successfully.

You will see a window with "Hello World" text.

Understanding the Program
These lines give you the proper imports for the class:

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;


Most classes that use SWT import the SWT object and pieces of the swt.widgets package.

These lines create the Display object and the Shell object:

Display display = new Display();
Shell shell = new Shell(display);

At a high level, the Display object represents the underlying windowing system. The Shell object is an abstraction that represents a top-level window when created with a Display object, as this one is. A more detailed introduction to the Display and Shell classes is presented later in this chapter.

Next, you create your label widget with this code:

Label label = new Label(shell, SWT.CENTER);
label.setText("Hello, World");
label.setBounds(shell.getClientArea());

The Label object is capable of displaying either simple text, as you use it here, or an image. The widget is constructed with a reference to a Shell object, which is an indirect descendant of the Composite class. Composite classes are capable of containing other controls. When SWT encounters this line, it knows to create the underlying windowing system's implementation of the label widget on the associated Composite object.

To make your window display, you call this:

shell.open();

This indicates to the underlying system to set the current shell visible, set the focus to the default button (if one exists), and make the window associated with the shell active. This displays the window and allows it to begin receiving events from the underlying windowing system.

The main loop of your application is this:

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

You'll have a loop similar to this in each of your SWT applications. In this loop, you first check to make sure that the user hasn't closed your main window. Because the window is still open, you next check your event queue for any messages that the windowing system or other parts of your application might have generated for you. If no events are in the queue, you sleep, waiting for the next event to arrive. When the next event arrives, you repeat the loop, ensuring first that the event didn't dispose your main window.

Finally, you call:

display.dispose();

Check out more @ : SWT: The Standard Widget Toolkit—Part 2: Managing Operating System Resources.

Eclipse SWT Resources

SWT Snippets (Examples)

SWT Introduction

SWT: The Standard Widget Toolkit - PART 1: Implementation Strategy for Java™ Natives

SWT: The Standard Widget Toolkit - PART 2: Managing Operating System Resources

Understanding layout in SWT

1 comment: