O'Reilly    
 Published on O'Reilly (http://oreilly.com/)
 See this if you're having trouble printing code examples


Why the excitement about Swing? The authors let O'Reilly Web site editor Allen Noren know why the ability to build state-of-the-art user interfaces is plenty to get excited about.


Noren:
What is Swing?

Eckstein:
Swing is the next-generation GUI toolkit that Sun Microsystems has developed for the Java language. It is essentially a vast component framework built over parts of the older AWT component libraries used in Java 1.0 and 1.1. Swing is written in 100% Java; there are no native calls. Hence, it can run on any platform with a compatible Java virtual machine.

Noren:
Why is Swing important to Java developers?

Eckstein:
Three reasons. First, it provides Java programmers many new powerful components-- most of which we've been begging for since Java's inception. Second, it gives developers a chance to create a powerful, portable application that can not only target users on any Java-compatible platform, but also target their desired look-and-feel as well. Third, the distribution is easy to learn, but can also be used at varying levels of complexity to curtail to the programmer's wishes.

Noren:
How is Swing related to JFC?

Eckstein:
Swing is only one part of the JFC. The Java Foundation Classes (JFC) are a collection of packages that developers can use to create full- featured applications in Java. They include AWT, Swing, Accessibility, Java 2D, and Drag and Drop functionality. With the exception of access- ibility, most of these buzzwords are familiar: accessibility gives applications the ability to provide an assistive technology (a set of classes) that can share the same virtual machine as the target application and interface with their components. For assistive technologies, such as speech recognition and braille displays, this offers an easy way to work with all applications. We devote an entire chapter in this book to programming with accessibility.

Noren:
What distinguishes Swing from other libraries for creating graphic user interfaces, like MFC (Microsoft Foundation Classes) or WFC (Microsoft's Windows Foundation Classes)?

Eckstein:
MFC is Microsoft's core C/C++ libraries for using widgets in the Windows operating system. It is enormously popular among Windows programmers. The Windows Foundation Classes are a revamping of MS's Application Foundation Classes (AFC), developed exclusively in Java. The latter is a direct competitor of JFC, and promises features that work only on the Windows platform. (This has been the source of some controversy between Microsoft and Sun.)

So what distinguishes Swing? There is of course the issues that we touched on already: 100% Java means portability, and its design allows the ability to reset the look-and-feel of an application at runtime. Swing depends on the event model introduced with Java 1.1, where as the AFC used the event model of Java 1.0. On the downside, Swing is likely to be a little slower than any native libraries put out by Microsoft, especially MFC, because it's written in pure Java. And of course, it's brand new, so there are still some kinks to be worked out.

Noren:
What does Swing give you that other libraries don't?

Eckstein:
Swing employs a component design known as model-view-controller (MVC), which allows you to abstract out the data of a component (model) from how it is displayed on the screen (view). This is a wonderful design from a programming perspective because its tried-and-true and molds into Java nicely. With Swing, there is a slight modification: the view and the controller are fused into a single object called the "UI-delegate," which manages the visual aspect of the component. Programmers can either use the Swing components as they are, or modify parts of the model or delegate to greater deal with their needs.

Swing also provides a rich text framework that allows programmers to do almost anything in six or seven lines of code. I'm extremely glad to see that those classes made it into Swing; this removes a ton of work from creating everything from a simple word processor to an HTML browser.

Noren:
Conversely, what do other libraries do that Swing doesn't?

Eckstein:
Swing has issues when working with AWT and other "heavyweight" components that can frustrate C/C++ developers. Also, there are some multithreading issues that Swing developers will face when working with the component libraries. Both are relatively easy to overcome and are covered in great detail in the book.

Noren:
Are those enhancements planned for the next version of Swing?

Eckstein:
Some enhancements will make it in. Others will not. It's hard to gage which one's will right now.

Noren:
How has Swing changed since it was first released?

Eckstein:
Swing has undergone many changes since it was first released. Some new components have been added, such as a directory chooser; others have been updated. However, a great deal of effort has also been placed into optimization of the Swing components, as this has been a central issue since it first debuted almost a year ago. Many programmers have reported that optimization in the latest version (1.1 beta) has noticably improved.

Noren:
Where is Swing headed?

Eckstein:
I think we can expect to see most of the Java AWT developers switching over to using the new Swing libraries in the near future. The benefits of using lightweight components clearly outweight those of the older AWT components. In the future, I hope to see many new components added to the Swing libraries. (In fact, the Swing team maintains a "pending" directory for all the things that they want to add, including classes to handle choosing money and playing audio.)

Noren:
What are some of the most interesting implementations of Swing that you know of?

Eckstein:
Beyond what I've already mentioned, one thing in particular stands out: Swing has debuted a wonderful new look-and-feel called Metal. This UI is a bit more stylized that most graphical interfaces, but it looks pretty slick. In addition, implementations of the Macintosh look-and-feel have just been released, so there's plenty of room for programmers to play.

Noren:
A more general question: It's been six months since Java One in San Francisco. Have the hype and promises resulted in milestones? How is Java's general health?

Eckstein:
(See "Swing is clearly an extremely..." question below)

Noren:
Two particular claims about Swing are that it fixes the cross-platform portability problems of AWT, and that it has significantly better performance than AWT. What's the truth here?

Eckstein:
Swing components take advantage of the "lightweight" component features introduced in Java 1.1. Essentially, this means that the components are not tied to native peer components, but instead are allowed to create and manage themselves, as well as react to mouse or keyboard events by themselves. The performance is, as far as I've seen, slightly less than AWT. However, conversion was easy, and with some simple optimizations, it has not affected my applications at all.

Eckstein:
How did you manage to get a book covering Swing 1.1/JDK1.2 beta4 just six or seven weeks after the software was released?

Eckstein:
Caffeine was the key ingredient.

Noren:
Swing is clearly an extremely important addition to Java. How have the delays in Swing's release schedule affected the attitudes of Java developers? Are people frustrated? Are they starting to look for other solutions?

Eckstein:
I doubt it. We know the Swing team to very responsive when we were authoring this book. (We also know them to be swamped with work.) While development delays have frustrated a few people, most programmers have downloaded the Swing distribution and said, "Wow! This actually works!" And after looking at parts of the design, they've seen that this component library is easy to use. Speed issues have been the primary concern among the developers that I've seen, but the component library itself has a solid architecture behind it. After the reception it received at JavaOne, I'm convinced the Swing libraries will flourish.

Noren:
This book covers the beta4 release of JDK 1.2. What kinds of changes do you anticipate between now and the final release?

Eckstein:
Well, assuming that the "final" release is the one tied with the final release of JDK 1.2, I think there will be many additions that allow Swing to take better advantage of the Java 2D libraries. I think there will be several new components added to the libraries, and the existing ones will get a significant performance boost. More support for drag- and-drop, and finally, I think there will be better support for accessibility in both AWT and Swing.

Copyright © 2009 O'Reilly Media, Inc.