One of the most exciting trends in software development is the move toward the use of open source tools and components to assist developers in quickly and easily completing assigned programming tasks. One of the most successful of the open source platforms is Eclipse, an open source Integrated Development Environment (IDE) which is designed to enable developers to write code in any language, for any platform, using a standardized IDE. Eclipse has been downloaded by more than 18 million developers worldwide and forms the basis for IBM’s WebSphere Application Developer, perhaps the most popular Java development environment in the corporate world.
One aspect of Eclipse is the Standard Widget Toolkit (SWT), a set of components that enable the developer to easily build GUIs. Although Java itself has built-in capability to develop graphical applications using the Abstract Windowing Toolkit (AWT) and the Java Foundation Classes (Swing) components, these toolkits have been tarred with the brush of sluggish performance and an inability to deliver user interfaces that appear to seamlessly integrate with the operating system platform for which the GUI was developed. Such is the price we pay for the promise of Java—write once, run anywhere.
The SWT provides the ability to write once, run natively. SWT delivers this capability by delivering the code necessary to create the on-screen widgets in an operating-system-specific library, and allowing access to that library from a thin layer of Java classes that make calls to that native code using the Java Native Interfaces (JNI). This affords developers the best of both worlds. Graphical interfaces run in fast native code, while applications can be developed entirely in Java, preserving the ability to write a single code base across multiple platforms. By shifting the on-screen presentation to native code, Java applications have the same look and feel as applications developed entirely in native code on a particular platform. SWT developers also gain from the performance boost of having the code that does the heavy lifting of drawing graphics being performed in compiled code executing outside the Java Virtual Machine (JVM). The only downside is the requirement that you deploy a separate, and different, runtime library for each platform upon which you want to execute your finished product.
Although nothing in the SWT requires it, this book uses the Eclipse IDE to look at the nuts and bolts of SWT development. The command-line tools of the Java SDK could be used just as easily.
I begin the discussion with how to set up the Eclipse IDE for SWT development. I then proceed with a look at how each of the most commonly used widgets works, developing real working code along the way. I also provide you with some useful hints and tips for developing GUIs—what widget to use under which circumstances.
The Eclipse web site has a list of mirror sites for downloading in case the Eclipse site happens to be overloaded.
From here, click Main Eclipse Download Site, which will take you to the window shown in Figure 1-2.
Here you will find 11 (as of this writing) different versions of the SWT, one for each supported platform. Download the file that matches your development environment and extract the files to a location on your development machine.
For my system, I downloaded the version for Windows 98/ME/2000/XP.
This downloaded a file called
swt-3.0M8-win32.zip, which I then expanded into
a directory on my local machine, C:\swt.