Swing is a windowing framework that adds new capabilities to the AWT. For example, Swing introduces a wide assortment of lightweight components (from lightweight buttons to lightweight trees and tables). These components are completely managed by Java and not by peers. (Some of the containers into which Swing's components are placed are associated with heavyweight peers. After all, at some point, Swing must ensure that the underlying windowing system displays these components.) Because Swing's components are rendered and managed by Java, they look nicer and have more capabilities (such as tooltips) than their AWT counterparts. Furthermore, these components can have either a consistent look and feel across windowing systems, or take on any desired (and available) look and feel. (In contrast, AWT components are "forced" to adopt the look and feel of the underlying windowing system. Look and feel will be explored in the next chapter.) Perhaps the best way to start learning Swing is to take a brief look at those Java packages that comprise this framework. Table 15.1 presents the names and contents of these packages. Table 15.1. Java's Swing Packages
One of Swing's more important features is its pluggable look-and-feel. This feature makes it possible to create a GUI that looks the same on any windowing system. You can also change a GUI's look and feel on-the-fly . (Pluggable look-and-feel is discussed in Chapter 16, "Building a GUI: Swing Style." ) Swing (as with the rest of the JFC) is an evolving API. Each new version offers new features and fixes bugs . If you are asked to create a Swing-based program that requires a certain version of Swing, you must be able to determine the version. A solution to this problem is demonstrated by the Version application, whose source code is presented in Listing 15.1. Listing 15.1 The Version Application Source Code// Version.java import javax.swing.*; class Version { public static void main (String [] args) { JLabel l = new JLabel (); Package p = Package.getPackage ("javax.swing"); if (p != null) { System.out.print ("Swing Version: "); System.out.println (p.getSpecificationVersion ()); } p = Package.getPackage ("java.awt"); if (p != null) { System.out.print ("AWT Version: "); System.out.println (p.getSpecificationVersion ()); } } } Version obtains the current version of Swing and the AWT by using the Package class, along with its getPackage and getSpecificationVersion methods . The getPackage method returns a reference to a Package object that corresponds to its package name argument, provided that at least one class belonging to the package has been loaded. (By creating a dummy JLabel object, it's possible to obtain Package objects corresponding to javax.swing and java.awt. ) After a Package object has been obtained, getSpecificationVersion is called to return a String object containing the package's assigned version number. And there you have it ”the current version of Swing and the AWT. Swing's startup performance has been significantly improved in Java 2 Platform Standard Edition, version 1.3. This has been accomplished by minimizing the number of classes that are loaded at startup. |