SWT Creates Fast, Native-looking GUIs for Your Java Apps

ince Sun Microsystems released the first version of Java, the industry has been displeased with the way Java’s front-end development toolkit works. Though Swing appeased some developers with its look-and-feel, the interfaces one could build with it still lacked the right look and its performance did not meet the requirements of large-scale, industrial-strength applications.

To address this shortcoming, some folks like IBM set out to develop an entirely new toolkit that has a widget framework similar to Java’s Abstract Window Toolkit (AWT) but is entirely dependent on the native operating system’s GUI widgets. The result was the Standard Widget Toolkit (SWT), an open source Java graphical user interface (GUI) toolkit comprised of a widget set and a graphics library. Unlike AWT, SWT relies on the underlying operating system’s native widgets, even if they are not present on other platforms. As a result, SWT enables client-side Java applications to assume the appearance and performance of traditional native desktop applications, albeit at the expense of portability.

Compared with AWT, SWT offers a number of benefits:

  • UIs built on SWT are very similar to that of native applications.
  • Resources are used more efficiently.
  • Memory management is safer.
  • Program logic is clearer.
  • Performance improves.
  • Fewer overall operating system threads are required.

In short, SWT widgets (UI components) let you create fast, native-looking GUIs for your Java applications. If platform independence is not a required feature for your application, SWT may be a very attractive option. Though application development with SWT involves some study, it is not a steep learning curve for Java programmers. This tutorial teaches the basic steps to get started with SWT and introduces the SWT widgets and their usages in a simple SWT application.

SWT Widgets
This section demonstrates the widgets that SWT provides and compares them with equivalent offerings in Swing. For those familiar with Swing, this is a good introduction to SWT.

Display and Shell
You need to create a display and a shell to hold the SWT widgets. A display is an object that contains all GUI components. It is not actually visible, but the components added to it are. Typically, you create only one display for an application.

A shell is a window within the application. You can create any number of shells for an application, attaching them at the top level (to a display) or to other shells.

Instances of the Display class manage the connection between SWT and the underlying operating system. Their most important function is to implement the SWT event loop in terms of the platform event model. This class has overall control over the operating system resources that SWT allocates. Display is analogous to JFrame and Shell is analogous to the Content pane in Swing.

SWT Components
Label
A label is a collection of characters that the user cannot modify (although they can be modified programmatically). Unlike Swing label or AWT label, SWT labels provide a special SEPARATOR style. The separator style is a special kind of label that draws a line separating different widgets. The following command places a label on the shell:

Label label1 = new Label(shell,SWT.BORDER);

Button
A button is a widget that can capture user clicks and initiate some processing. A SWT button provides a type field that can be used to distinguish among various types of buttons: PUSH, CHECK, RADIO, TOGGLE, and ARROW. In Swing, different components are provided (JCheckBox, JRadioButton, and JToggleButton) for each type of button. The following command places a Button widget on the shell:

Button button1 = new Button(shell,SWT.PUSH);

Text
A Text widget contains text that users can enter and modify. Unlike Swing’s JTextField, you can set the max number of characters that the users can edit. To provide password-like services, Swing provides a separate class (JPasswordField). SWT offers the same function with the setEchoChar class in its Text widget. The following command places new Text widget on the shell:

Text text = new Text(shell, SWT.BORDER);

Combo
A combo widget allows the user to select an item from a collection of items. The user may also type a value into the combo widget. Since this widget is a composite containing Text and List, it contains the methods of both Text and List widgets. Swing JComboBox is a combination of JTextField, Jbutton, and a drop-down JList. The following command places a Combo widget on the shell:

Combo combo = new Combo(shell, SWT.DROP_DOWN);

List
A list is a widget that contains a collection of items. It allows the user to select an item from the collection. A list may be single or multi select. The list widget supports scrolling directly, unlike Swing, which requires you to add the JList to a JscrollPane. The following command places a List widget on the shell:

List list = new List(shell,SWT.MULTI);

Composite
A composite is a widget that can contain other widgets. You place widgets inside a composite the same way you place widgets on a shell. The position of each widget inside a composite is relative to the composite, so if the composite is moved on the shell, the widgets inside the composite retain their relative positions. The following command creates a Composite widget:

Composite compObj = new Composite(shell, SWT.BORDER);

TabFolder
Using the TabFolder widget also demonstrates the use of composites. This widget provides the notebook UI metaphor. It allows the user to select a notebook page from a set of pages. TabItem widgets can be added only to the TabFolder.

The following command creates a TabFolder widget:

       TabFolder tf = new TabFolder(shell, SWT.NONE) ;

TabItem
TabItem is a selectable user interface object corresponding to a tab for a page in a tab folder. The following command creates a TabItem widget:

       TabItem ti = new TabItem(tf, SWT.NO_FOCUS, 0) ;       ti.setText("Tab 1");

To build the user interface that goes into the TabItem, use a composite widget:

Composite comp = new Composite(tf,SWT.NONE);

The item children that may be added to instances of this class must be of type TabItem.

Create Control children and then set them into a tab item using the setControl method.

Table
The table widget provides a selectable user interface that displays a list of images and strings and issues notification when selected. If the style is defined as CHECK, it creates a checkbox column as the first column:

Table table = new Table(shell,SWT.BORDER);

You can make the headers for the column visible with the following method:

table.setHeaderVisible(true);

You can make the lines visible on the table with the following method:

table.setLinesVisible(true);

By default, both these properties are set to false.

TableColumn represents a column in a table widget:

TableColumn name = new TableColumn(table1, SWT.LEFT);

TableItem represents a single row in the table. You can create a single row using the following command:

TableItem item1 = new TableItem(table1,SWT.NONE);

You can either create a single row or set data for one field at a time using setText(int, String).

Tree
Tree widget displays data in a hierarchical format:

Tree tree = new Tree(shell,SWT.MULTI | SWT.BORDER);

You can use TreeItem widgets to attach the items to the tree or recursively to attach new tree items to existing tree items. The following command creates a Tree widget and adds a child node to the tree:

TreeItem rootNode1 = new TreeItem(tree, SWT.NONE);rootNode1.setText("Root Node 1");TreeItem childNode1 = new TreeItem(rootNode1, SWT.NONE);childNode1.setText("Child Node 1");

Menu and MenuItem
The MenuBar widget spans the top of the shell. To create a MenuBar use the following code:

Menu mBar = new Menu(shell,SWT.BAR);Shell.setMenuBar(menu);

Swing provides a separate component MenuBar, while the SWT menu provides a separate style SWT.BAR to create a menu bar. You also can use the Menu widget recursively to create sub-menus.

Use MenuItem widgets to create a selectable menu item from the menu. The main advantage of SWT widgets is they provide different styles to create different types of menu items.

To create a radio box use the style SWT.RADIO, to create a check menu item use SWT.CHECK. SWT Menu provides other aspects such as accelerators, mnemonics.

Running an Application
The simple application included in the code download demonstrates the usage of each of the SWT widgets discussed in this article. Once you’ve unzipped the package, include swt.jar in your class path and compile the MyFirstSWTApp.java file.

Run the application with the following command:

Java –Djava.library.path= MyFirstSWTApp

This gets you started on using the SWT widgets and details the available basic widgets in SWT.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: