Table Of Contentwww.it-ebooks.info
01_777106 ffirs.qxp 12/5/06 5:58 PM Page i
Professional
Java®
JDK® 6 Edition
W. Clay Richardson, Donald Avondolio, Scot Schrager,
Mark W. Mitchell, and Jeff Scanlon
www.it-ebooks.info
01_777106 ffirs.qxp 12/5/06 5:58 PM Page ii
Professional Java,®JDK®6 Edition
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2007 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN-13: 978-0-471-77710-6
ISBN-10: 0-471-77710-2
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
1O/RU/RS/QW/IN
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any
means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections
107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or
authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood
Drive, Danvers, MA01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be
addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317)
572-3447, fax (317) 572-4355, or online at http://www.wiley.com/go/permissions.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY:THE PUBLISHER AND THE AUTHOR MAKE NO REP-
RESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACYOR COMPLETENESS OF THE CON-
TENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT
LIMITATION WARRANTIES OF FITNESS FOR APARTICULAR PURPOSE. NO WARRANTYMAYBE CREATED
OR EXTENDED BYSALES OR PROMOTIONALMATERIALS. THE ADVICE AND STRATEGIES CONTAINED
HEREIN MAYNOT BE SUITABLE FOR EVERYSITUATION. THIS WORK IS SOLD WITH THE UNDERSTAND-
ING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PRO-
FESSIONALSERVICES. IF PROFESSIONALASSISTANCE IS REQUIRED, THE SERVICES OF ACOMPETENT
PROFESSIONALPERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALLBE
LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS
REFERRED TO IN THIS WORK AS ACITATION AND/OR APOTENTIALSOURCE OF FURTHER INFORMA-
TION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE
ORGANIZATION OR WEBSITE MAYPROVIDE OR RECOMMENDATIONS IT MAYMAKE. FURTHER, READ-
ERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAYHAVE CHANGED OR
DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.
For general information on our other products and services or to obtain technical support, please contact our Cus-
tomer Care Department within the U.S. at (800) 762-2974, outside the U.S. at (317) 572-3993 or fax (317) 572-4002.
Library of Congress Cataloging-in-Publication Data
Professional Java JDK, 6 Edition / W. Clay Richardson ... [et al.].
p. cm.
Includes index.
ISBN-13: 978-0-471-77710-6 (paper/website)
ISBN-10: 0-471-77710-2 (paper/website)
1. Java (Computer program language) I. Richardson, W. Clay, 1976-
QA76.73.J38P7623 2007
005.13’3—dc22
2006032740
Trademarks:Wiley and related trade dress are registered trademarks of Wiley Publishing, Inc., in the United States
and other countries, and may not be used without written permission. All other trademarks are the property of their
respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be
available in electronic books.
www.it-ebooks.info
01_777106 ffirs.qxp 12/5/06 5:58 PM Page iii
This book is dedicated to all those who make the daily sacrifices,
especiallythose who have made the ultimate sacrifice, to ensure our
freedom and security.
www.it-ebooks.info
01_777106 ffirs.qxp 12/5/06 5:58 PM Page iv
About the Authors
W. Clay Richardsonis a software consultant concentrating on agile Java solutions for highly specialized
business processes. He has fielded many Java solutions, serving in roles including senior architect,
development lead, and program manager. He is a co-author of More Java Pitfallsand Professional Portal
Development with Open Source Tools(Wiley), and Professional Java, JDK 5 Edition. As an adjunct professor
ofcomputer science for Virginia Tech, Richardson teaches graduate-level coursework in object-oriented
development with Java. He holds degrees from Virginia Tech and the Virginia Military Institute.
Donald Avondoliocurrently serves in a lead position as an architect/developer on an enterprise devel-
opment project. In his spare time, Donald loves fly-fishing, watching baseball and lacrosse, running
triathlons (not very well), and sitting around his house complaining about things.
Scot Schragerhas consulted extensively in the domains of pharmaceuticals, supply chain management,
and the national security market. He has led and participated in various project teams using Java and
Object Oriented Analysis & Design techniques. Most recently, Schrager has been focused on distributed
application architecture using J2EE technology.
Mark W. Mitchellhas extensive experience in enterprise application integration, particularly Web
Services integration between Java and the Microsoft platform. He has developed and deployed several
mission-critical web applications. Mitchell holds a degree in computer science from the University of
Virginia.
Jeff Scanlonis a software development consultant from Virginia. He holds both the Sun Certified Java
Developer and Microsoft Certified Solutions Developer certifications, and has been published in Software
Developmentmagazine.
www.it-ebooks.info
01_777106 ffirs.qxp 12/5/06 5:58 PM Page v
Credits
Acquisitions Editor Vice President and Executive Publisher
Robert Elliott Joseph B. Wikert
Development Editor Project Coordinator
Brian Herrmann Kristie Rees
Technical Editor Graphics and Production Specialists
David Parks Carrie A. Foster
Brooke Gracyzk
Production Editor Denny Hager
Kathryn Duggan Joyce Haughey
Barbara Moore
Copy Editor Barry Offringa
Kim Cofer Alicia B.South
Ronald Terry
Editorial Manager
Mary Beth Wakefield Quality Control Technicians
John Greenough
Production Manager Charles Spencer
Tim Tate Brian H. Walls
Vice President and Executive Group Publisher Proofreading and Indexing
Richard Swadley Techbooks and Stephen Ingle
Anniversary Logo Design
Richard Pacifico
www.it-ebooks.info
01_777106 ffirs.qxp 12/5/06 5:58 PM Page vi
www.it-ebooks.info
02_777106 ftoc.qxp 11/28/06 10:35 PM Page vii
Contents
Acknowledgments xv
Introduction xvii
Part I: Thinking Like a Java Developer 1
Chapter 1: Key Java Language Features and Libraries 3
Introducing Derby 3
Using Derby 4
Language Features Added in Java 5 7
Generics 7
Enhanced for Loop 16
Additions to the Java Class Library 17
Variable Arguments 18
Boxing and Unboxing Conversions 19
Static Imports 21
Enumerations 24
Metadata 26
Important Java Utility Libraries 34
Java Logging 35
Java Preferences 70
Summary 77
Chapter 2: Tools and Techniques for Developing Java Solutions 79
Principles of Quality Software Development 80
Habits of Effective Software Development 81
Communicate 81
Model 81
Be Agile 81
Be Disciplined 82
Trace Your Actions to Need 82
Don’t Be Afraid to Write Code 83
Think of Code as a Design,not a Product 83
www.it-ebooks.info
02_777106 ftoc.qxp 11/28/06 10:35 PM Page viii
Contents
Read a Lot 84
Build Your Process from the Ground Up 84
Manage Your Configuration 84
Unit Test Your Code 85
Continuously Integrate 85
Maintaining Short Iterations 86
Measure What You Accomplished — Indirectly 87
Track Your Issues 87
Development Methodology 88
Waterfall Methodology 88
Unified Process 90
Extreme Programming 91
Observations on Methodology 92
Practical Development Scenarios 93
Ant 93
Maven 2 101
TestNG 106
XDoclet 110
JMeter 117
Summary 120
Chapter 3: Exploiting Patterns in Java 123
Why Patterns Are Important 124
Keys to Understanding the Java Programming Language 124
Keys to Understanding Tools Used in Java Development 125
Keys to Developing Effective Java Solutions 126
Building Patterns with Design Principles 127
Designing a Single Class 127
Creating an Association between Classes 128
Creating an Interface 129
Creating an Inheritance Loop 129
Important Java Patterns 131
Adapter 131
Model-View-Controller 134
Command 142
Strategy 146
Composite 150
Summary 154
viii
www.it-ebooks.info
02_777106 ftoc.qxp 11/28/06 10:35 PM Page ix
Contents
Part II: A Broad Understanding of Java APIs,
Tools, and Techniques 155
Chapter 4: Developing Effective User Interfaces with JFC 157
Layout Managers 158
BorderLayout 158
BoxLayout 164
FlowLayout 173
GridLayout 177
GridBagLayout 189
SpringLayout 194
CardLayout 202
GroupLayout 208
Mustang Release Desktop Enhancements 214
Managing Navigation Flows in Swing Applications 225
Summary 235
Chapter 5: Persisting Your Application Using Files 237
Application Data 237
Saving Application Data 239
Sample Configuration Data Model for an Application 239
Java Serialization: Persisting Object Graphs 241
Key Classes 242
Serializing Your Objects 243
Extending and Customizing Serialization 257
When to Use Java Serialization 261
JavaBeans Long-Term Serialization: XMLEncoder/Decoder 262
Design Differences 262
Serializing Your JavaBeans 265
When to Use XMLEncoder/Decoder 269
Flexible XML Serialization: Java API for XML Binding (JAXB) 270
Sample XML Document for the Configuration Object 271
Defining Your XML Format with an XML Schema 273
JAXB API Key Classes 280
Marshalling and Unmarshalling XML Data 281
Creating New XML Content with JAXB-Generated Classes 283
Using JAXB-Generated Classes in Your Application 283
ix
www.it-ebooks.info