Chapter 5. SWT

The Standard Widget Toolkit (SWT) consists of a set of tools created by the Eclipse project developers to build Graphical User Interface (GUI) applications easily. This open source widget toolkit provides efficient and portable access to the user interface facilities by focusing on the target operating system.

Although Java provides support for the development of GUI applications by using the Abstract Windowing Toolkit (AWT) and the Java foundation classes (swing), both cannot seamlessly integrate with the target operating system.

SWT supports widgets that makes use of operating-systems-specific libraries and grant access to them from a set of Java classes that are responsible for communicating with the native code. The outcome is the achievement of better performance with the native code, preserving the ability of writing the desired code only a single time so that it can be used across multiple platforms. Since the code is native, the look and feel of the applications follow the target platform, and the task of drawing the graphics is performed by the compiled code that is not part of the Java Virtual Machine (JVM). A minor setback is the necessity of deploying a run-time library for each platform that is going to execute the application.

In this chapter the following topics are discussed:

  • Setting up an environment for developing SWT applications
  • Widgets and displays
  • Several user interface elements (controls), such as labels, texts, lists, buttons, and combos
  • Layout styles

Getting started

As previously mentioned, SWT is a widget toolkit for Java. A widget is a GUI element developed to provide interaction with an user. Text, labels, lists and buttons are examples of widgets. For making use of widgets and getting started with the development of SWT applications, some configurations must be performed, as described in the next section.

Setting up

Although SWT is integrated as part of the Eclipse plug-in API, for older versions or for the development of standalone applications, it may be necessary to download the standalone SWT.

For that, one must go to www.eclipse.org/swt. After selecting the appropriate version (in our case swt-4.2.1-gtk-linux-x86.zip), it is necessary to import the downloaded file into Eclipse by going to File | Import... | Existing projects into workspace | Select archive file, and by hitting the Browse button to locate the SWT library file, as it is shown in the following screenshots:

Setting up

(a) Import Source

Setting up

(b) SWT Library Archive File

After these first steps, it is necessary to include the library in the Java build path of the working project. We show the source code of a sample Hello World SWT program that is used in our explanation on how to add library in the path, as follows:

import org.eclipse.swt.widgets.*;
public class HelloWorld {
   public static void main(String[] args) { 
  Display display = new Display(); 
  Shell shell = new Shell(display); 
  shell.setText("Hello, World."); 
  shell.open(); 
  while (!shell.isDisposed()) { 
    if (!display.readAndDispatch()) 
      display.sleep(); 
  } 
  display.dispose(); 
   }
}

In this sample code that results in the output that is shown in the following screenshot, the basics of a SWT application is presented. A connection to the platform window system is created by the Display object, in which a window is built using that connection containing the text Hello, World. by the Shell object. In the loop that follows, the window system waits for an action by the user that would cause the application to terminate.

Setting up

To properly run and compile this sample SWT program, you will need to add the SWT library project as a dependency. This operation can be performed by opening the Properties dialog of your Java project, and on the Java Build Path page, including the org.eclipse.swt project, as it is shown in the following screenshot:

Setting up

Widgets

Widgets are GUI elements responsible for maintaining the interaction with the user, keeping and redrawing their state, depending on which operation is performed. When an event occurs, which could be the result of an user action or an operation contained inside a program code, a widget redraws itself so that the new state can be shown.

Standard constructors are used to create a widget. As seen in the code snippet of the Setting up section , they are explicitly disposed when there is no more need for them. In a different matter from traditional Java classes, you can note this behavior because widgets use operating systems resources that must be manually disposed in order to avoid resource leaks. The Java garbage collector does not dispose operating system resources.

In this context, when an SWT object is created, or when an SWT resource-based object is called, there is a need to manually dispose it. If there the resource has a parent, disposing it also causes the disposal of its children.

A group of widgets instances are used to build a user interface in the SWT. For that we have the control element, that is a user interface contained inside an application window, called a Shell. Several ordinary elements as Buttons, Texts, Labels and Tables subclass the abstract class Control.

To deal with actions, instead of sub-classing to implement code that reacts to modifications in a GUI, programmers are encouraged to use events and listeners that are added to widgets appropriately. Listeners are used to notify the program when an event has happened.

The last main feature that you must be aware of a widget, regards its application data. Due to the discouraged use of sub-classing (due to possible misbehavior regarding modifications in the superclass followed by the non-recompilation of the subclass), application data can be used to associate any object with a widget. The setData and getData methods can be used for that purpose, as we exemplify as follows:

widget.setData("Doctor", "Walter Bishop");
if ("Walter Bishop".equals(widget.getData("Doctor"))) {
  System.out.println("I'm perfectly sane!");
}

In this example, we store the data as a key/value pair in the widget, where the key is used to get/find the value object, that is, the Doctor key corresponds to the Walter Bishop object, and if the if statement returns true, the I'm perfectly sane message is printed.

Real-world situations may require factories to facilitate the storage of data, as it can be seen in the Widgets section.

Displays

The Display class is responsible for connecting the SWT with the underlying window system. It is not a widget; nevertheless, some of the principles discussed in Widgets section are still the same, such as the handling of events, listeners, and application data. The Display lifecycle is also similar to that of a widget, and should be explicitly created and disposed by the programmer.

One of the most important methods inside it is the readAndDispatch method, that can read and dispatch events. The method reads events from native widgets and dispatches them to the SWT event system. This continuous loop is executed until the main shell is closed. If the loop is interrupted, the application terminates immediately. It is called inside the user-interface thread, where many widgets methods are also called. As seen in the Setting up section, standalone applications can create and make calls to the display event processing class inside its main method.

It is also possible to name the application via the setAppName method, that takes a single String as argument.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset