Section 13.2. Java GUIs: From AWT to Swing


[Page 604 (continued)]

13.2. Java GUIs: From AWT to Swing

Ever since the release of version 1.2 of the Java Development Kit (JDK) in 2000, Java has contained two distinct libraries of GUI components. The Abstract Windowing Toolkit (AWT) has been part of Java since the original 1.0 version of the JDK 1.0. The more advanced Swing component set was first introduced in JDK 1.1 and was extensively revised in JDK 1.2.

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 meant that Java GUI programs were forced to rely on GUI elements that were part of the underlying operating system. A Java GUI program running on a Windows platform had to depend on Windows code for implementations of its buttons and text fields. A Java program running on Unix depended upon underlying Unix code for its GUI components. Such dependence on the underlying operating system made the AWT less portable and less efficient.

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 peer model, a design in which every AWT component has a corresponding class (a peer) written in the underlying system's code. For example, the java.awt.Button class has a peer named java.awt.peer.Button. The peer class serves as the interface between the Java code and the computer's underlying windowing system. The methods in the peer class are written in so-called native codethat is, the non-Java code of the underlying operating system. Therefore, AWT components are inherently platform dependent.

The AWT peer model


AWT components are called heavyweight 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 affecting the overall efficiency of the system.


[Page 605]

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.

Lightweight components


Figures 13.1 and 13.2 show the relationship between AWT and Swing classes. The top-level Swing classesJApplet, JDialog, JFrame, and JWindoware direct subclasses of their corresponding AWT counterparts. These are the top-level GUI windows. The remaining Swing components (Fig. 13.2) are subclasses of java.awt.Component and java.awt.Container. As you can see, the names of Swing and AWT components are very similar. Swing components that have corresponding AWT components have names that begin with "J."

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 superior to their AWT counterparts, the AWT package will eventually be dropped. However, this is not likely. Even if a Java program uses Swing components exclusively, that will still not break the dependence on the AWT.

There are several reasons for this dependence. First, Swing's top-level window classesJApplet, JDialog, JFrame, and JWindoware 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 windowsWindow, Frame, Dialog, and Panelprovide that mapping.


[Page 606]

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 (java.awt.FlowLayout), fonts (java.awt.Font), colors (java.awt.Color), and other noncomponent classes that are defined in the AWT. There is just no way to design a GUI without using AWT classes. Therefore, the programs presented in this and subsequent chapters will use Swing components instead of corresponding AWT components, but they also will use layouts and other elements from the AWT.

Java Programming Tip: Swing Documentation

Complete documentation of the Swing classes is available for downloading or browsing on Sun's Web site at http://java.sun.com/docs/.





Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net