7
The M cGraw'H iII Companies Ca taloging-in-Publication Data is on file with the Library of Congress Preface Contents xix Information has been obtained by McGraw-Hill from sources believed to be reliable. How ever, because of the possibility of hum an or mechanical error by our sources, McGraw-Hill, or others, McGraw-Hill does not gua rantee the accuracy. ad equa cy, or comp leteness of any information and is not responsible for any errors or omissions or the results obtained from the use of suc h informa tion. McGraw-Hill books are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training programs . For more information, please write to the Director of Special Sales, Professional Publishing, McGraw-Hill, Two Penn Plaza, New York, NY 10121-2298. Or contact your local bookstore. Herb Schildt's Java Programming Cookbook Copyright © 2008 by The McGraw-Hill Companies. AUrights reserved. Printed in the United States of America. Except as permitted under the Cop yright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, w ithout the prior written permission of publisher, with the exception that the program listings may be entered, stored, and executed in a computer system, but they may not be reproduced for publication. 1234567890 FGR FGR 01987 ISBN: 978-0-07-226315-2 MHID: 0-07-226315-6 Sponsoring Editor Wendy Rinaldi Editorial Supervisor Patty Man SamRC, International Typesetting and Composition Acquisitions Coordinator Mandy Canales Technical Edito r Hale Pringle Copy Editor Robert Campbell Proofreading MeghaRC Indexer Sheryl Schildt Production Supervisor Jim Kussow Composition international Typesetting and Composition Illustration International Typesetting and Composition Art Director, Cover JeffWeeks 1 O verview . What 's Inside . How the Recipes Are Organized .. A Few Words of Caution . Java Experience Required . What Version of Java? . 2 Work ing with Strings and Regular Expressions .. .. .. .. .. .•• ...... .. An Overview ofJava's String Classes . Java's Regular Expression API . An Introduction to Regular Expressions . Normal Characters ....... ...... .... .. ...... ...... ........ .. . Character Classes . The Wildcard Character ...................................... Quantifiers . Greedy , Reluctant, and ..::::::::::: :: ::::: Boundary Matchers .. ......... .. . .. .. .. ........... .. ....... The OR Operator . Groups . ............................. Flag Sequences . Remember to Escape the \ in Java Strings . Sort an Array of Strings in Reverse Order . Step-by-Step . Discuss ion . .. ..... ......... ..... .. Example . Options and Alternatives . I gnore Case Differences when Sorting an Array of Strings . Step-by-Step . Discussion ................................................. Example . Options a nd Alterna tives . Ignore Case Differences when Searching for or Replacing Substrings . Step-by-Step . Discussion . ... ............ ..... .......... ........ ... Example . Options and Alternatives . Split a String in to Pieces by Using split() . Step-by-Step . . . 1 1 2 3 3 4 5 6 8 8 9 9 10 10 11 11 11 12 13 13 14 14 14 16 17 18 18 19 19 21 22 22 22 23 24 25 25 y

The McGraw'HiII Contents

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: The McGraw'HiII Contents

The McGraw'H iII Companies

Ca taloging-in-Publication Data is on file with the Library of Congress

Preface

Contents

xix

Information has been obtained by McGraw-Hill from sources believed to be reliable. How ever, because of the possibility ofhum an or mechan ical error by our sources, McGraw-Hill, or others, McGraw-Hill does not gua rantee the accuracy. ad equa cy,or comp leteness of any information and is not responsible for any errors or omissions or the resul ts obtained from the use ofsuch informa tion.

McGraw-Hill books are available at special quantity discounts to use as premiums and salespromotions, or for use in corporate training programs. For more information, please writeto the Director of Special Sales, Professional Publishing, McGraw-Hill, Two Penn Plaza,New York, NY 10121-2298. Or contact your local bookstore.

Herb Schildt's Java Programming Cookbook

Copyright © 2008 by The McGraw-Hill Companies. AUrights reserved. Printed in theUnited States of America. Except as permitted under the Copyright Act of 1976, no part ofthis publication may be reproduced or distributed in any form or by any means, or stored ina database or retrieval system, w ithout the prior written permission of publisher, with theexception that the program listings may be entered, stored, and executed in a computersystem, but they may not be reproduced for publication.

1234567890 FGR FGR 01987

ISBN: 978-0-07-226315-2MHID: 0-07-226315-6

Sponsoring EditorWendy Rinaldi

Editorial SupervisorPatty Man

Project~anagement

SamRC,International Typesettingand Composition

Acquisitions CoordinatorMandy Canales

Technical Edito rHale Pringle

Copy EditorRobert Campbell

ProofreadingMeghaRC

IndexerSheryl Schildt

Production SupervisorJim Kussow

Composi tioninternational Typesettingand Composition

IllustrationInternational Typesettingand Composition

Art Director, CoverJeffWeeks

1 Overview .What's Insid e .How the Recipes Are Organized ..A Few Words of Caution .Java Experience Required .What Version of Java? .

2 Work ing with Strings and Regular Expressions .. . . .. • .. .. .••...... ..An Overview ofJava's String Classes .Java's Regular Expression API .An Introduction to Regular Expressions .

Normal Characters . . .. . .. ...... .... .. ...... . .... ......... .. .Character Classes .The Wildcard Character . . . ... . . . .. . .. . . ... .. .. . .. . . . . . ... . . ..Quantifiers .Greedy, Reluctant, and Posses'si~~'6~~~~;fi~~s' . . : : : : : : : : : : : :: : : : : :Boundary Matchers .. . . . . . . . . . .. . .. . . .. . . . . . . . . . . . • .. . . . . . . .The OR Operator ~ .Groups . . . . . .. . . . . ... . . . . . ... .. . .. .. .Flag Sequences .Remember to Escape the \ in Java Strings .

Sort an Array of Strings in Reverse Order .Step-by-Step .Discussion . . . ..... . . . ... . . . ..... ..Example .Options and Alternatives .

Ignore Case Differences when Sorting an Array of Strings .Step-by-Step .Discussion .. . .. . . . . .. . . .. . . . .. . . . . . . .. . . .. . .. . .. .. . .. . . ... .Example .Options and Alternatives .

Ignore Case Differences when Searching for or Replacing Substrings .Step-by-Step .Discussion . ... .. .. . .. .. ... .. ... .. .. . .. ... . . .. . ... . . .Example .Options and Alternatives .

Split a String into Pieces by Using split() .Step-by-Step . . .

112334

568899

10101111111213131414141617181819192122222223242525

y

Page 2: The McGraw'HiII Contents

vi Herb Sch i ldt's Java Programming Coo kboo k

-Contents vii

3

Discussion .Example .Options and Alternatives .

Retri eve Key / Value Pairs from a String .Step-by-Step .Discussion .Example .Options and Alterna tives .

Match and Extract Substrings Using the Regular Expression API .Step-by-Step .Discussion .Example .Op tions and Alternatives .

Tokenize a String Using the Regular Expre ssion API .Step-by-Step .Discussion .Example .Bonus Example .Options and Alternatives .

File Handling .An Overview of File Handling .

Streams .The RandomAccessFile Class .The File Class .The I/O Interfaces .The Compressed File Streams .

TIps for Handling Errors .Read Bytes from a File ..

Step-by-Step .Discussion .Example .Options and Alternatives .

Write Bytes to a File .Step-by-Step .Discussion .Example .Options and Alternatives r: .

Buffer Byte-Based File I/O .Step-by-Step .Discussion .Example .Options and Alterna tives .

Read Characters from a File .Step-by-Step .

26262828292929323233333334353637384047

49505053545557575959596061626363636465666666686969

Discussion .Example .Options and Alterna tives

Write Characters to a File . . . . .. .. . . . . .. . . . . .. . . . . . . . .. . . . . . .. . . .. ..Step-by-Step .Discussion ... .. .. .. .. . .. . . .. . .. . . .... ... . .. ... . . ... .. .. .. . .Example .Options and Alternatives .

Buffer Character-Based File I/O .Step-by-Step .Discussion .. . .... . . . .. . .. . .. .. ...... . . . . . ...... . . .. .. . . ... .Example .Op tions and Alternatives .

Read and Write Random-Access Files . . .. ..... . . .. . . . . . . ..... .. ..... .Step-by-Step .Discussion .. . . . . .. . .. . .. . . . .... . . . . . .. . .. . ..... . .... . ......Example .Options and Alternatives , .

Obtain File Att ributes .Step -by-Step .Discussion .Example .Op tions and Alternatives

Set File Attributes . . .. . . . . . .. . . . . ... . . . . .. . .. . . . .. . . . . . .Step-by-Step 7 .• . • • •

Dis cussion . . .Example .Options and Alternatives .

List a Directory .Step-by-Step .Discussion .. .. .. . . . . .. . ... . .. . . . . . .. . . . .. . . .. .. .. . . . . . . .. . .Example .Bonus Example .Options and Alternatives .

Compress and Decompress Data .Step-by-Step .Discussion .Example .Options and Alternatives .

Create a ZIP File .. . . . . .. ... . .. . ... . ... . ... . .. .. ... . . .. . . . .. ...Step-by-Step .Discussion . . .Example .Options and Alternatives

697071727273737475767677798080808183838484848686878787899090909193949595969699

100100101102105

Page 3: The McGraw'HiII Contents

viii Herb Schildt's Java Programming Cookbook Contents ix

,I

Decompress a ZIP File .Step-by-Step .Discussion .Example .Options and Alternatives .

Serialize Objects .Step-by-Step .Discussion .Example .Options and Alternatives .

4 Formatting Data . . . . ..... • . . . . . . . .... . . . . .. . . . .. • • . . . . . . . . •. . . . . . .An Overview of Formatter .

Formatting Basics .Specifying a Minimum Field Width .Specifying Precision .Using the Format Flags .The Uppercase Option .Using an Argument Index .

Overview of NumberFormat and DateFormat .Four Simple Numeric Formatting Techniques Using Formatter .

Step-by-Step .Dis cussion .Example .Options and Alternatives .

Vertically Align N ume ric Data Using Formatter .Step-by-Step .Dis cussion .Example .Bonus Example: Center Data .Options and Alternatives .

Left-Justify Output Using Formatter .Step-by-Step .Discussion .Examp le .Options and Alternatives .

Format Time and Date Using Formatter .Step-by-Step .Discussion .Example .Options and Alternatives .

Specify a Locale with Formatter ..Step-by-Step .Discussion .Example .Option s and Alternatives .

105105106107109110111111112115

117118119121121122122123123124124124125126126126127127128131131131131132133133134134136137138138138139140

Use Streams with Formatter .Step-by-Step .Discuss ion .Example .Options an d Alternatives .

Use printf() to Display Formatted Data .Step-by-Step .Dis cussion .Example .Bonus Example .Options and Alternatives .

Format Time and Date with DateFormat .Step-by-Step .Dis cussion .Example .Options and Alternatives .

Format Time and Date with Patterns Using SimpleDateFormat .Step-by-Step .Discuss ion .Example .Options and Alternatives .

Format Numeric Values with NumberFormat .Step-by-Step .Discussion .Example .Options and Altern atives .

Format Currency Values Using NumberFormat .Step-by-Step .Discussion .Example .Options and Alternatives .

Format Numeric Values wi th Patterns Using DecimalFormat .Step-by-Step .Discussion .Example .Options and Alternatives .

5 Working with Collections .Collections Overview .

Three Recen t Changes .The Collection Interfaces .The Collection Classes .The ArrayList Class .The LinkedList Class .The HashSet Class .The LinkedHashSet Class .

140140140141142143143143144145146147147148148149150151151152153153154154155156156157157157157158158158159160

161162163164173173174175175

Page 4: The McGraw'HiII Contents

-X He rb Schildt's Java Prog ramming Cookboo k

Contents xi

The TreeSet Class .The PriorityQueue Class .The ArrayDeque Class .The EnumSet Class .

An Overview of Maps .The Map Interfaces .The Map Classes .

Algorithms .Basic Collection Techniques .

Step-by-Step .Dis cussion .Example .Options and Alternatives .

Work with Lists .Step-by-Step .Discuss ion .Example .Op tions and Alternatives .

Work w ith Sets .Step-by-Step .Discussion .Example .Bonus Example .Options and Alternatives .

Use Comparable to Store Objects in a Sor ted Collection .Step -by-Step .Discussion .Example .Options and Altern atives .

Use a Comparator with a Collection .Step-by-Step .Discussion .Example .Options and Alternatives .

Iterate a Collection .Step-by-Step .Dis cussion ;..' .' .Example .Options and Alternatives .

Create a Queue or a Stack Using Deque .Step-by-Step .Discussion .Example .Option s and Alternatives .

176176177178178178183185186187187188190191191192192195195196196197198201201202202203204205205205206209209210210211213214214215216217

Revers e, Rotate, and Shuffle a List .Step-by-Step .Discussion . .. . . ... . . . . .. . . . ... ... . . ... .. ..... ... ... . .... ...Example .Options and Alternatives .

Sort and Search a List . . . . ... . .. ... . . ..... . . .. .. . . . .. .. . . . ... . . . ... .Step-by-StepDiscussion .. . .. . ... .. . .. . .... .. . . . ...... .. . ... .. .. . . . ......Examp le .Op tions and Alternatives .

Create a Checked Collection .Step-by-Step .Discussion . . .... .. . .. .. ... .. .. . . ... .. .... . .. .. . . . ...... . . . .Example .Options and Alternatives .

Create a Synchron ized Collection .Step-by-Step .Discuss ion . .... . . . ... . ..... . . . ... . . . .. . .. . .. ... . . . .. .. . . . ..Example .Options and Alterna tives .

Create an Immutable Collection .Step-by-Step .Discussion ... . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . .Example .Options and Alternatives , .

Basic Map Techniqu es .Step-by-Step .Discussion .Example .Options and Alternatives .

Conve rt a Properties List into a HashMap .Step-by-Step .Discussion .... . . . . .. . . .. ... . . .. . ......... . ... .. . . ..... .... .Example .Options and Alternatives .

6 AppIets and Servlels .Applet Overview .

The Applet Class .Applet Architecture .The Applet Life Cycle .The AppletContext, AudioClip , and AppletStub Interfaces .

Servlet Overview .The javax.servlet Package .The javax.servlet.http Package .

218219219219220221221221222223224224224225227227228228228231231231232232233233234235235238238239239239240

241241242244245246246246249

Page 5: The McGraw'HiII Contents

xii Herb Schildt 's Java Programming Cookboo k

..Content s xiii

.. . .. .. . . .. . . . . . . .. . . ..... .. . ... . . .. . . ... . . . ....... .. . . . . . .. . .. . . . .. . . . . ... . . . . .. .. . .. . .. . . .. . .. ... .

The HttpServlet Class .The Cookie Class .The Servlet Life Cycle .Using Tomcat for Serv let Development .

Crea te an AWT-Based App let Skeleton ' .Step-by-Step .Discussion .Example .Options and Alternatives .

Create a Swing-Based Applet Skeleton .Step-b y-Step .Discussion .Example .Options and Alternatives .

Create a GUI and Handle Events in a Swing Applet .Step-by-Step .Discussion .Histo rical Note: getContentPane () .Example .Bonus Example .Options and Alternatives .

Paint Directly to the Surface of an Applet .Step-by-Step .Discussion .Examp le .Options and Alternatives .

Pass Parameters to Applets .Step-by-Step .Discussion .Example .Options and Alternatives .

Use AppletC ontext to Disp lay a Web Page .Step-by-Step .Discussion .Example .Options and Alternatives .

Crea te a Simple Servlet Using GenericServ let .Step-by-Step .Discussion .Example .Options and Alternatives .

Handle HTTP Requests in a Servlet .Step-by-Step .DiscussionExamp le

251251253254255256256256257257258258259260260261261263263266268269269270271273275275275276277278278278278281282282282283284285285285286

Bonus Example .Options and Alternatives .

Use a Cookie with a Servlet . .. .... .. .. . . . . .. .... . . ... .. .. ... . . .. . ..Step-b y-Step .Dis cussion . . . . . . . . . . . . . . . . . .. . . . . . .. . . . . . .. . . .. . . . . . . . . .. . .Example .Options and Alternatives .

7 Multith rea di ng .Multithreading Fundamentals .

The Runnable Interface . .. . . .... . .. . . . . .. . .. .. . . .. .. .. . . . . . .The Thread Class .

Crea te a Thread by Implementing Runnable .Step-by-Step .Discussion . . . ... . .. . . .. . . . .. . . . . ... . .. .. . . . .. . .. . .. . . .. . .. .Example .Options and Alternatives .

Create a Thread by Extending Thread .Step-by-Step .Discussion ... . . .... . ... . .. . .. . ...... . . . .... . .... .. . .. ... ..Example .Options and Alternatives .

Use a Thread's Name and ID .Step-by-Step .Dis cussion .Example .Options and Alternat ives .

Wait for a Thread to End .Step-by-Step .Discussion . . . ... .. .... .. . . .... . . ... ... ... . ..... . ... . ..... . .Example .Options and Alternatives .

Synchronize Threads .Step-b y-Step .Discussion . ... .. .. . ..... . .. . . .... . . ... ..... . . .... . . . ... . .Example .Options and Alternatives .

Communicate Between Threads .Step-by -Step .Discussion . .. . . . .. .. . . . . .. . . . .. . . . . .. . . . . .. . .. . . . . .. .. . . . ..Example .Options and Alternatives .

Suspend, Resume, and Stop a Thread .Step-by-Step .Discussion

287290290290290291293

295296297298299300300300303304305305305306307307308308310311311311312313314315315316318318319319320322323323324

Page 6: The McGraw'HiII Contents

II,

xiv Herb Sc bildt's Java Programmi ng Coo kbook

Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 325Options and Alternatives .. . . . . . . . . . . . . . . . . .. . . . . .. . . . . . . . . . . 327

Use a Daemon Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328Step-by-Step . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . 329Discussion , . 329Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329Bonus Example: A Simple Reminder Class . . . . . . . . . . . . . . . . . . . .. 331Options and Alternatives . . . . . . . . . . .. . . . . . .. . . . . . . . . . . . . . . . . . 336

Interrupt a Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336Step-by-Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 337Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 337Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 339

Set and Obtain a Thread's Priority 341Step-by-Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341Discussion ... . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . .. . . . . . . . . . ... 342Example .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 342Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 344

Monitor a Thread's State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344Step-by-Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 345Example . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 346Bonus Example: A Real-Time Thread Monitor . . . . . . . . . . . . . . .. .. 349Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 353

Use a Thread Group . . . . . . . . . . . . . . . . . . . . . . .. 353Step-by-Step . .. . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . .. . . . . . . 354Discussion , 354Example . . . . . .. .. . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . .. . . . .. 355Options and Alternatives . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . 357

8 Swing 359Overview of Swing 360Components and Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 361

Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 362The Top-Level Container Panes . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . 363

Layout Manager Overview 363Event Handling : ~ . . . . 364

Events 365Event Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 365Event Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 365

Crea te a Simple Swing Application 366Step-by-Step . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . .. . . .. .. . . . 366Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 367Historical Note: getContentPane( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369

p

Con tents

Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 369Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371

Set the Content Pane's Layout Manager 372Step-by-Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 372Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375

Work with JLabel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 376Step-by-Step . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . .. 376Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377Example .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 379Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . 382

Create a Simp le Push Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383Step-by-Step . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 385Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 387

Use Icons, HTML, and Mnemonics with JButton . . . . . . . . . . . . . . . . . . . . . . 390~~~~ mDiscussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391Example ... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 393Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 395

Create a Toggle Button 396Step-by-Step . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 397Discussion , . . . . . .. 397Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398Options and Alternatives . . .. . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . .. 400

Create Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400Step-by-Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. .. 401Discussion .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405

Create Radio Buttons .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405Step-by-Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . 406Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 407Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410

Input Text with JTextField 411Step-by-Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 411Discussion ... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 412Example .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413Bonus Example: Cut, Copy, and Paste . . . . . . . . . . . . . . . . . . . . . . . .. 416Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419

Work with [List 420Step-by-Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420

xv

Page 7: The McGraw'HiII Contents

Conte nt s

Return a Value from a Thread .Step-by-Step .Discussion .Example .Options and Alternatives .

Use Reflection to Obtain Information about a Class at Run time .Step-by-Step .Discussion .Example .Bonus Example: A Reflection Utility .Options and Alternatives .

Use Reflection to Dynamically Create an Object and Call Methods .Step-by-Step .Discussion .Example .Options and Alternatives .

Create a Custom Exception Class .Step-by-Step .Discussion .Example .Options and Alternatives .

Schedule a Task for Future Execut ion .Step-by-Step .Discussion .Example , .Options and Alternatives .

xvi Herb Schildt 's Java Prog ramming Coo kboo k

Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . 420Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . .. . . . . . . . . . . . 422Options and Alternatives . . . . . . . . . .. . . . . . . .. . . . . . . . .. . . . . .. . . 424

Use a Scroll Bar .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426Step-by-Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ... . . . . 427Discussion . . . . . . . . . . . . . . . . . . . . . . .. . . . . . .. . . . . .. . . . . . . . . . 427Example . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . 429Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 431

Use JScrollPane to Handle Scrolling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433Step-by-Step . . . . . . .. . . . . . . . . . . . . . . . . . . . .. . . . . . . . . .. . . . . . .. . 433Discussion . . . . . . . . .. . . . . . . . .. . . . . . . . . . . .. . . . . .. . . . . . . . . . . . . 433Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 433Options and Alternatives . . . . .. . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . 436

Display Data in a JTable 438Step-by-Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 439Discussion . . . . . . .. . . . . . . .. . . . . . . . . . . . . . . . . . .. . . . . . . . . .. . 440Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441Options and Alternatives . . . . . . . . . . . .. . . . .. . . . . . . . . . . . . . . . . .. 444

Handle JTable Events . . . . . . . . 446Step-by-Step . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 447Discussion . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . .. . . .. . . . . . . . 447Example . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . . . .. . . . . . . . . . .. 450Options and Alte rnatives . . . . .. . . . . .. . . . . . . . . . . . . . . . .. . . . . . .. 455

Display Data in a ]Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456Step-by-Step . . . . . . . .. . . . . . . .. . . . . . . . . . . . .. . . . . .. . . . . . . . . 458Discussion . . .. .. .. . . . . .. .. .. . .. .. .. .. .. .. . .. .. . . .. .. .. .. 458Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 461Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464

Create a Main Men u . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 466Step-by-Step . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . .. . . .. . . . . . . 467Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 467Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 471

9 Potpourri . . •• . . . . . . . . . . . . . . . . . . ... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473Access a Resource via an HTTPConnection . . . . . . . . . . . . . . . . . . . . . . . . .. 474

Step-by-Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 474Discussion .. . . . . . . . . . .. . . . . . .. . . . . . . . . . . . . .. . . . . . . . . . .. . . . . . 475Example .. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 479

Use a Semaphore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480Step-by-Step . . . . . . . . . . .. . . . . . .. . . . . . . . . . . . .. . . . .. . . . . . . . . . . 481Discussion .. . .. . .. . .. . .. .. .. .. . .. .. .. .. .. . . .. . .. .. .. .. .. ... 482Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482Options and Alternatives . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485

=

Index .. .. ................... ... ......................... ........

486487487488491491492492493494496496497497498501501502502504505506507507508510

Sl1

xvii