John Zukowski's Definitive Guide to Swing for Java 2 JOHN ZUKOWSKI APress Media, LLC John Zukowski's Definitive Guide to Swing for Java 2 Copyright by John Zukowski ~1999 Originally published by Apress in 1999 Ali rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. Library of Congress Cataloging-in-Publication Data Zukowski, John. [Definitive Swing Guide] John Zukowski's Definitive Swing Guide 1 John Zukowski. p. cm. 1. Java (Computer program language) 2. Swing (Computer file) I. Title. QA76.73.J38Z8493 1999 99-34562 OOS.13'3-dc21 CIP Additional material to this book can be downloaded from http://extras.springer.com ISBN 978-1-893115-02-6 ISBN 978-1-4302-5251-1 (eBook) DOI 10.1007/978-1-4302-5251-1 1 2 3 4 5 6 7 8 9 10 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a.trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Project Manager: Nancy DelFavero Proofreaders: Valerie Cover and Project Editor: Carol Lombardi Christine Sabooni Technical Reviewers: Gary Cornell, Production Assistance: Lori Ash Randy Kahle, Blake Ragsdell, Tim Rohaly, Indexer: Nancy Humphreys and Laurence Vanhelsuwe Cover and Interior Design: Derek Yee Design Copyeditor: Mark Woodworth In the United States, phone 1-800-SPRINGER; [email protected]; http://www.springer-ny.com For information on translations, please contact APress directly: APress, 6400 Hollis Street, Suite 9, Emeryville, CA 94608 Phone: 510/595-3110; Fax: 510/595-3122; [email protected]; www.apress.com The information in this book is distributed on an ''As Is" hasis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author nor APress shal1 have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. ii Author's Acknowledgments WORKING WITH APRESS ON THIS BOOK has been fim. 1 would like to personally thank everyone who helped along the way, both in the formation of a new technical publisher and for the help in converting my rough manuscript into the material you have in your hands today. At APress, 1 would first like to thank Gary Cornell, Bill Pollock, and Dan Appleman for allowing me to work with them at such an early stage of company development. Now that we have Dan Appleman's book and my book, we can work at filling some spaces between the N.s and Z's. 1 would especially like to thank Nancy DelFavero and Carole Lombardi for their help in shaping what you're reading. Special thanks to the technical editors, Laurence Vanhelsuwe, Blake Ragsdell, Gary Cornell, Tim Rohaly, and Randy Kahle, who helped to ensure the technical accuracy of the content. Any remaining technical inaccura cies are my fault alone. On the graphics side, thanks to Derek Yee Design for helping to convert my rough sketches, mostly in Visio, to the quality illustrations you see in this book. For some of the images used in the example programs, 1 would like to thank Dean Jones for the JavaLobby Foundation Applications -Icon Collection (http:/ /webart.javalobby.org/jlicons/) and Deb Felts at the Image Addict's Attic (http://members.xoom.com/imageaddictl). (There is a reason why 1 am nota graphic artist.) They retain the copyrights on their respective images, and the images are used with permission. For their ideas and assistance during development, 1 would like to thank Joe Sam Shirah, Wong Kok Wai, Tomoko Iwama, Carlos Lucasius and the Licensee Support team in Ontario, the Swing! Team, and everyone at MageLang Institute. This book is that much better because of them. As always, 1 am grateful to my wife, Lisa, for her never-ending patience dur ing the development of this book and aur playful pup, Jaeger, who helps keep me sane (sometimes). Thanks to Mom and Dad, too, even though they don't remem ber getting me thatVic 20. iii About the Author JOHN ZUKOWSKI IS A WELL-KNOWN FIGURE in the Java community. He is a popular columnist for ]ava World magazine and a member of the ]ava World Senior Advisory Board. Zukowski provides significant content for Sun's Java Developer Connection and is the guide for Java at About.com. In addition, he is a faculty member of the MageLang Institute, an instructor-led training firm that is the leading provider of advanced Java training to the computer industry. He is also the author of]avaAWT Reference (O'Reilly), Mastering]ava 2 (Sybex), and Borland's ]Builder: No Experience Required (Sybex). iv Contents Introduction ..................................................................................................... 1 Development Versions ........................................................................................2 How This Book Is Structured .......................................................................2 Reading the UML Diagrams .............................................................................. 4 Support .....................................................................................................................5 Chapter 1 Swing Overview .................................................................... ? AWT Component Replacements .......................................................................... 8 New Components ................................................................................................... 10 Event Handling and Layout Management ................................................. 14 Undo Framework ................................................................................................... 15 Using Swing Classes with JDK 1.1 .......................................................... 16 SwingSet Demonstration ................................................................................. 17 JComponent to Page Mapping ........................................................................ 18 Summary ................................................................................................................... 19 Chapter 2 Event Handling with the Swing Component Set ..................................................................... 21 Delegation-Based Event Handling ............................................................. 21 Using Event Listeners as Observers ................................................................. 23 A Quick Look at the JFrame Class .................................................................... 26 Creating an ExitableJFrame Class .................................................................... 27 Using SwingUtilities for Mouse Button Identification ................................... 29 Using Property Change Listeners as Observers .............................................. 30 Managing Listener Lists .................................................................................... 33 Swing-Specific Event Handling ................................................................. 39 Interface Action ................................................................................................ .40 Class AbstractAction ......................................................................................... 42 Class KeyStroke ................................................................................................. 49 Using Mnemonics and Accelerators ................................................................ 53 V Swing Focus Management ................................................................................. 53 Examining Focus Cycles ................................................................................... 58 Customizing Component-Level Focus Management .................................... 60 Setting the Next Focusable Component ......................................................... 61 Class FocusManager and DefaultFocusManager ........................................... 62 WritingYour Own Focus Manager ................................................................... 64 Multithreaded Swing Event Handling ..................................................... 64 Using SwingUtilities for Thread Safety ............................................................ 65 Class Timer ........................................................................................................ 66 Summary ................................................................................................................... 69 Chapter 3 The Model-View-Controller Architecture ...................................................................................................7 1 MVC Communications .......................................................................................... 71 Sharing Data Models ........................................................................................ 73 Understanding the Predefined Data Models ........................................ 75 Summary ................................................................................................................... 77 Chapter 4 Core Swing Components ............................................... 79 Class JComponent ............................................................................................... 79 Component Pieces ............................................................................................ 80 JComponent Properties .................................................................................... 85 Handling JComponent Events ......................................................................... 89 Class JTool Tip ................................................................................................... 93 Creating a JToolTip ............................................................................................ 93 Creating Customized JToolTip Objects ........................................................... 94 Displaying Positional ToolTip Text .................................................................. 95 Customizing JToolTip Look and Feei .............................................................. 96 Class ToolTipManager ...................................................................................... 97 ToolTipManager Properties .............................................................................. 97 Class JLabe1 ........................................................................................................9 9 Creating a JLabel ............................................................................................. 100 JLabel Properties ............................................................................................. 10 0 Handling JLabel Events .................................................................................. 103 Customizing JLabel Look and Feel ................................................................ 103 vi Interface Icon ................................................................................................. 104 Creating an lcon ............................................................................................... 104 Using an Icon ................................................................................................... 106 Class Imagelcon .............................................................................................. 106 Class GrayFilter ......................................................................................· . ........ 110 Class AbstractButton ...................................................................................... 110 AbstractButton Properties .............................................................................. 111 HandlingAbstractButton Events ................................................................... 116 Listening to AbstractButton Events with a PropertyChangeListener ......... 116 Class Button ...................................................................................................... 117 Creating a JButton ........................................................................................... 118 JButton Properties ........................................................................................... 118 Handling JButton Events ................................................................................ 120 Listening to JButton Events with an ActionListener .................................... 120 Customizing a JButton Look and Feel... ........................................................ 122 Class JPane1 ...................................................................................................... 122 Creating a JPanel ............................................................................................. 123 Using a JPanel .................................................................................................. 124 Customizing JPanel Look and Feel ................................................................ 126 Summary ................................................................................................................. 126 Chapter 5 Toggle Buttons ................................................................ 127 Class ToggleButtonModel ............................................................................. 127 Class ButtonGroup ........................................................................................... 12B Class JToggleButton ...................................................................................... 131 Creating JToggleButton Components ........................................................... 132 JToggleButton Properties ................................................................................ 133 Handling JToggleButton Selection Events .................................................... 134 Customizing a JToggleButton Look and Feel.. .............................................. 138. Class JCheckBox ............................................................................................... 139 Creating JCheckBox Components ................................................................. 140 JCheckBox Properties ..................................................................................... 141 Handling JCheckBox Selection Events .......................................................... 143 Customizing a JCheckBox Look and Feel... ................................................... 146 Class JRadioButton ........................................................................................ 148 Creating JRadioButton Components ............................................................. 148 JRadioButton Properties ................................................................................. 150 Grouping JRadioButton Components in a ButtonGroup ............................ 150 Handling JRadioButton Selection Events ..................................................... 153 Customizing a JRadioButton Look and Feel... .............................................. 162 Summary ................................................................................................................. 163 vii Chapter 6 Swing Menus and Toolbars ..................................... 165 Working with Menus ........................................................................................ 166 Getting Started ................................................................................................ 166 Menu Class Hierarchy. ...................................................•................................ 170 Class JMenuBar ............................................................................................... 171 SingleSelectionModel Interface ..................................................................... 177 Class JMenultem ............................................................................................. 178 Class JMenu ..................................................................................................... 185 Class JSeparator. .............................................................................................. 192 Class JPopupMenu .......................................................................................... 194 Class JCheckBoxMenultem ............................................................................ 203 Class JRadioButtonMenultem ....................................................................... 209 Creating Custom MenuElement Components ............................................. 214 Working with Toolbars: Class JToolBar ............................................ 220 Creating ]ToolBar Components ..................................................................... 220 Adding Components to a JToolBar ................................................................ 221 ]ToolBar Properties ......................................................................................... 222 Handling ]ToolBar Events ............................................................................... 223 Customizing JToolBar Look and Feei ............................................................ 223 A Complete JToolBar Usage Example ............................................................ 224 JToolBar.Separator .......................................................................................... 226 Summary ................................................................................................................. 226 Chapter 7 Borders ...................................................................................2 29 Some Bas ies on Working with Borders ................................................. 229 Exploring the Border lnterface ...................................................................... 231 Introducing BorderFactory. ............................................................................ 233 Starting with AbstractBorder .......................................................................... 235 Examining the Predefined Borders ................................................................ 236 Creat ing Your Own Borders ........................................................................ 251 Summary ................................................................................................................. 253 Chapter 8 Root Pa ne Containers ................................................ 255 Class JRootPane ............................................................................................... 255 Creating a JRootPane ...................................................................................... 257 JRootPane Properties ...................................................................................... 257 Interface RootPaneContainer ........................................................................ 258 Class JLayeredPane ......................................................................................... 259 viii Class JFrame ...................................................................................................... 262 Creating a JFrame ............................................................................................ 262 JFrame Properties ........................................................................................... 263 Adding Components to a JFrame .................................................................. 264 Handling JFrame Events ................................................................................. 265 Extending JFrame ............................................................................................ 266 Class JWindow .................................................................................................... 267 Creating a JWindow ........................................................................................ 267 JWindow Properties ........................................................................................ 268 Handling JWindow Events .............................................................................. 268 Extending JWindow. ........................................................................................ 268 Class JDialog .................................................................................................... 269 Creating a }Dialog ........................................................................................... 269 }Dialog Properties ........................................................................................... 270 Handling }Dialog Events ................................................................................. 271 Extending }Dialog ........................................................................................... 273 Class JApplet .................................................................................................... 273 Testing JApplet Solutions ................................................................................ 273 Extending JApplet ........................................................................................... 274 Working with a Desktop ............................................................................... 275 Class JinternalFrame ...................................................................................... 276 Class JDesktopPane ........................................................................................ 283 Summary ................................................................................................................. 288 Chapter 9 Pop-Ups and Choosers ................................................ 289 Class JOptionPane ........................................................................................... 289 Creating a JOptionPane .................................................................................. 290 Displaying a JOptionPane .............................................................................. 295 Automatically Creating a JOptionPane in a Pop-up Window. ..................... 298 JOptionPane Properties .................................................................................. 304 Customizing JOptionPane Look and Feel ..................................................... 312 Class ProgressMonitor ................................................................................. 315 Creating a ProgressMonitor ........................................................................... 316 Using a ProgressMonitor ................................................................................ 316 ProgressMonitor Properties ........................................................................... 318 Customizing ProgressMonitor Look and Feel .............................................. 319 Complete ProgressMonitor Example ............................................................ 319 Class ProgressMonitorinputStream ........................................................ 322 Creating a ProgressMonitorlnputStream ...................................................... 322 Using a ProgressMonitorlnputStream. .......................................................... 323 ProgressMonitorlnputStream Properties ...................................................... 325 ix