Saturday, March 22, 2008

SWT (Standard Widget Toolkit)

The Standard Widget Toolkit (SWT) is a graphical widget toolkit for use with the Java platform. It was originally developed by IBM and is now maintained by the Eclipse Foundation in tandem with the Eclipse IDE. It is an alternative to the AWT and Swing Java GUI toolkits provided by Sun Microsystems as part of the Java Platform, Standard Edition.

SWT is a graphics library and a widget toolkit integrated with the native window system especially with Windows but Linux and Solaris are supported as well. SWT is an OS-independent API. SWT can be seen as a thin wrapper over the native code GUI of the host operating system.

SWT is written in Java. The design strategy of SWT was focused on building a simple, essential library that would produce GUI applications closely coupled to the native environment.

SWT has been designed to be as inexpensive as possible. This means that it is native-oriented. Anyway, it differs from AWT in a number of details. SWT provides different Java implementations for each platform, and each of these implementations calls natively (through the Java Native Interface, JNI) the underlying native implementation. The old AWT is different in that all platform-dependent details are hidden in C (native) code and the Java implementation is the same for all the platforms.


An important difference from normal Java programming is the way OS-dependent objects are managed in SWT. Swing emulates a large part of such objects (such as widgets, for instance) in Java, leaving the disposal job to the JRE garbage collector. This saves a lot of complexity for the programmer but this lack of control can lead to some unexpected issues, especially with cross-platform development.

SWT designers chose a different approach, obliging the developer to explicitly dispose of OS-dependent objects in the application code. SWT has been designed with efficiency in mind, so handling explicitly OS resources becomes an occasion to promote efficient programming and not just a necessity. Resource disposal is needed to free the OS resources used by the SWT application. Such OS resources need to be explicitly de-allocated by invoking the dispose method.

In practice, disposing of objects is a delicate business and it can lead to unpredictable results whenever another object tries to access an already disposed item.

The Basic Structure of an SWT Program

As already said, an SWT program relies upon the native platform resources (wrapped by the Display class, as we will see later on) both in terms of allocated resources (such as colors, images, and so on) and as regards the event mechanism. As regards event handling, in SWT there is only one thread that is allowed to handle native user interface events.

We now see the basic structure of an SWT program.

There are two basic classes used in any SWT application, the Display and Shell classes. Instances of the Display class are responsible for managing the connections between SWT and the underlying operating system, enforcing the SWT models (for colors or images, for example). The Shell class instead represents windows managed by the platform-dependent desktop manager.

Typically, an SWT program will first create a local resources manager (an instance of the Display class) and attach all the needed widgets to one or more Shell objects.

A snippet of code showing the use of the Display and Shell classes.

00:     Display display = new Display();
01: Shell shell = new Shell(display);
02: //GUI code here
04: while (!shell.isDisposed()) {
05: if (!display.readAndDispatch())
06: display.sleep();
07: }
08: display.dispose();
09: }
10: }

Basic Controls

Some of the basic components are the following:

  • Button. This component is the well-known button component used in toolbars, forms, and so forth.

  • ComboBox. This widget is the well-known combo box component.

  • Label. This component represents a (non-selectable) object that displays a string or an image.

  • List. This widget represents a basic list component.

  • ProgressBar. It shows a progress indicator.

  • Sash. It is the Swing equivalent of a JSplitPane. It is a widget that can be dragged to resize two areas in a GUI.

  • Scale. This component implements an editable GUI item representing a range of continuous numeric values.

  • Slider. This component represents an editable object that stands for a range of discrete, numeric values.

  • Table. This component represents a basic table.

  • Text. This component represents a basic text area.

  • Tree. This component represents a basic tree widget.

  • StyledText. This component represents a text area with styled fonts and other advanced attributes.

No comments: