Most read:
Popular archives:
JavaWorld's new look is here!
We've upgraded the site with a fresh look-and-feel, improved topical navigation, better search, new features, and expanded
community platform. Learn more about the changes to JavaWorld.
| Oracle Compatibility Developer's Guide |
| The Explosion in DBMS Choice |
Like many Java programmers, you may have given up on writing client-side window applications. There's a lot of debate about why client-side Java is out of fashion. But most of it boils down to the fact that Java—until now—could not produce native window applications. With the arrival of SWT you now can put together fast and attractive GUIs using relatively straightforward Java code. SWT applications are largely undistinguishable from natively written applications and, since many concepts will also be familiar to Java Swing programmers, the learning curve should prove to be gentle.
This article presents a common use for any windowing toolkit: displaying and editing data-bound widgets.
To get up and running with SWT, you need to download the SWT runtime. Check Resources for a link to the latest stable build. For this article, we use Eclipse 3.0 M6, which contains 2 DLLs, or dynamic link libraries, (in the Windows download) and the Java JNI (Java Native Interface) library, swt.jar.
To compile or run any SWT code, you must pass a Java property that locates the runtime libraries. On Windows, for example,
you could install to C:\SWT and set this property like so:
-Djava.libary.path=c:\SWT
However, you must ensure other native libraries in your application, like a database driver, are not using this property.
Of course, swt.jar needs to be in the Java classpath. You may also wish to include jface.jar from the Eclipse distribution to take advantage of the JFace library. However, for this article, I do not discuss JFace, and the library is not required.
Much of the interest in SWT stems from the Eclipse IDE. To easily run or compile SWT applications with Eclipse, use the SWT runtime download mentioned above. From the Project Properties dialog, select Java Build Path and add the swt.jar library, as shown in Figure 1.
Figure 1. Add SWT to the Eclipse build path. Click on thumbnail to view full-size image.
To run any SWT application, you still must alter the java.library.path and point it to the SWT installation directory. You can do this in the Run dialog box, where you set VM arguments, as shown in Figure 2.
Figure 2. Add SWT runtimes in Eclipse. Click on thumbnail to view full-size image.
Before you get started with any SWT application, you need to understand some of the basics.
There is a distinct and important line drawn between the Display and Shell classes. Shell represents the Java application window. Display represents the display area, including operating system resources like fonts, images, and graphics contexts. Shell references a chain of windows that includes the main window itself and all its child widgets. Included in that list are all
the dialog boxes and other windows that use the main Shell as a parent.
The distinction between these two classes point to an important facet of SWT development. Unlike most of Swing and AWT, an SWT application needs to clean up its own resources. No garbage collector is available for these types of resources. And failure to clean up can result in memory leaks, some of them severe. In some operating systems, for example, graphics contexts are limited to less than a dozen instances for the whole system. Failure to dispose of these resources can bring the entire system to a halt.
Archived Discussions (Read only)