These days, there are two popular toolkits for building graphical user interfaces in Java: Swing (from Sun) and the Standard Widget Toolkit (SWT) (from IBM/Eclipse). Different developers often have strong opinions about which is best, but in truth they both have their strengths.
Swing, part of the Java Foundation Classes (JFC) first introduced with Java 2 (J2SE 1.2), is the “official” (if you will) GUI framework for Java. It’s built into the desktop version of Java (J2SE). Swing’s approach is to make user interface components (widgets) as customizable and extensible as possible without sacrificing cross-platform consistency.
To do this, it avoids using the operating system’s built-in widgets and instead uses pure Java code to draw the user interface in blank windows and respond to interface events.
The upshot of this is that all of its components are fully skinnable, and can be extended with custom appearance and behaviour. The downside is that the user interface often doesn’t quite match the look and feel of the native operating system (although Sun is getting better and better at this). Additionally, the API can get quite complex as soon as you want to do something not-quite-standard, because of Swing’s amazing flexibility.
Also, because of Swing’s complexity, it’s easy for beginners to do things “the wrong way,” producing applications that work but seem slow or unresponsive while responding to user requests.
Because Swing is built into Java, it’s ideal for situations where application size must me minimized and cross-platform deployment must be completely transparent. For this reason, Swing remains the best choice for Web developers who need to build applets or applications launched via Java Web Start.
From the application developer’s standpoint, SWT is a much simpler API for building GUIs in Java. Instead of avoiding the operating system’s native user interface features, it embraces them, making use of native widgets to provide optimal performance and ease of coding. SWT was first developed for use in the Eclipse Project, but developers have since harnessed it to build all kinds of desktop applications.
The upsides of SWT are clear: it’s an easy API to write for, which means you can produce impressive results more quickly, and because the widgets are all handled by the native operating system, users will find the interface appearance and behaviour completely familiar. It’s also easier to achieve good interface performance (i.e. response time) with SWT.
Depending on the type of development you do, the downsides of SWT are either insignificant or deal-breakers. It is difficult and usually impractical to extend SWT widgets with custom functionality. Because SWT makes use of native operating system functionality, an SWT application must be bundled with native libraries that interface with the operating system on which the program will run. This means you’ll usually have to distribute different versions of your application for Windows, Linux with GTK, Linux with Motif, and Mac OS X. Due to the size of the libraries, it will usually be impractical to deploy applets that use SWT, and support for Java Web Start is problematic.
Although for awhile it seemed Swing was standing still while SWT, the new kid on the block, got all the attention, Swing is experiencing a renaissance as curious developers explore the range of customization and eye candy that can be implemented with Swing UI components.
Here are a few of my favourite examples:
- Creating a custom UI delegate for JTabbedPane
Implements Adobe-style “mini-tabs”, demonstrating how easy it is to change the look and feel of a widget without implementing an entire look and feel.
- Data Tips
Slicker than most native implementations, displays the full label for a list item on mouseover when it is clipped by the list width.
- Help Your Shelf (waiting dialog for Swing)
Though a little gratuitous, this example only scratches the surface of the kind of eye candy that can be achieved by blending Java2D effects with Swing components.
- Swing in 3D
If only to prove that there are no limits, this example shows off a Swing look and feel that appears in 3D when viewed with 3D glasses!
An excellent place to watch for the latest practical and not-so-practical Swing experiments is the blog at ClientJava.com.