The JFC Swing Tutorial: A Guide to Constructing GUIs / Edition 2

Paperback (Print)
Buy Used
Buy Used from
(Save 41%)
Item is in good condition but packaging may have signs of shelf wear/aging or torn packaging.
Condition: Used – Good details
Used and New from Other Sellers
Used and New from Other Sellers
from $2.44
Usually ships in 1-2 business days
(Save 96%)
Other sellers (Paperback)
  • All (13) from $2.44   
  • New (4) from $44.26   
  • Used (9) from $2.44   


The JFC Swing Tutorial is back, fully revised and updated to include the latest revisions to the JFC Swing API and the Java™ 2 platform. In this book, authors and Java experts Kathy Walrath, Mary Campione, Alison Huml, and Sharon Zakhour—working closely with the Sun Microsystems Swing team—explore the ins and outs of creating GUIs with Swing components.

This task-oriented, example-driven tutorial allows you to create user interfaces that work without change on multiple platforms, appearing and performing as well as or better than native interfaces. Leveraging the full power of the latest edition of the Java 2 platform, the authors bring the art of GUI creation to life with content new to this edition. This includes an easy-to-use tabbed reference section, new introductory chapters, and coverage of newer features such as JSpinner, JFormattedTextField, JProgressBar, mouse wheel support, the rearchitected focus subsystem, and improved support for drag and drop.

Coverage includes:

  • Introductory material for developers getting started with Swing, including sections on basic components such as text fields, labels, and buttons, as well as on using images
  • The latest advice from the Swing team about thread safety
  • Advanced Swing material, including changing key bindings, manipulating the focus, using data models, and adding painting code that uses the powerful Java 2D™ API
  • How-to discussions on using individual components and containers, including advanced components such as tables, trees, and text editors
  • Over 150 complete, working code examples

For the novice or experienced Java developer looking to create robust, powerful, and visually stunning GUIs, The JFC Swing Tutorial, Second Edition, is an indispensable tutorial and reference.

Read More Show Less

Editorial Reviews

A book/CD-ROM package using a task-oriented, example-driven approach to introduce fundamental concepts and applications for using the new Swing components provided by the Java Foundation Classes (JFC). An appendix lists source code for example applications and applets. The CD-ROM includes Swing and Java software, and a hypertext version of three Java tutorial books. Can be used as a tutorial or a reference. Assumes some experience writing programs using the Java platform. The authors are senior technical writers at Sun Microsystems, Inc. Annotation c. Book News, Inc., Portland, OR (
Read More Show Less

Product Details

  • ISBN-13: 9780201914672
  • Publisher: Addison-Wesley
  • Publication date: 2/24/2004
  • Series: Java Series
  • Edition description: REV
  • Edition number: 2
  • Pages: 784
  • Product dimensions: 7.40 (w) x 9.10 (h) x 1.70 (d)

Meet the Author

Kathy Walrath is a senior technical writer on the Swing team at Sun Microsystems. After graduating from the University of California, Berkeley, with a B.S. in Electrical Engineering and Computer Science, Kathy wrote extensively about Unix, Mach, and NextStep. Since 1993, Kathy has been writing specifications and how-to guides for the Java platform. Mary Campione was formerly a senior technical writer at Sun Microsystems, where she started writing about the Java platform in 1995. Mary graduated from California Polytechnic, San Luis Obispo, with a B.S. in Computer Science and has worked as both a technical writer and programmer.

Alison Huml is a technical writer at Sun Microsystems, where she joined The Java Tutorial team in 1997 and also works with the Security team. Alison received her B.A. in English from the University of California, Berkeley, and is currently pursuing her master's degree in Computer Science at Mills College.

Sharon Zakhour, the Java Tutorial team lead, has worked at Sun as a senior technical writer for seven years. She graduated from UC Berkeley with a B.A. in Computer Science and has worked as a programmer, developer support engineer, and technical writer for more than 20 years.

Read More Show Less

Read an Excerpt


This volume of The Java(TM) Tutorial tells you how to write GUIs that use the Java(TM) Foundation Classes (JFC) "Swing" components. You can use the information in this book both with the Java 2 platform (Standard Edition, v 1.2 and compatible versions) and with JDK(TM) 1.1 (with additional JFC 1.1 libraries).

The online form of The Java(TM) Tutorial has covered the Swing components since their first public early access release—Swing 0.2, which came out in July, 1997. Through the many early access releases, the Tutorial kept pace with API changes and additions. Readers and reviewers kept us on our toes, helping us improve each page tremendously. However, readers often requested a printed version of the online material. This book is that version.

About This Book's Structure

The hyperlinked origins of this book may be evident as you read it. For instance, underlined phrases throughout this book mimic online links. A link to material within this book is followed by the appropriate page number. A link to material outside this book, such as to the JDK API documentation, is accompanied by a footnote that contains a URL. Other evidence of this book's online origin can be found on the first page of each lesson and major section, which provides the URL where the lesson or section can be found in the online Tutorial.

You might be wondering why we use the terms "trails" and "lessons." We know that people don't learn linearly. People learn by posing a problem, solving it, uncovering other problems, solving them, and learning information as the need arises. Our original vision for the online Tutorial was toencourage and enable this type of thinking and learning. We envisioned a mountain of ski trails, where at any junction, a reader could choose the most interesting or appropriate path at that time. But we also needed some sort of structure and organization, so we created a two-tiered hierarchy: trails at the top level and lessons within them. This book consists of the largest Tutorial trail, Creating a GUI with JFC/Swing.


We would like to thank every member of the Swing project. They're a great team of people who do excellent work and are fun to be around. For help with general technical issues, we depended on two people. Hans Muller, the Swing project lead, provided not only reviews of individual sections, but also gave sound advice and help with overall issues. Amy Fowler reviewed individual sections and, as someone with broad knowledge of both the AWT and Swing, helped us to correctly describe such architectural features as graphics support.

Reviewers of individual how-to sections include Philip Milne, who gave masterful reviews of the table and list portions and supplied the sorter example. Georges Saab reviewed the menu- and action-related discussions. Scott Violet provided invaluable help with the text and tree pages. Jeff Dinkins reviewed several sections and also provided quality-of-life enhancements such as tile samples and miniature phone lists.

Earl Johnson and Peter Korn, from the accessibility team, gently prodded us into covering accessibility well. Their demos, coaching, and careful review of the accessibility section helped us improve it greatly.

It's always a pleasure working with Pat Chan, whose early review of this book helped us determine its scope and approach.

Other reviewers and Swing team members that we'd like to thank include Mike Albers, Tim Prinzing, Tom Santos, Steve Wilson, Rich Schiavi, Tom Ball, Jim Graham, and Hania Gajewska. Rick Levenson, the original manager of the Swing project, was very supportive. We look forward to working with the new manager, Howard Rosen.

Alison Huml performed production duties on this book, juggling coursework, paid work, RSI, and a household move. Without her, this book wouldn't exist. She also drew most of the pictures in this book, with the exception of the cartoony ones, which were drawn by Kathy's sweetheart Nathan.

Jennifer Ball helped us at crucial points, doing such tasks as checking API tables and code snippets, and converting graphics examples.

Lisa Friendly, our manager and series editor, gave us the freedom and support necessary to do our work—and enjoy it. Stans Kleijnen and Jon Kannegaard, respectively the director of product engineering and the vice president of the Java platform, also contributed to an atmosphere that let everyone do their best.

We'd also like to thank the team at Addison-Wesley: Mike Hendrickson, Sarah Weaver, Evelyn Pyle, Jacquelyn Young, Marina Lang, and Julie DeBaggis. They've been a pleasure to work with.

Finally, thank you to our readers.

Read More Show Less

Table of Contents


Before You Start.

1. Getting Started with Swing.

About the JFC and Swing.

Compiling and Running Swing Programs.

Questions and Exercises.

Example Programs.

2. Learning Swing by Example.

Example One: Your First Swing Program.

Example Two: SwingApplication.

Example Three: CelsiusConverter.

Example Four: An Improved CelsiusConverter.

Example Five: LunarPhases.

Example Six: VoteDialog.


Questions and Exercises.

Example Programs.

3. Using Swing Components.

A Visual Index to Swing Components.

Using HTML in Swing Components.

Using Top-Level Containers.

Using Models.

The JComponent Class.

Using Text Components.


Questions and Exercises.

Example Programs.

4. Laying Out Components within a Container.

A Visual Guide to Layout Managers.

Using Layout Managers.

How Layout Management Works.

Creating a Custom Layout Manager.

Doing without a Layout Manager (Absolute Positioning).


Questions and Exercises.

Example Programs.

5. Writing Event Listeners.

Some Event-Handling Examples.

General Information about Writing Event Listeners.

Listeners Supported by Swing Components.

Listener API Table.


Questions and Exercises.

Example Programs.

6. Performing Custom Painting.

How Swing Components Are Displayed.

Introduction to Painting Concepts.

Implementing a Custom Component.


Questions and Exercises.

Example Programs.

7. Components Reference.

How to Make Applets.

How to Use Buttons.

How to Use Check Boxes.

How to Use Color Choosers.

How to Use Combo Boxes.

How to Make Dialogs.

How to Use Editor Panes and Text Panes.

How to Use File Choosers.

How to Use Formatted Text Fields.

How to Make Frames (Main Windows).

How to Use Internal Frames.

How to Use Labels.

How to Use Layered Panes.

How to Use Lists.

How to Use Menus.

How to Use Panels.

How to Use Password Fields.

How to Use Progress Bars.

How to Use Radio Buttons.

How to Use Root Panes.

How to Use Scroll Panes.

How to Use Separators.

How to Use Sliders.

How to Use Spinners.

How to Use Split Panes.

How to Use Tabbed Panes.

How to Use Tables.

How to Use Text Areas.

How to Use Text Fields.

How to Use Tool Bars.

How to Use Tool Tips.

How to Use Trees.

8. Layout Manager Reference.

How to Use BorderLayout.

How to Use BoxLayout.

How to Use CardLayout.

How to Use FlowLayout.

How to Use GridBagLayout.

How to Use GridLayout.

How to Use SpringLayout.

9. Other Swing Features Reference.

How to Use Actions.

How to Support Assistive Technologies.

How to Use Borders.

How to Use Drag and Drop and Data Transfer.

How to Use the Focus Subsystem.

How to Use Icons.

How to Use Key Bindings.

How to Set the Look and Feel.

How to Use Threads.

How to Use Timers.

10. Event Listeners Reference.

How to Write an Action Listener.

How to Write a Caret Listener.

How to Write a Change Listener.

How to Write a Component Listener.

How to Write a Container Listener.

How to Write a Document Listener.

How to Write a Focus Listener.

How to Write an Internal Frame Listener.

How to Write an Item Listener.

How to Write a Key Listener.

How to Write a List Data Listener.

How to Write a List Selection Listener.

How to Write a Mouse Listener.

How to Write a Mouse-Motion Listener.

How to Write a Mouse Wheel Listener.

How to Write a Property-Change Listener.

How to Write a Table Model Listener.

How to Write a Tree Expansion Listener.

How to Write a Tree Model Listener.

How to Write a Tree Selection Listener.

How to Write a Tree-Will-Expand Listener.

How to Write an Undoable Edit Listener.

How to Write Window Listeners.

Appendix. Troubleshooting Reference.

Java Web Start Troubleshooting.

Solving Common Component Problems.

Solving Common Layout Problems.

Solving Common Event-Handling Problems.

Solving Common Painting Problems.

Solving Common Problems Using Other Swing Features.


The JFC Swing Tutorial CD Contents.

Read More Show Less

First Chapter

Chapter 17: Solving Common Component Problems

This chapter discusses problems that you might encounter while using components. If you don't find your problem in this chapter, consult the following sections:
  • Solving Common Component Problems (page 337)
  • Solving Common Layout Problems (page 385)
  • Writing Event Listeners (page 439)
  • Solving Common Graphics Problems (page 573)
  • Solving Common Conversion Problems (page 603)
Problem: I can't make HTML tags work in my labels or buttons, etc. See JButton Features (page 172) for an example.
  • Make sure your program is running in a release that supports HTML text in the desired component.
  • JCheckBox and JRadioButton don't support HTML yet. We don't know yet when that support will be added.
  • If you can't guarantee that your program will be executed only with a release that supports HTML text in the desired component, don't use that feature!
Problem: Certain areas of the content pane look weird when they're repainted.
  • If you set the content pane, make sure it's opaque. JPanel and JDesktopPane make good content panes because they're opaque by default. See Adding Components to the Content Pane (page 79) for details.
  • If one or more of your components performs custom painting, make sure you implemented it correctly. See Solving Common Graphics Problems (page 573) for help.
  • You might have a thread safety problem. See the next entry.
Problem: My program is exhibiting weird symptoms that sometimes seem to be related to timing.
  • Make sure your code is thread-safe. See Threads and Swing (page 45) for details.
Problem: The scroll bar policies don't seem to be working as advertised.
  • Some Swing releases contain bugs in the implementations for the VERTICAL_SCROLLBAR_AS_NEEDED and the HORIZONTAL_SCROLLBAR_AS_NEEDED policies. If feasible for your project, use the most recent release of Swing.
  • If the scroll pane's client can change size dynamically, the program should set the client's preferred size and then call revalidate on the client.
  • Make sure you specified the policy you intended for the orientation you intended.
Problem: My scroll pane has no scroll bars.
  • If you want a scroll bar to appear all the time, specify either VERTICAL_SCROLLBAR_ALWAYS or HORIZONTAL_SCROLLBAR_ALWAYS for the scroll bar policy as appropriate.
  • If you want the scroll bars to appear as needed, and you want to force the scroll bars to be needed when the scroll pane is created, you have two choices: either set the preferred size of scroll pane or its container, or implement a scroll-savvy class and return a value smaller than the component's standard preferred size from the getPreferred-ScrollableViewportSize method. Refer to Sizing a Scroll Pane (page 122) for information.
Problem: The divider in my split pane won't move!
  • You need to set the minimum size of at least one of the components in the split pane. Refer to Positioning the Divider and Restricting Its Range (page 129) for information.
Problem: The setDividerLocation method doesn't work.
  • In some releases of Swing, there is a bug whereby a call to setDividerLocation doesn't work unless the split pane is already on screen. For information and possible workarounds, see bug #4101306 and bug #4182558 in the Bug Parade at the Java Developer's Connection at
Problem: The borders on nested split panes look too wide.
  • If you nest split panes, the borders accumulate -- the border of the inner split panes display next to the border of the outer split pane causing borders that look extra wide. The problem is particularly noticeable when nesting many split panes. The workaround is to set the border to null on any split pane that is placed within another split pane. For information, see bug #4131528 in the Bug Parade at the Java Developer's Connection online at
Problem: The buttons in my tool bar are too big.
  • Try reducing the margin for the buttons. For example:
    button.setMargin(new Insets(0,0,0,0));
Problem: The components in my layered pane aren't layered correctly. In fact, the layers seem to be inversed -- the lower the depth the higher the component.
  • This can happen if you use an int instead of an Integer when adding components to a layered pane. To see what happens, make the following change to Layered-PaneDemo:
    Change this...layeredPane.add(label, new Integer(i));

    to this...layeredPane.add(label, i);
Problem: The method call colorChooser.setPreviewPanel(null) does not remove the color chooser's preview panel as expected.
  • A nu11 argument specifies the default preview panel. To remove the preview panel, specify a standard panel with no size, like this: colorChooser.setPreviewPanel(new JPanel());
Read More Show Less


This edition of The Java™ Tutorial tells you how to write GUIs that use the Java Foundation Classes (JFC) “Swing” components. In this book we cover the most recent release of the Java 2 platform (v1.4.2 as of this printing). We also include information valuable to programmers using earlier releases and discuss Swing enhancements planned for the near future.

The online form of The Java Tutorial has covered the Swing components since their first public early-access release—Swing 0.2, which came out in July 1997. Throughout the early releases, the Tutorial kept pace with API changes and additions. Readers and reviewers kept us on our toes, helping us improve each page tremendously. Although this book has its roots in the online version, this edition has been reorganized and rewritten.

Numerous improvements have been made. First, this book uses current API (v1.4.2), has six new introductory chapters, and has an easy-to-use tabbed reference section. It reflects lessons learned by the Swing team in the years since the introduction of Swing components. Second, we cover newer features such as JFormattedTextField, JSpinner, indeterminate JProgressBar, mouse wheel support, the rearchitected focus subsystem, and improved support for drag and drop.

The book and CD contain more than 150 complete, working examples. The authors have worked closely with the Swing team to ensure that the code and discussions reflect recommended usage. The Swing component set has now been out for several years now and has a mature API. That, combined with the years of experience the Swing writers and engineers have had with the API, enables us to create the definitive introduction and guide for both inexperienced and advanced programmers who use Swing components.

Read More Show Less

Customer Reviews

Be the first to write a review
( 0 )
Rating Distribution

5 Star


4 Star


3 Star


2 Star


1 Star


Your Rating:

Your Name: Create a Pen Name or

Barnes & Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation


  • - By submitting a review, you grant to Barnes & and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Terms of Use.
  • - Barnes & reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously

    If you find inappropriate content, please report it to Barnes & Noble
    Why is this product inappropriate?
    Comments (optional)