JFC Swing Tutorial, The: A Guide to Constructing GUIs, Second Edition

Book description

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.

    Table of contents

    1. Copyright
    2. The Java™ Series
      1. The Jini™ Technology Series
      2. The Java™ Series, Enterprise Edition
    3. Preface
      1. Acknowledgments
      2. About the Authors
    4. Before You Start
    5. 1. Getting Started with Swing
      1. About the JFC and Swing
        1. Which Releases Contain the Swing API?
        2. Which Swing Packages Should I Use?
      2. Compiling and Running Swing Programs
        1. Install the Latest Release of the Java 2 Platform
        2. Create a Program That Uses Swing Components
        3. Compile the Program
        4. Run the Program
          1. Solaris
          2. Microsoft Windows
        5. Running Programs Using Java Web Start
      3. Questions and Exercises
        1. Questions
        2. Exercises
        3. Answers
      4. Example Programs
    6. 2. Learning Swing by Example
      1. Example One: Your First Swing Program
      2. Example Two: SwingApplication
        1. Look and Feel
        2. Setting up Buttons and Labels
        3. Adding Components to Containers
        4. Adding Borders around Components
        5. Handling Events
      3. Example Three: CelsiusConverter
      4. Example Four: An Improved CelsiusConverter
        1. Adding HTML
        2. Adding an Icon
        3. Setting the Default Button
        4. Creating a Formatted Text Field
      5. Example Five: LunarPhases
        1. Using Layout Managers
        2. Compound Borders
        3. Combo Boxes
          1. Handling Events on a Combo Box
        4. Multiple Images
      6. Example Six: VoteDialog
        1. Radio Buttons
        2. Dialogs
      7. Summary
      8. Questions and Exercises
        1. Questions
        2. Exercises
        3. Answers
      9. Example Programs
    7. 3. Using Swing Components
      1. A Visual Index to Swing Components
      2. Using HTML in Swing Components
        1. Example One: HtmlDemo
        2. Example Two: ButtonHtmlDemo
      3. Using Top-Level Containers
        1. Top-Level Containers and Containment Hierarchies
        2. Adding Components to the Content Pane
        3. Adding a Menu Bar
        4. The Root Pane (The Missing Details)
      4. Using Models
        1. An Example: Converter
        2. For More Information
      5. The JComponent Class
        1. JComponent Features
          1. Tool Tips
          2. Borders
          3. Application-Wide Pluggable Look and Feel
          4. Custom Properties
          5. Support for Layout
          6. Support for Accessibility
          7. Support for Drag and Drop
          8. Double Buffering
          9. Key Bindings
        2. The JComponent API
      6. Using Text Components
        1. Text Component Features
          1. Associating Text Actions with Menus and Buttons
          2. Associating Text Actions with Keystrokes
          3. Implementing Undo and Redo
            1. Part 1: Remembering Undoable Edits
            2. Part 2: Implementing the Undo and Redo Commands
          4. Concepts: About Documents
          5. Implementing a Document Filter
          6. Listening for Changes on a Document
          7. Listening for Caret and Selection Changes
          8. Concepts: About Editor Kits
            1. DefaultEditorKit
            2. StyledEditorKit
            3. HTMLEditorKit
            4. RTFEditorKit
        2. The Text Component API
      7. Summary
      8. Questions and Exercises
        1. Questions
        2. Exercises
        3. Answers
      9. Example Programs
    8. 4. Laying Out Components within a Container
      1. A Visual Guide to Layout Managers
        1. BorderLayout
        2. BoxLayout
        3. CardLayout
        4. FlowLayout
        5. GridBagLayout
        6. GridLayout
        7. SpringLayout
      2. Using Layout Managers
        1. Setting the Layout Manager
        2. Adding Components to a Container
        3. Providing Size and Alignment Hints
        4. Putting Space between Components
          1. The layout manager
          2. Invisible components
          3. Empty borders
        5. Setting the Container's Orientation
        6. Choosing a Layout Manager
          1. Scenario: You need to display a component in as much space as it can get.
          2. Scenario: You need to display a few components in a compact row at their natural size.
          3. Scenario: You need to display a few components of the same size in rows and columns.
          4. Scenario: You need to display a few components in a row or column, possibly with varying amounts of space between them, custom alignment, or custom component sizes.
          5. Scenario: You need to display aligned columns, as in a form-like interface where a column of labels is used to describe text fields in an adjacent column.
          6. Scenario: You have a complex layout with many components.
      3. How Layout Management Works
      4. Creating a Custom Layout Manager
        1. void addLayoutComponent(String, Component)
        2. void removeLayoutComponent(Component)
        3. Dimension preferredLayoutSize(Container)
        4. Dimension minimumLayoutSize(Container)
        5. void layoutContainer(Container)
      5. Doing without a Layout Manager (Absolute Positioning)
      6. Summary
      7. Questions and Exercises
        1. Questions
        2. Exercises
        3. Answers
      8. Example Programs
    9. 5. Writing Event Listeners
      1. Some Event-Handling Examples
        1. Multiple Listeners Example
      2. General Information about Writing Event Listeners
        1. Design Considerations
        2. Getting Event Information: Event Objects
        3. Concepts: Low-Level Events and Semantic Events
        4. Event Adapters
        5. Inner Classes and Anonymous Inner Classes
        6. The EventHandler Class
      3. Listeners Supported by Swing Components
        1. Listeners That All Swing Components Support
          1. component listener
          2. focus listener
          3. key listener
          4. mouse listener
          5. mouse-motion listener
          6. mouse-wheel listener (introduced in 1.4)
          7. property-change listener
        2. Other Listeners That Swing Components Support
      4. Listener API Table
      5. Summary
      6. Questions and Exercises
        1. Questions
        2. Exercises
        3. Answers
      7. Example Programs
    10. 6. Performing Custom Painting
      1. How Swing Components Are Displayed
        1. How Painting Happens
        2. The Swing Painting Methods
        3. An Example of Painting
        4. Repainting Transparent Components
      2. Introduction to Painting Concepts
        1. The Coordinate System
        2. The Graphics Object and Graphics2D
      3. Implementing a Custom Component
        1. An Example of Custom Painting
      4. Summary
      5. Questions and Exercises
        1. Questions
        2. Exercises
        3. Answers
      6. Example Programs
    11. 7. Components Reference
      1. How to Make Applets
        1. Features Provided by JApplet
        2. Threads in Applets
        3. Using Images in a Swing Applet
        4. Embedding an Applet in an HTML Page
        5. The JApplet API
        6. Applet Examples
      2. How to Use Buttons
        1. How to Use the Common Button API
        2. How to Use JButton Features
        3. The Button API
        4. Examples That Use Buttons
      3. How to Use Check Boxes
        1. The Check Box API
        2. Examples That Use Check Boxes
      4. How to Use Color Choosers
        1. Another Example: ColorChooserDemo2
        2. Showing a Color Chooser in a Dialog
        3. Removing or Replacing the Preview Panel
        4. Creating a Custom Chooser Panel
          1. void buildChooser()
          2. void updateChooser()
          3. String getDisplayName()
          4. Icon getSmallDisplayIcon()
          5. Icon getLargeDisplayIcon()
        5. The Color Chooser API
        6. Examples That Use Color Choosers
      5. How to Use Combo Boxes
        1. Using an Uneditable Combo Box
        2. Handling Events on a Combo Box
        3. Using an Editable Combo Box
        4. Providing a Custom Renderer
        5. The Combo Box API
        6. Examples That Use Combo Boxes
      6. How to Make Dialogs
        1. An Overview of Dialogs
        2. The DialogDemo Example
        3. JOptionPane Features
        4. Creating and Showing Simple Dialogs
          1. showMessageDialog
          2. showOptionDialog
          3. JOptionPane (constructor)
          4. Component parentComponent
          5. Object message
          6. String title
          7. int optionType
          8. int messageType
          9. Icon icon
          10. Object[] options
          11. Object initialValue
        5. Customizing Button Text
        6. Getting the User's Input from a Dialog
        7. Stopping Automatic Dialog Closing
        8. The Dialog API
        9. Examples That Use Dialogs
      7. How to Use Editor Panes and Text Panes
        1. Using an Editor Pane to Display Text from a URL
        2. Editor Panes versus Text Panes
        3. An Example of Using a Text Pane
        4. The Editor Pane and Text Pane API
        5. Examples That Use Editor Panes and Text Panes
      8. How to Use File Choosers
        1. Example One: FileChooserDemo
        2. Example Two: FileChooserDemo2
        3. Using a File Chooser for a Custom Task
        4. Filtering the List of Files
          1. Built-in Filtering
          2. Application-Controlled Filtering
          3. User-Choosable Filtering
        5. Customizing the File View
          1. String getTypeDescription(File f)
          2. Icon getIcon(File f)
          3. String getName(File f)
          4. String getDescription(File f)
          5. Boolean isTraversable(File f)
        6. Providing an Accessory Component
        7. The File Chooser API
        8. Examples That Use File Choosers
      9. How to Use Formatted Text Fields
        1. Creating and Initializing Formatted Text Fields
        2. Setting and Getting the Field's Value
        3. Specifying Formats
        4. Using MaskFormatter
        5. Specifying Formatters and Using Formatter Factories
          1. Use the JFormattedTextField constructor that takes a Format argument.
          2. Use the JFormattedTextField constructor that takes a JFormattedTextField. AbstractFormatter argument.
          3. Set the value of a formatted text field that has no format, formatter, or formatter factory specified.
          4. Make the formatted text field use a formatter factory that returns customized formatter objects.
        6. The Formatted Text Field API
        7. Examples That Use Formatted Text Fields
      10. How to Make Frames (Main Windows)
        1. Creating and Showing Frames
        2. Specifying Window Decorations
        3. Responding to Window-Closing Events
          1. DO_NOTHING_ON_CLOSE
          2. HIDE_ON_CLOSE (the default for JDialog and JFrame)
          3. DISPOSE_ON_CLOSE (the default for JInternalFrame)
          4. EXIT_ON_CLOSE (defined in the JFrame class)
        4. The Frame API
        5. Examples That Use Frames
      11. How to Use Internal Frames
        1. Internal Frames versus Regular Frames
        2. Rules of Using Internal Frames
          1. You must set the size of the internal frame.
          2. As a rule, you should set the location of the internal frame.
          3. To add components to an internal frame, add them to its content pane.
          4. Dialogs that are internal frames should be implemented using JOptionPane or JInternalFrame, not JDialog.
          5. You must add an internal frame to a container.
          6. You need to call show or setVisible on internal frames.
          7. Internal frames fire internal frame events, not window events.
        3. The Internal Frame API
        4. Examples That Use Internal Frames
      12. How to Use Labels
        1. The Label API
        2. Examples That Use Labels
      13. How to Use Layered Panes
        1. Adding Components and Setting Component Depth
        2. Setting a Component's Position within Its Depth
        3. Laying Out Components in a Layered Pane
        4. The Layered Pane API
        5. Examples That Use Layered Panes
      14. How to Use Lists
        1. Initializing a List
        2. Selecting Items in a List
        3. Adding Items to and Removing Items from a List
        4. Writing a Custom Cell Renderer
        5. The List API
        6. Examples That Use Lists
      15. How to Use Menus
        1. The Menu Component Hierarchy
        2. Creating Menus
        3. Handling Events from Menu Items
        4. Enabling Keyboard Operation
        5. Bringing up a Popup Menu
        6. Customizing Menu Layout
        7. The Menu API
        8. Examples That Use Menus
      16. How to Use Panels
        1. An Example
        2. Setting the Layout Manager
        3. Adding Components
        4. The Panel API
        5. Examples That Use Panels
      17. How to Use Password Fields
        1. The Password Field API
        2. Examples That Use Password Fields
      18. How to Use Progress Bars
        1. JProgressBar
        2. ProgressMonitor
        3. ProgressMonitorInputStream
        4. Using Determinate Progress Bars
        5. Using Indeterminate Mode
        6. Using Progress Monitors
        7. Deciding Whether to Use a Progress Bar or a Progress Monitor
        8. The Progress Monitoring API
        9. Examples That Monitor Progress
      19. How to Use Radio Buttons
        1. The Radio Button API
        2. Examples That Use Radio Buttons
      20. How to Use Root Panes
        1. The Glass Pane
        2. The Layered Pane
        3. The Content Pane
        4. The Optional Menu Bar
        5. The Glass Pane
        6. The Layered Pane
        7. The Root Pane API
        8. Examples That Use Root Panes
      21. How to Use Scroll Panes
        1. How a Scroll Pane Works
        2. Setting the Scroll Bar Policy
        3. Providing Custom Decorations
        4. Implementing a Scrolling-Savvy Client
        5. Sizing a Scroll Pane
        6. Dynamically Changing the Client's Size
        7. The Scroll Pane API
        8. Examples That Use Scroll Panes
      22. How to Use Separators
        1. Using JSeparator
        2. The Separator API
        3. Examples That Use Separators
      23. How to Use Sliders
        1. Customizing Labels on a Slider
        2. Using a Formatted Text Field with a Slider
        3. The Slider API
        4. Examples That Use Sliders
      24. How to Use Spinners
        1. Using Standard Spinner Models and Editors
          1. SpinnerListModel
          2. SpinnerNumberModel
          3. SpinnerDateModel
        2. Specifying Spinner Formatting
        3. Creating Custom Spinner Models and Editors
        4. Detecting Spinner Value Changes
        5. The Spinner API
        6. Examples That Use Spinners
      25. How to Use Split Panes
        1. Setting the Components in a Split Pane
        2. Positioning the Divider and Restricting Its Range
        3. Nesting Split Panes
        4. The Split Pane API
        5. Examples That Use Split Panes
      26. How to Use Tabbed Panes
        1. The Tabbed Pane API
        2. Examples That Use Tabbed Panes
      27. How to Use Tables
        1. Creating a Simple Table
        2. Adding a Table to a Container
        3. Setting and Changing Column Widths
          3. AUTO_RESIZE_OFF
        4. Detecting User Selections
        5. Creating a Table Model
        6. Detecting Data Changes
        7. Concepts: Editors and Renderers
        8. Using a Combo Box as an Editor
        9. Using an Editor to Validate User-Entered Text
        10. Using Other Editors
        11. Using Custom Renderers
        12. Specifying Tool Tips for Cells
        13. Specifying Tool Tips for Column Headers
        14. Sorting and Otherwise Manipulating Data
        15. The Table API
        16. Examples That Use Tables
      28. How to Use Text Areas
        1. Customizing Text Areas
        2. The Text Area API
        3. Examples That Use Text Areas
      29. How to Use Text Fields
        1. The Text Field API
        2. Examples That Use Text Fields
      30. How to Use Tool Bars
        1. Creating Tool-Bar Buttons
        2. Customizing Tool Bars
        3. The Tool Bar API
        4. Examples That Use Tool Bars
      31. How to Use Tool Tips
        1. The Tool-Tip API
        2. Examples That Use Tool Tips
      32. How to Use Trees
        1. Creating a Tree
        2. Responding to Node Selection
        3. Customizing a Tree's Display
        4. Dynamically Changing a Tree
        5. Creating a Data Model
        6. The Tree API
        7. Examples That Use Trees
    12. 8. Layout Manager Reference
      1. How to Use BorderLayout
        1. The BorderLayout API
        2. Examples That Use BorderLayout
      2. How to Use BoxLayout
        1. BoxLayout Features
        2. Using Invisible Components as Filler
          1. Rigid Area
          2. Glue
          3. Custom Box.Filler
        3. Fixing Alignment Problems
        4. Specifying Component Sizes
        5. The Box Layout API
        6. Examples That Use Box Layouts
      3. How to Use CardLayout
        1. The CardLayout API
        2. Examples That Use CardLayout
      4. How to Use FlowLayout
        1. The FlowLayout API
        2. Examples That Use FlowLayout
      5. How to Use GridBagLayout
        1. Specifying Constraints
          1. gridx, gridy
          2. gridwidth, gridheight
          3. fill
          4. ipadx, ipady
          5. insets
          6. anchor
          7. weightx, weighty
        2. The Example Explained
        3. The GridBagLayout API
        4. Examples That Use GridBagLayout
      6. How to Use GridLayout
        1. The GridLayout API
        2. Examples That Use GridLayout
      7. How to Use SpringLayout
        1. How Spring Layouts Work
        2. Example: SpringDemo
        3. Springs and Component Size
        4. Alternative Expressions
        5. Utility Methods for Grids
        6. The SpringLayout API
        7. Examples That Use SpringLayout
    13. 9. Other Swing Features Reference
      1. How to Use Actions
        1. The Action API
        2. Examples That Use Actions
      2. How to Support Assistive Technologies
        1. Rules for Supporting Accessibility
        2. Testing for Accessibility
        3. Setting Accessible Names and Descriptions on Components
        4. How Accessibility Works
        5. Making Custom Components Accessible
        6. The Accessibility API
        7. Examples That Use the Accessibility API
      3. How to Use Borders
        1. The BorderDemo Example
        2. Using the Borders Provided by Swing
        3. Creating Custom Borders
        4. The Border API
        5. Examples That Use Borders
      4. How to Use Drag and Drop and Data Transfer
        1. A Visual Guide to Drag-and-Drop Cursor Icons
        2. Introduction to Data Transfer Support
        3. A Simple Example: Adding DnD to JLabel
        4. Extending Default DnD Support
        5. Specifying the Data Format
        6. Importing a New Flavor: Color
        7. Replacing Default Support: Color and Text
        8. Importing a New Flavor: Files
        9. Data Transfer with a Custom Component
        10. Data Transfer with a Custom DataFlavor
        11. Adding Cut/Copy/Paste Support
        12. The Data Transfer API
        13. Examples That Use Data Transfer
      5. How to Use the Focus Subsystem
        1. Introduction to the Focus Subsystem
        2. Validating Input
        3. Making a Custom Component Focusable
        4. Customizing Focus Traversal
        5. Tracking Focus Changes to Multiple Components
        6. Timing Focus Transfers
        7. The Focus API
        8. Examples That Use Focus
      6. How to Use Icons
        1. A More Complex Image Icon Example
        2. Loading Images Using getResource
        3. Loading Images into Applets
        4. Improving Perceived Performance When Loading Image Icons
          1. Providing Dimmed Icons
          2. Lazy Image Loading
          3. Background Image Loading
          4. Status Updates
          5. Caching
        5. Creating a Custom Icon Implementation
        6. The Image Icon API
        7. Examples That Use Icons
      7. How to Use Key Bindings
        1. How Key Bindings Work
        2. How to Make and Remove Key Bindings
        3. The Key Binding API
        4. Examples That Use Icons
      8. How to Set the Look and Feel
        1. Programmatically Setting the Look and Feel
          1. UIManager.getCrossPlatformLookAndFeelClassName()
          2. UIManager.getSystemLookAndFeelClassName()
          3. "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"
          4. "javax.swing.plaf.metal.MetalLookAndFeel"
          5. "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
          6. "com.sun.java.swing.plaf.motif.MotifLookAndFeel"
        2. Specifying the Look and Feel: Command Line
        3. Specifying the Look and Feel: swing.properties
        4. How the UI Manager Chooses the Look and Feel
        5. Changing the Look and Feel after Startup
      9. How to Use Threads
        1. The Event-Dispatching Thread
        2. Using the invokeLater Method
        3. Using the invokeAndWait Method
        4. Using Threads to Improve Performance
        5. Using the SwingWorker Class
      10. How to Use Timers
        1. The Timer API
        2. Examples That Use Timers
    14. 10. Event Listeners Reference
      1. How to Write an Action Listener
        1. The Action Listener API
        2. Examples That Use Action Listeners
      2. How to Write a Caret Listener
        1. The Caret Listener API
        2. Examples That Use Caret Listeners
      3. How to Write a Change Listener
        1. The Change Listener API
        2. Examples That Use Change Listeners
      4. How to Write a Component Listener
        1. The Component Listener API
        2. Examples That Use Component Listeners
      5. How to Write a Container Listener
        1. The Container Listener API
        2. Examples That Use Container Listeners
      6. How to Write a Document Listener
        1. The Document Listener API
        2. Examples That Use Document Listeners
      7. How to Write a Focus Listener
        1. The Focus Listener API
        2. Examples That Use Focus Listeners
      8. How to Write an Internal Frame Listener
        1. The Internal Frame Listener API
        2. Examples That Use Internal Frame Listeners
      9. How to Write an Item Listener
        1. The Item Listener API
        2. Examples That Use Item Listeners
      10. How to Write a Key Listener
        1. The Key Listener API
        2. Examples That Use Key Listeners
      11. How to Write a List Data Listener
        1. The List Data Listener API
        2. Examples That Use List Data Listeners
      12. How to Write a List Selection Listener
        1. The List Selection Listener API
        2. Examples That Use List Selection Listeners
      13. How to Write a Mouse Listener
        1. The Mouse Listener API
        2. Examples That Use Mouse Listeners
      14. How to Write a Mouse-Motion Listener
        1. The Mouse-Motion Listener API
        2. Examples That Use Mouse-Motion Listeners
      15. How to Write a Mouse Wheel Listener
        1. The Mouse Wheel Listener API
        2. Examples That Use Mouse Wheel Listeners
      16. How to Write a Property-Change Listener
        1. The Property-Change Listener API
        2. Examples That Use Property-Change Listeners
      17. How to Write a Table Model Listener
        1. The Table Model Listener API
        2. Examples That Use Table Model Listeners
      18. How to Write a Tree Expansion Listener
        1. The Tree Expansion Listener API
        2. Examples That Use Tree Expansion Listeners
      19. How to Write a Tree Model Listener
        1. The Tree Model Listener API
        2. Examples That Use Tree Model Listeners
      20. How to Write a Tree Selection Listener
        4. The Tree Selection Listener API
        5. Examples That Use Tree Selection Listeners
      21. How to Write a Tree-Will-Expand Listener
        1. The Tree-Will-Expand Listener API
        2. Examples That Use Tree-Will-Expand Listeners
      22. How to Write an Undoable Edit Listener
        1. The Undoable Edit Listener API
        2. Examples That Use Undoable Edit Listeners
      23. How to Write Window Listeners
        1. The Window Listener API
        2. Examples That Use Window Listeners
    15. Troubleshooting Reference
      1. Java Web Start Troubleshooting
      2. Solving Common Component Problems
      3. Solving Common Layout Problems
      4. Solving Common Event-Handling Problems
      5. Solving Common Painting Problems
      6. Solving Common Problems Using Other Swing Features
    16. The JFC Swing Tutorial CD
      1. CD-ROM Warranty

    Product information

    • Title: JFC Swing Tutorial, The: A Guide to Constructing GUIs, Second Edition
    • Author(s):
    • Release date: February 2004
    • Publisher(s): Addison-Wesley Professional
    • ISBN: 9780201914672