What Is Swing?

   

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
Package Contents
javax.swing Classes and interfaces that describe a set of lightweight (all-Java) components
javax.swing.border Classes and an interface that make it possible to draw borders around Swing components
javax.swing.colorchooser Classes and an interface that are used by the JColorChooser component
javax.swing.event Classes and interfaces that describe Swing-specific events and listeners
javax.swing.filechooser Classes that are used by the JFileChooser component
javax.swing.plaf Classes and an interface that Swing uses to provide its pluggable look-and-feel capabilities
javax.swing.plaf.basic Classes and an interface that Swing uses to provide a basic look and feel foundation, on which other look and feels are built
javax.swing.plaf.metal Classes that Swing uses to provide a metal look and feel (also known as the Java look and feel)
javax.swing.plaf.multi Classes that Swing uses to provide an auxiliary look and feel ”a combination of a custom look and feel with the default look and feel
javax.swing.table Classes and interfaces that are used by the JTable component
javax.swing.text Classes and interfaces that are used by editable and read-only text components
javax.swing.text.html Classes for creating HTML text editors
javax.swing.text.html.parser Classes and an interface for creating an HTML parser
javax.swing.text.rtf A single class for creating a rich-text editor
javax.swing.tree Classes and interfaces that are used by the JTree component
javax.swing.undo Classes and interfaces for supporting undo/redo capabilities in an application (such as a text editor)

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.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

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