Chapter 13. Graphical User Interfaces
As we have seen, a
We will try to identify design principles that can be applied to the design of more advanced interfaces. Also, because Java's GUI classes provide an
Ever since the release of version 1.2 of the Java Development Kit (JDK) in 2000, Java has contained two distinct libraries of GUI
Although the original version of the AWT was suitable for developing Java applets, it wasn't powerful enough to support full-fledged applications. Commonly used programs, such as word processors and spreadsheets, have GUI requirements that were just too much for the original AWT. The main problem was that the AWT was dependent on the underlying operating system. This
In contrast, the Swing GUI components are part of the Java Foundation Classes (JFC), a collection of classes that do not depend as much on the underlying platform. The Swing library makes it possible to write GUI programs entirely in Java. Because they are rendered entirely by Java code, Swing components make it possible to design GUIs that are truly platform independent. Such programs are much more portable than those that rely on AWT components and the underlying platform. A program that uses Swing components will have the same look and feel on a Mac, Windows, or Unix platform.
13.2.1. Heavyweight Versus Lightweight Components
AWT components are based on the
, a design in which every AWT component has a corresponding class (a
) written in the underlying system's code. For example, the
class has a peer named
. The peer class serves as the interface between the Java code and the computer's underlying windowing system. The
The AWT peer model
AWT components are called
because they depend on the native (peer) system for their drawing and rendering. Since every AWT component has an associated peer component, a Java AWT component would look just like the peer component. This is why an AWT button on a Windows platform looks just like a Windows button. In effect, the AWT button, via its peer, creates and uses a Windows button. When you change the Java button's label, it must call a method in the peer class that changes the label of the peer button. This interaction between Java and the native windowing system requires a good deal of overhead, thereby
By contrast, a lightweight component is one written entirely in Java. Instead of depending on a native component for its rendering, a lightweight component is drawn and rendered entirely by Java code. Because they do not depend on underlying system code, Swing components are more efficient and more portable than corresponding AWT components.
Figures 13.1 and 13.2 show the relationship between AWT and Swing classes. The top-level Swing classes
are direct subclasses of their corresponding AWT counterparts. These are the top-level GUI windows. The remaining Swing components (Fig. 13.2) are subclasses of
. As you can see, the names of Swing and AWT components are very similar. Swing components that have corresponding AWT components have
Figure 13.1. Swing classes, part 1: Relationship between the AWT and the top-level Swing windows.
Figure 13.2. Swing classes, part 2: Swing GUI components are derived from the JComponent class.
(This item is displayed on page 606 in the print version)
One might think that because Swing components are
There are several reasons for this dependence. First, Swing's top-level window classes JApplet , JDialog , JFrame , and JWindow are defined as extensions to their AWT counterparts. This means that Swing-based GUIs are still dependent on the AWT. Java programs need to have some way to map their windows to the windowing system used on the native (Windows, Unix, or Macintosh) platform. The AWT's top-level windows Window , Frame , Dialog , and Panel provide that mapping.
Second, the JComponent class, which is the basis for all Swing components, is derived from java.awt.Container . There are many more such dependencies. Fundamentally, Swing components are based on the AWT.
Finally, all GUI applications and applets use layout managers (
), fonts (
), colors (
Java Programming Tip: Swing Documentation