Uh-oh, it looks like your Internet Explorer is out of date.

For a better shopping experience, please upgrade now.

Java Swing

Java Swing

4.0 2
by Robert Eckstein, Marc Loy, Dave Wood

The Swing classes eliminate Java's biggest weakness: its relatively primitive user interface toolkit. Swing provides many new components and containers that allow you to build sophisticated user interfaces, far beyond what was possible with AWT. The old components have been greatly improved, and there are many new components, like trees, tables, and even text


The Swing classes eliminate Java's biggest weakness: its relatively primitive user interface toolkit. Swing provides many new components and containers that allow you to build sophisticated user interfaces, far beyond what was possible with AWT. The old components have been greatly improved, and there are many new components, like trees, tables, and even text editors. It also adds several completely new features to Java's user interface capabilities: drag-and-drop, undo, and the ability to develop your own "look and feel," or the ability to choose between several standard looks. The Swing components are all "lightweight," and therefore provide more uniform behavior across platforms, making it easier to test your software.All these new features mean that there's a lot to learn. Swing is undoubtedly way ahead of AWT — or, for that matter, any widely available user interface toolkit — but it's also a lot more complicated. It's still easy to do simple things. But once you've seen what's possible, you won't want to do the simple things.Java Swing gives you in-depth coverage of everything you need to know to take full advantage of Swing, providing detailed descriptions of every class and interface in the key Swing packages. It shows you how to use all of the new components, allowing you to build state-of-the-art user interfaces. It also discusses how the components implement the MVC (Model View Controller) architecture, so you can understand how the components are designed and subclass them intelligently. Finally, it shows how to create your own "look and feel." Throughout, Java Swing focuses on giving you the context you need to understand what you're doing. It's more than documentation; Java Swing helps you develop code quickly and effectively.Whether you're a serious Java developer, or just trying to find out what Java can do, you'll find Java Swing an indispensable guide.

Product Details

O'Reilly Media, Incorporated
Publication date:
Java (O'Reilly) Series
Edition description:
Older Edition
Product dimensions:
7.01(w) x 9.21(h) x 1.91(d)

Read an Excerpt

Chapter 9: Internal Frames

In this Chapter:

Managing a Desktop

Certain GUI applications need to simulate a desktop environment by allowing multiple "frames" to be displayed within a single root window. These frames typically look like the normal frames you'd see on a real desktop, but are not actually known to the window manager, because they are not really windows in the normal sense of the term. For some types of applications (word processors, IDEs, etc.), this can be a very powerful approach to UI design.

In this chapter, we'll look at a collection of classes Swing provides to allow you to create this type of application in Java. At the end of the chapter, we'll provide a large sample program that shows how to implement a variety of useful features.


Before looking at each of the classes involved in the Swing desktop/internal frame model, we'll take a moment for an overview of how they all work together. Figure 9-1 shows the relationships between the classes we'll be covering in this chapter.

A JInternalFrame is a container that looks much like a JFrame. The key difference is that internal frames can only exist within some other Java container. JInternalFrame implements the following six interfaces: Accessible, MouseListener, MouseMotionListener, WindowConstants, RootPaneContainer, ComponentListener.

Each internal frame keeps a reference to an instance of the static inner class called JDesktopIcon. Like real frames, JInternalFrames can be iconified. JDesktop Icon is the class responsible for taking the place of the frame when it gets iconified.

Though it is not required, JInternalFrames are typically used inside of a JDesktopPane. JDesktopPane is an extension of JLayeredPane that adds direct support for managing a collection of JInternalFrames in layers. JDesktopPane uses an object called a DesktopManager to control how different behavior, like iconification or maximization, is carried out. A default implementation of this interface, DefaultDesktopManager, is provided. We'll see how all of this functionality is broken out as we cover the various classes and interfaces involved.

One more thing to notice about Figure 9-1 is that JInternalFrame supports a new type of listener called InternalFrameListener. This interface contains methods that match those defined by the AWT WindowListener class, but have slightly different names and take InternalFrameEvents, rather than WindowEvents, as input.

The JInternalFrame Class

JInternalFrame is a powerful addition to Java, providing the ability to create lightweight frames that exist inside other components. An internal frame is managed entirely within some other Java container, just like any other component, allowing the program complete control over iconification, maximization, resizing, etc. Despite looking like "real" windows, the underlying windowing system knows nothing of the existence of internal frames.* Figure 9-2 shows what an internal frame looks like in the different look-and-feels.

There's quite a lot to discuss about JInternalFrames, but most of their power comes when they are used inside a JDesktopPane. In this section, we will give a quick overview of the properties, constructors, and methods available in JInternalFrame, but we'll leave the more detailed discussion of using internal frames to the sections that follow.


JInternalFrame defines the properties and default values shown in Table 9-1.

The accessibleContext property is as expected. The background and foreground properties are delegated to the frame's content pane.

There are three pairs of properties that indicate whether or not something can be done to a frame and whether or not that thing is currently done to the frame.

Table 9-1. JInternalFrame Properties

They are: closable/closed, iconifiable/icon, and maximizable/maximum. Note that closed, icon, and maximum are constrained properties.

The contentPane, glassPane, layeredPane, and menuBar properties come from the RootPaneContainer interface and are taken directly from the frame's JRootPane. The rootPane property is set to a new JRootPane when the frame is constructed.

The value of the defaultCloseOperation property defaults to Windowconstants.HIDE_ON_CLOSE. This implies that when the frame is closed, its setClosed ( ) method will be called. The frame could be reopened at a later time.

The desktopIcon reflects how the frame will be displayed when iconified. A JDesktopIcon (which leaves the rendering to the L&F) is created for the frame when it is instantiated. The desktopPane property provides a convenient way to access the JDesktopPane containing the frame, if there is one.

FrameIcon is the icon painted inside the frame's titlebar (usually on the far left). By default, there is no icon. However, the basic look-and-feel checks to see if a frameIcon has been set and, if not, paints the 'Java cup" icon. This explains why an icon appears in the Windows L&F frame shown in Figure 9-2, but not in the others (which provide their own paint ( ) implementations, rather than using the one provided by the basic L&F).*

The layer property indicates the frame's current layer, if it has been placed in a JLayeredPane. The resizable property indicates whether or not the frame can be resized by dragging its edges or corners, and selected indicates whether or not the frame has been selected (this will typically determine the color of the titlebar). Note that selected is a constrained property. Title contains the string to appear on the titlebar.

The Ul property holds the current L&F implementation for the frame, and UIClassID reflects the class ID for internal frames.

Finally, the warningString property, which is always null, is used to specify the string that should appear in contexts where the frame might be insecure. This is the technique used by java.awt. Window to display a string like "Warning: Applet Window" when a Java window is displayed from an applet. Since JInternalFrames are always fully enclosed by some other top-level container, this property is always null. . . .

Meet the Author

Robert Eckstein, an editor at O'Reilly, works mostly on Java books (notably Java Swing) and is also responsible for the XML Pocket Reference and Webmaster in a Nutshell, 2nd Edition. In his spare time he has been known to provide online coverage for popular conferences. He also writes articles for JavaWorld magazine. Robert holds bachelor's degrees in computer science and communications from Trinity University. In the past, he has worked for the USAA insurance company and more recently spent four years with Motorola's cellular software division. He is the co-author of Using Samba.

Marc Loy is a senior programmer at Galileo Systems, LLC, but his day job seems to be teaching Java and Perl to various companies — including Sun Microsystems. He has played with Java since the alpha days and can't find his way back to C. He is developing an interactive learning application at Galileo written entirely in Java. He received his master's degree in computer science at the University of Wisconsin-Madison, and still lives in Madison with his partner, Ron Becker. He does find time to relax by playing the piano and/or throwing darts, depending on how successful the day of teaching or programming was.

David Wood is Technical Director of Plugged In Software in Brisbane, Australia, where he works with a wonderful team producing Java custom software. In his eclectic career he has been a ship's navigator, deep sea salvage engineer, and aerospace project manager for the U.S. Navy, and consulted to Lawrence Livermore National Laboratory and Netscape. David enjoys hiking and sailing with his very patient wife and teaching his son Perl before he goes to kindergarten. David holds degrees in mechanical, electrical, aeronautical, and astronautical engineering from the U.S. Naval Postgraduate School and the Virginia Military Institute.

Customer Reviews

Average Review:

Post to your social network


Most Helpful Customer Reviews

See all customer reviews

Java Swing 4 out of 5 based on 0 ratings. 2 reviews.
Guest More than 1 year ago
I've looked at a bunch of books covering Swing, and this is the best I've seen. It gave me all I needed to write a pretty complex Java/Swing application.
Guest More than 1 year ago
There are some code errors in the first few programs that make them hard to understand. I had to go the the books website listing the errors to figure out what was wrong. Also, the author uses a lot of custom classes that implement or extend the java classes. This can be good or bad depending upon your skill level.