SWT and JFace, Part 1: A gentle introduction

Introduction

This article was first published by IBM developerWorks at http://www.ibm.com/developerWorks, and has been reproduced here with permission.


The Standard Widget Toolkit (SWT) and JFace libraries are used to develop graphical user interfaces (GUIs) for the Eclipse environment, and also to develop stand-alone GUI native applications. In this article, I introduce some of the basic SWT (the name for the basic GUI object) types and show how to combine them to create a working application.

About this series

This series, SWT and JFace, includes basic articles describing developing applications using the Standard Widget Toolkit (SWT) and JFace libraries that come with the base Eclipse software development kit (SDK). This series concentrates on using SWT and JFace for stand-alone applications. However, most of what you learn will apply to using them within the Eclipse workbench.

About Eclipse, SWT, and JFace

As noted on the Eclipse Web site, Eclipse is a kind of universal tool platform. It's an open, extensible IDE for anything and nothing in particular, and provides tool developers with flexibility and control over their software technologies.

Eclipse provides a base for developers to produce rich GUI-driven tools and applications. Fundamental to this capability are the Eclipse GUI libraries SWT and JFace.

SWT is a library that creates a Java view of the native host operating system GUI controls. It is host implementation-dependent. This means SWT-based applications have several key characteristics:

  1. They look, act, and perform like "native" applications.
  2. The widgets provided reflect the widgets (the components and controls) provided on the host operating system.
  3. Any special behavior of the host GUI libraries is reflected in SWT GUIs.

These goals make SWT different from Java technology's Swing, which was designed to hide operating system differences.

The SWT library reflects the basic widgets of the host operating system. In many circumstances, this approach is too low-level. The JFace library helps by adding many services to SWT applications. JFace does not hide SWT; it just extends it. As you will see later in this series, one of its most important extensions is to isolate the application's data model from the GUI that displays and changes it.

We will start out with simple GUIs that have limited function and progress toward useful applications. We will introduce most of the standard and custom SWT widgets and many JFace features. Our discussion will include at least one example use of the technology.

This series assumes you are familiar with the Java language and Java-based development, and have some understanding of the Java AWT or Swing GUI tool kits.

Before we begin, I need to introduce some SWT terminology:

  • Widget -- The basic SWT GUI component (like Component in Java AWT and JComponent in Swing). Widget is an abstract class.
  • Control -- A widget that has an operating system peer (in other words, has an identity in the operating system). Control is an abstract class.
  • Composite -- A control that can contain other controls (like Container in Java AWT and JPanel in Swing).
  • Item -- A widget contained by other controls (that may not be composites), such as a list or a table. Note that controls that contain items rarely also contain controls and vice-versa. Item is an abstract class.

These widgets are arranged in an inheritance hierarchy. See figures 1, 2, and 3 to see how this is done. In Figure 2, the Basic1 class is the class from this article; all the other classes are standard SWT widgets.


Figure 1. SWT Widget Tree
SWT Widget Tree

Figure 2. SWT Composite Tree
SWT Composite Tree

Figure 3. SWT Item List
SWT Item List

Installing SWT and JFace

See Resources to find the Eclipse Web site. To install Eclipse and make the SWT libraries available for use in your programs:

  1. From the Eclipse home page select Downloads, then select the desired Eclipse version for your platform. Download the associated ZIP file.
  2. Expand the ZIP into a top-level directory (I suggest a name like "EclipseX.Y.Z" where X, Y, and Z are the Eclipse version you selected).
  3. Use the DIR command or Search to find the swt.jar file and place it on your CLASSPATH (or use it on your java -cp command).
  4. Also find all the SWT-*.DLL files and make sure they are accessible (put the directory they are in on the PATH or copy the DLL files into a directory already on the PATH).

Similar steps are needed for other platforms.

Note that although Eclipse is cross-platform (in that it runs on many operating systems), this article is based on the Microsoft® Windows® version of Eclipse. Even so, every example included in this article should work unchanged on other platforms. Also note that this article is based on Eclipse V3.0. Eclipse V3.1 adds a few GUI widget types and features.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“It works on my machine.” - Anonymous