Understanding Swing


Java™ 2 Primer Plus
By Steven Haines, Steve Potts

Table of Contents
Chapter 16.  The Swing Library

Swing is really two complementary products rolled into one. On one hand, Swing is like a rearchitected version of the AWT. On the other hand, Swing is the next logical version of the Java GUI classes.

The AWT implements its components using the native GUI toolkit on each platform. It creates a peer, which is similar to an object in that GUI. This approach, called the heavyweight approach, has several drawbacks. Because AWT uses different components on each operating system, it can only use the features that they have in common. The peer object in the OS might have tons of additional features that AWT must instantiate, but not use. This causes the AWT component to be heavier than it should be, which keeps it from performing well.

The second problem with this approach is the "least-common denominator" problem. If the Java team wanted to add an xxxBox to Java, it had to identify a peer component on each platform for this widget. If even one platform is missing this component, the widget can't be added. The ambition of Java developers to implement trees, tables, and tabbed panes caused dissatisfaction with the heavyweight approach.

The third problem with the peer approach is that it is very difficult to create custom components such as JavaBeans under the AWT because they have to be written for each OS on which the JVM runs. There is no intermediate, platform-independent layer in AWT to program with.

Swing approaches the problem of component creation differently. It limits its use of heavyweight (AWT) components to just the Window, Dialog, Applet, and Frame classes. You never see the AWT classes, however, because the Swing class equivalents hides them from you. In addition, Swing makes use of low-level AWT objects such as Color, Font, Graphics, Toolkit, and Event.

By limiting the use of native windowing code to just the basic containers, Swing reduces the weight of its objects. By only using low-level libraries, it eliminates the least-common denominator problem. By creating a framework for JavaBean creation, Swing solves the problem of GUI extensibility. In fact, the Swing components that we will study in this book are implemented as JavaBeans. Chapter 15, "Creating JavaBeans," covers the process of creating and deploying custom components in commercial GUI development products such as Forte.

As we stated earlier, Swing is the next logical release of the Java windowing system. In addition to providing replacements for all the AWT components, Swing provides a whole new set of components for your use:

  • JOptionPane This enables you to create message boxes by specifying a few parameters.

  • BoxLayout A nonwrapping layout manager that lays out components either horizontally or vertically.

  • Floating Toolbars These toolbars can be moved to locations other than the top of the window.

  • ToolTips Sometimes called balloon help, these little yellow messages appear when you leave the cursor over a component for a few seconds.

  • Drag and Drop This allows for the intuitive moving of components and dropping them on to others.

  • JTable A JTable is a kind of a spreadsheet/Grid component.

  • JTree A JTree is a tree view of data like the one that MS Windows Explorer uses in the left panel. It can be expanded and contracted.

  • JLayeredPane A container that enables you to assign a permanent layer to an object to keep objects at the same depths relative to one another.

  • JInternalFrame This enables you to create a window within a window.

We will look at the new Swing components in detail in Chapter 17, "Advanced Swing."


    Java 2 Primer Plus
    Java 2 Primer Plus
    ISBN: 0672324156
    EAN: 2147483647
    Year: 2001
    Pages: 332

    Similar book on Amazon

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