Preface

Since Java was first released, its user interface facilities have been a significant weakness. The Abstract Window Toolkit (AWT) was part of the JDK from the beginning, but it really wasn’t sufficient to support a complex user interface. It supported everything you could do in an HTML form, and provided free-standing frames, menus, and a few other objects, but you’d be hard-pressed to implement an application as complex as Quicken or Lotus Notes. AWT also had its share of portability problems; it relied heavily on the runtime platform’s native user interface components, and it wasn’t always possible to hide differences in the way these components behaved.

JDK 1.1 fixed a number of problems—most notably, it introduced a new event model that was much more efficient and easier to use—but it didn’t make any major additions to the basic components. We got a ScrollPane and a PopupMenu, but that was about it. Furthermore, AWT still relied on the native components, and therefore continued to have portability problems.

In April of 1997, JavaSoft announced the Java Foundation Classes, or JFC, which supersedes (and includes) AWT. A major part of the JFC is a new set of user interface components that is much more complete, flexible, and portable. These new components are called “Swing.” (The JFC also includes a comprehensive facility for 2D graphics, printing, and “drag-and-drop.”) With Swing, you can design interfaces with tree components, tables, tabbed dialogs, tooltips, and many other features that computer users have grown accustomed to.

In addition to the new components, Swing makes three major improvements on the AWT. First, it doesn’t rely on the runtime platform’s native components. It’s written entirely in Java, and creates its own components. This new approach should solve the portability problem, since components don’t inherit the weird behaviors from the runtime environment. Second, because Swing is in complete control of the components, it’s in control of the way components look on the screen, and gives you more control of how your applications look. You can choose between several pre-built “look-and-feels,” or you can create your own if you want your software to show your personal style. This feature is called “Pluggable Look-and-Feel” or PLAF. Third, Swing makes a very clear distinction between the data a component displays (the “model”) and the actual display (the “view”). While the fine points of this distinction are appreciated mostly by computer scientists, it has important implications for all developers. This separation means that components are extremely flexible. It’s easy to adapt components to display new kinds of data that their original design didn’t anticipate, or to change the way a component looks without getting tangled up in assumptions about the data it represents.

The first official release of Swing, for use with JDK 1.1, took place in the spring of 1998. Swing (and the rest of JFC) is a part of JDK 1.2, and is currently revolutionizing Java user interface development. This book shows you how to join the revolution.

What This Book Covers

This book gives a complete introduction to the entire Swing component set. Of course, it shows you how to use all of the components: how to display them on the screen, register for events, and get information from them. You’d expect that in any Swing book. This book goes much further. It goes into detail about the model-delegate architecture behind the components, and discusses all of the data models. Understanding the models is essential when you’re working on an application that requires something significantly different from the components’ default behavior: for example, if you need a component that displays a different data type, or one that structures data in some non-standard way, you’ll need to work with the data models. This book also discusses how to create your own look-and-feel, and how to write “accessible” user interfaces.

There are a few topics that this book doesn’t cover, and assumes you already know. First, we assume you know the Java language. For Swing, it’s particularly important to have a good grasp of inner classes (both named and anonymous), which are used by Swing itself and in our examples. We assume that you understand the JDK 1.1 event model, Java’s mechanism for communicating between asynchronous threads. Swing introduces many new event types, all of which are discussed in this book, but we only provide an overview of the event mechanism as a whole. We also assume that you understand the older AWT components, particularly the Component and Container classes, which are superclasses of the new Swing JCompo-nent. We assume that you understand the AWT layout managers, all of which are usable within Swing applications. If you are new to Java, or would like a review, you can find a complete discussion of these topics in Java AWT by John Zukowski, or a solid introduction in Exploring Java by Pat Niemeyer and Joshua Peck (both published by O’Reilly). We do not assume that you know anything about other JFC topics, like Java 2D; all the drawing and font manipulation in this book can be done with the older ( JDK 1.1) AWT. (We do cover the JFC’s Accessibility API, which is supported by every Swing component.)

We were hoping to say that this book was based on JDK 1.2. Unfortunately, nothing is ever quite that simple. As this book goes to press, the most recent release of JDK is 1.2 beta 4, which incorporates Swing 1.1 beta. The most recent version of Swing that has been “blessed” as an officially released product is Swing 1.0.3. The differences between these versions are minor, and we’ve tried to point them out, but we do feel like we’re swimming in version number soup.

One significant problem we’ve faced is Sun’s waffling on the Swing package names. Swing was first released in the com.sun.java.swing package hierarchy, which was supposed to be a temporary resting place. With JDK 1.2, Swing was supposed to move into the java.awt.swing hierarchy. For some reason, Sun backed off, and kept Swing in the com.sun hierarchy for beta 4. They then moved it to javax.swing for the first official release of JDK 1.2—except for a few oddball platform-specific packages (like the Windows look-and-feel) that remain in com.sun. We’ve been at our wit’s end trying to fix the package names, both in the book and in the online source files. At any rate, for JDK 1.2 and Swing 1.1 (for use with JDK 1.1), the major Swing classes are in the following packages:[1]

javax.accessibility

Classes to support accessibility for people who have difficulty using standard user interfaces. Covered in Chapter 25.

javax.swing

The bulk of the Swing components. Covered in Chapters 3 through 14, and 27 and 28.

javax.swing.border

Classes for drawing fancy borders around components. Covered in Chapter 13.

javax.swing.colorchooser

Classes providing suport for the JColorChooser component (Chapter 12).

javax.swing.event

Swing events. Covered throughout the book.

javax.swing.filechooser

Classes providing support for the JFileChooser component (Chapter 12).

javax.swing.pending

A home for components that aren’t ready for prime time; these components aren’t discussed in this book, though we’ll add them to future editions when they graduate from the pending package.

javax.swing.plaf

Classes supporting “pluggable look and feel,” including classes that implement the Metal and Multi look-and-feels. (Implementations of the Windows and Motif L&Fs are packaged under com.sun.java.swing.plaf.) Covered in Chapter 26.

javax.swing.table

Classes providing support for the JTable component (JTable itself is in javax.swing). Covered in Chapter 15, and Chapter 16.

javax.swing.text

Classes providing support for the text components (JTextField, etc.; the components themselves are in the javax.swing package). Covered in Chapters 19 through 24. The text.html package has a subpackage, parser, that includes tools for parsing HTML. We expect significant upgrades to the HTMLEditorKit; when there’s news, we’ll make an update to this chapter available online. Check O’Reilly’s web site for the latest information.

javax.swing.text.html and javax.swing.text.rtf

“Editor kits” for working with HTML and Microsoft RTF documents. Covered in Chapter 24.

javax.swing.tree

Classes providing support for the JTree component (JTree itself is in javax.swing). Covered in Chapter 17.

javax.swing.undo

Classes that implement undoable operations. Covered in Chapter 18.



[1] The latest rumor is that Sun will rechristen JDK 1.2 when the final official release occurs—it will probably be called Java 2.

Get Java Swing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.