• No results found

Contents. Example. Ca taloging-in-publication Data is on file with the Library of Congress

N/A
N/A
Protected

Academic year: 2021

Share "Contents. Example. Ca taloging-in-publication Data is on file with the Library of Congress"

Copied!
7
0
0

Loading.... (view fulltext now)

Full text

(1)

TheMcGraw'H iIICompanies

Cataloging-in-PublicationData is on file with the Library of Congress

Preface

Contents

xix

Informationhas been obtaine d byMcGraw-Hillfrom sourcesbelieved to bereliable.However,because ofthe possibilityof humanor mechan ical error byour sources, McGraw-Hill,orothers,McGraw-Hilldoesnotguarantee theaccuracy. adequa cy, orcomp leteness ofany informationandisno tresponsiblefor anyerrors oromissions or theresultsobtainedfrom the use of such informa tion.

McGraw-Hillbooks are availableat specialquantity discounts touseas premiumsandsales promotions,or for useincorp orate training programs.For moreinformation,please write to the Directorof Special Sales,Professional Publishing,McGraw-Hill,Two Penn Plaza, New York, NY 10121-2298.Or contact your local bookstore.

Herb Schildt's Java ProgrammingCookbook

Copyright©2008 by The McGra w-HillCompanies.AU rights reser ved. Printed in the United Statesof America.Except as permitted under the CopyrightAct of 1976,no part of this publicationmay be reproduced or distributed in any form or by anymeans,or stored in a databaseor retrieval system, withoutthe prior written permission of publisher,with the exception that the program listings may be entered,stored, and executedin a computer system, but theymay not be reproduced for publication.

1234567890 FGR FGR 01987 ISBN: 978-0-07-226315-2 MHID: 0-07-226315-6

SponsoringEditor WendyRinaldi Editorial Supervisor

Patty Man

Project~anagement

SamRC,

InternationalTypesetting and Composition AcquisitionsCoordinator

Mandy Canales

TechnicalEdito r HalePringle CopyEditor

Robert Campbell Proofreading

MeghaRC In dexer

Sheryl Schildt

ProductionSupervisor Jim Kussow Composi tion

international Typesetting and Composition Illustration

International Typesetting and Composition Art Director, Cover

JeffWeeks

1 Overview .

What's In sid e .

Howthe Recipes Are Organized ..

A Few Words of Caution .

Java Experience Required .

What Version of Java? .

2 Work ing with Strings and RegularExpressions ... . ..• .... .••...

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 .

Rememberto Escape the\ in Java Strings .

Sort an Array of Strings in Reverse Order .

Step-by-Step .

Discussion .

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

Example .

Options and Alternatives .

Igno re Case Differences when Sorting an Arrayof Strings .

Step-by-Step .

Discussion

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

Example .

Options andAlterna tives .

Ignore Case Differences whenSearching for or Replacing Substrings .

Step-by-Step .

Discussion .

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

Example .

Options and Alternatives .

Split a String into 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

(2)

v i

Herb Schildt's Java Programming Cookboo k

-

Contents

v ii

3

Discussion .

Example

.

Options an d Alternatives .

Retri eve Key

/

V alue Pairs from a String

.

Step-by-Step .

Discussion .

Example .

Options and Alterna tives .

Match and Extra ct S u bstrings Us ing the Regular Ex pression API

.

Step-by-Step .

Discussion .

Example .

Op tions an d Alternatives .

Token ize a String Using the Regular Expre ssion API .

Step-by-Step .

Discussion .

Example .

Bonus Examp le

.

Options and Alternatives .

File Handling .

An Overview of File Handling .

Stream s

.

Th e RandomAccessFile Class .

The File Class .

The I/O Interfa ces

.

The Com pressed File Streams .

TIps for Handling Errors

.

Read Bytes from a File ..

Step-by-Step

.

Discussion .

Example .

Options and Altern atives .

Write Bytes to a File .

Step-by-Step

.

Discu ssion .

Example

.

Options and Alternatives r : .

Buffer Byte-Based File I/O .

Step-by-Step .

Discu ssion .

Example

.

Options and Alterna tives .

Read Ch aracters from a File .

Step -by-Step .

26 26 28 28 29 29 29 32 32 33 33 33

34

35 36

37

38

40

47 49 50 50 53

54

55 57 57 59 59 59 60 61 62 63 63 63 64 65 66 66 66 68 69 69

Discussion .

Example .

Options and Alterna tives

Write Characters to a File ... ... .. . .. ... . .. ... . .. .... .. .. .. . ... ... ..

Step-by-Step

.

Dis cussion

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

Example .

Options and Alternatives .

Buffer Character-Based File I/O .

Step-b y-Step .

Discussion

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

Example .

Op tions and Alternatives .

Read and Write Random-Access Files . ... ... .. ... ... .. ... .

Step-by-Step .

Discussion

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

Examp le .

Options and A lternatives

, .

Obtain File Att ributes .

Step -by-Step .

Discu ssion .

Example .

Op tions and Alternativ es

Set File Attributes .

.

.. ... .. .. .

..

.. .

.. .

. ... ... . ...

.

.

...

Step-by-Step

7.•. • ••

Discussion ..

.

Example .

Options and Altern atives .

List a Directory .

Step-b y-Step

.

Discu ssion

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

Example .

Bonus Example .

Options and Alternatives .

Compress and Decompress Data

.

Step-by-Step

.

Discu ssion .

Example .

Options and Alternatives .

Create a ZIP File .. ... . .. ... ... . ... .... .... ... .. ... . .... .... ...

Step -by-Step .

Discussion .

. .

Example .

Options and Alternatives

69 70 71

72 72 73 73

74 75 76 76 77

79

80 80 80 81 83 83 84 84 84 86 86 87 87 87 89 90 90 90 91 93 94 95 95 96 96 99

100

100 101 102

105

(3)

viii Herb Schildt's Java Programming Cookbook i x

,I

Decompress a ZIP File .

Step-by-Step .

Discussion .

Example .

Options and Alternative s .

Serialize Objects

.

Step-by-Step .

Discussion .

Exam ple .

Options an d Alternative s .

4 Formatti ng D ata . . . . ... • . . . . . . . .... . . . . .. . . . .. • • . . . . . . . . •. . . . . . .

An

Ove rview of Formatter .

Formatting Basics

.

Spe cifying a Minimum Field Width .

Sp ecifying Precision

.

Using the Format Flags .

The Up p ercase Option .

Using an Argument Index

.

Ov erview of N u mberForm a t an d DateFormat

.

Four Simple N umeric Formatting Techniques Using Formatter .

Step-by-S tep .

Discussion .

Examp le .

Options and Altern atives .

Vertically Align N ume ric Data Usin g Formatter .

Step -by-Step

.

Discussion .

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 Us ing Formatter .

Step-by-Step .

Discu ssion .

Example .

Options and Alternatives .

Specify a Locale with Formatter ..

Step-by-Step .

Discussion .

Examp le .

Option s and Alt ernatives

.

105 105 106 107 109 110 111 111 112 115

117

11 8 119 121 121 122 122 123 12 3 124 124 124 125 126 126 126 127 127 128 131 131 131 131 132 133 133 134 134 136 137 138 138 138 139 140

Use Streams with Formatter

.

Step-by-Step .

Discu ss ion .

Example .

Options an d Alternatives .

Use printf() to Display For matted Data .

Step-by-Step .

Discussion .

Exam p le

.

Bonus Examp le .

Option s and Altern atives .

Format Time and Date with DateFormat .

Step-by-Step .

Discussion .

Examp le .

Options and Alternatives .

Format Tim e and Date w ith Patterns Using Simp leDateForm at .

Step-by-Step .

Discuss ion .

Example .

Options and Alternat ive s .

Format Nu m eric Values with N umberFor m at

.

Step -by-Step .

Discussion .

Example .

Options and Altern atives

.

Form at Currency Value s Using NumberFormat .

Step-by-Step .

Discussion .

Examp le .

Options an d Alternatives .

Format Nu m eric Valu es wi th Patterns Using DecimalFormat .

Step-by-Step .

Discussion .

Examp le .

Options and Alternatives .

5 Work ing with Co ll ections .

Collection s Overview .

Three Recen

t

Ch anges

.

The Collection Interfaces .

The Collection Classes

.

The ArrayList Class

.

The LinkedList Clas s

.

The H ashSet Class .

The LinkedHashSet Cl ass .

140

140

140

141

142

143

143

143

144

145

146

147

147

148

148

149

150

151

151

152

15 3

15 3

154

154

155

156

156

157

157

157

157

158

158

158

159

160

161

162

163

164

173

173

174

175

175

(4)

-

X Herb Schildt's Java Programmi ng Cookbook

Contents

xi

Th e TreeS et Class .

Th e PriorityQueu e Class .

The ArrayDeque Class .

The EnumSet Class

.

An Overview of Maps .

The Map Interface s .

The Map Classes

.

Algor ithms

.

Basic Collection Techniques .

Step -by-Step

.

Discussion .

Example .

Options and Alternatives .

Work w ith 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 Comp arable to Store Objects in a Sor ted Collection .

Step -by-Step

.

Discussion .

Example

.

Options and Altern atives .

U se a Compar ato r with a Collection .

Step -by -Step .

Discussion .

Exam ple .

Op tions and Alternatives .

Iterate a Collection

.

Step-by-Step .

Discussion ;..'.' .

Example .

Options and Alternatives .

Create a Queue or a Stack Using Deque .

Step-b y-Step .

Discussion .

Exam ple

.

Op tion s and Alternatives .

176 176 177 178 17 8 178 183 185 186 187 187 188 190 191 191 192 192 195 195 196 196 197 198 201

201

202 202 203 204 20 5 205 20 5 206 209 209

210

210 211

213

214

214

215 21 6 21 7

Revers e, Ro tate, and Shuffle a List .

Step-by-Step .

Discussion

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

Example

.

Options and Alt ernativ es .

Sort and Search a List . ... . ... ... .... .. . . ... .. ... .. .... .

Step-by-St ep

Discu ssion

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

Examp le .

Op tions and Alternativ es .

Create a Checked Collection .

Step-by-Step .

Discussion

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

Example .

Options and Alt ernatives .

Crea te a Synchron ized Collection

.

Step-b y-Step .

Discuss ion

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

Example .

Options an d Alterna tives

.

Create an Immutable Collection .

Step-by-Step .

Discu ssion ... .. . ... ... .. .. . . .... ... . ... . . ... .. .... ..

Example .

Options and Alternatives

,

.

Basic Map T echniqu es .

Step-by-Step .

Discussion .

Exam ple .

Options and Alternatives .

Conve rt a Properties List into a Ha shM ap .

Step-by-Step

.

Discussion

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

Examp le

.

Options and A ltern atives .

6 AppI ets and Serv lels .

Applet Overview

.

The Applet Class

.

Applet Architecture

.

The Applet Life Cycle .

The Ap pletCont ext, AudioClip , and AppletStub Interfaces

.

Servlet Over view .

The javax .servlet Package .

The javax.servlet.http Package .

21 8

219

219

21 9

220

221

221

221

222

22 3

224

224

224

225

227

227

22 8

228

22 8

231

231

231

232

232

23 3

23 3

234

235

235

238

238

239

239

239

240

241

241

242

244

24 5

246

246

246

249

(5)

xii Herb Schildt 's Java Programming Cookboo k

..

Content s x iii

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

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

The HttpServlet Class .

Th e Cookie Cla ss

.

Th e Serv let Life Cycle .

Us in g Tomcat for Serv le t Development .

Crea te an AW T-Based App let Skeleton

'

.

Step-by-Step .

Discu ssion .

Examp le

.

Options and Alternat ives .

Create a Swing-Based Applet Skeleton .

Step-b y-Step .

Discussion .

Exam p le .

Options and Alternatives

.

Create a GUI an d Handle Events in a Swing Applet .

Step-by-Step .

Discussion .

Histo rical N ote: getContentPane () .

Examp le

.

Bonus Exam p le .

Options an d A lternativ es .

Paint Directly to the Surface of an Applet .

Step -by-S tep

.

Discussion .

Examp le .

Option s an d Alternatives .

Pass Paramet ers to Applets .

Step -by-Step

.

Discus sion .

Examp le .

Options and Alte rn atives .

Use AppletC ontext to Display a Web Page .

Step-by-Step .

Discussion .

Examp le .

Options and Alte rn a tives .

Crea te a Sim p le Servlet Using Gen ericServ let .

Step-by-Step .

Discussion .

Examp le .

Option s and Alternatives .

H andle HTTP Requ ests

in

a Servlet .

Step-by-Step .

Discussion Example

251 251 253 254 255 256 2 56 25 6 257 257 25 8 258 259 260 260 261 261 263 263 266 268 269 269 270 271 27 3 275 275 27 5 276 277 278 278 278 278 281 28 2 282 282 283 284 285 285 285 286

Bon us Exam p le .

Options an d Alternatives

.

Use a Cookie w ith a Servlet ... . ... .. .. .... .. .... ... .. .. ... ...

Step-b

y-Step

.

Discussion

... .. .

.

. .

. .. .. .

... .

.

.. . . ... .

.. .

..

. .

. .

. . ..

.

.

. .. ..

.

Examp le

.

Option s an d Alternatives

.

7 Multith rea di ng .

M ultithread ing Fund ame n tals .

The Runnable Interface . ... . .. .. .... ... .... .. .. .. ... .. .

The Thread Cl ass .

Crea te a Thread by Implementing Runnable .

Step -by-Step .

Discussion

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

Example .

Options and Alternatives .

Create a Thread b y Extending Thread .

Step-by-Step .

Discu ssion

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

Example .

Options an d Alte rn atives .

Use a Thread's Nam e and ID

.

Step-by-Step .

Discussion .

Example .

Options and Alternatives .

Wait for a Thread to En d .

Step-by-Step .

Discu ssion

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

Examp le .

Options and Alterna tives .

Synchronize Threads

.

Step-b y-Step .

Discu ssion

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

Example .

Option s and Alternatives

.

Comm unicate B e tw een Threads .

Step-by -Step

.

Discu ssion

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

Exam p le .

Options and Alternative s .

Susp end, Resume, and Stop a Thread .

Step -by- Step .

Discussion

28 7 290 290 290 290 291 29 3 295 296 297 298 299 300 300 300 303 304 305 305 305 306 307 307 30 8 308 310

311 311 311

312

313

314

315

31 5

316

318

318

31 9

319

320

322

323

323

324

(6)

II

,

x iv

Herb Scbildt's Java Programmi ng Cookbook

Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 325

Options and Alternatives .. . . . . . . . . . . . . . . . . .. . . . . .. . . . . . . . . . . 327

Use a Daemon Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328

Step-by-Step . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . 329

Discussion , . 329 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329

Bonus Example: A Simple Reminder Class . . . . . . . . . . . . . . . . . . . .. 331

Options and Alternatives . . . . . . . . . . .. . . . . . .. . . . . . . . . . . . . . . . . . 336

Interrupt a Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336

Step-by-Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 337

Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337

Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 337

Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 339

Set and Obtain a Thread's Priority 341 Step-by-Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341

Discussion ... . . . . . . . . . . . . . . . . . . . . . . . .. . . . .. . . . . . . . . . ... 342

Example .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 342

Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 344

Monitor a Thread's State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344

Step-by-Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345

Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 345

Example . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 346

Bonus Example: A Real-Time Thread Monitor . . . . . . . . . . . . . . .. .. 349

Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 353

Use a Thread Group . . . . . . . . . . . . . . . . . . . .. 353

Step-by-Step . .. . . . . . . . . . .. . . . . . . . . . . . . . . . . .. . . . . . . 354

Discussion , 354 Example . . . . . .. .. . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . .. . . . .. 355

Options and Alternatives . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . 357

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

Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362

Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 362

The Top-Level Container Panes . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . 363

Layout Manager Overview 363 Event Handling

:~

. . . . 364

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

Event Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 365

Crea te a Simple Swing Application 366 Step-by-Step . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . .. .. . . . 366

Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 367

Histo rical Note: getContentPane( ) . . . . . . . . . . . . . . . . . . . . . . . . . . 369

p Contents

Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 369

Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371

Set the Content Pane's Layout Manager 372 Step-by-Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 372

Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372

Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373

Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375

Work with JLabel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 376

Step-by-Step . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . .. 376

Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377

Example .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 379

Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . 382

Crea te a Simp le Push Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383

Step-by-Step . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384

Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 4 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 385

Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 387

Use Icons, HTML, and Mnemonics w ith JButton . . . . . . . . . . . . . . . . . . . 390

~~~~

m Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391

Example ... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 393

Options and Alternati ves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 395

Create a Toggle Button 396 Step-by-Step . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 397

Discussion

,

. . . . . .. 397

Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398

Options and Alternatives . . .. . . . . . . . . . . . . . . .. . . . . . . . . . .. 400

Create Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400

Step-by-Step . . . . . . . . . . .

.

. . . . . . . . . . . . . . . . . . . . . . . . . . .. .. 401

Discussion .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401

Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401

Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405

Create Radio Buttons .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405

Step-by-Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . 406

Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406

Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 407

Options an d Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410

Input Text with JTextField 411 Step-by-Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 411

Discussion ... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 412

Example .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413

Bonus Example: Cut, Copy, and Paste . . . . . . . . . . . . . . . . . . . . . . . .. 416

Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419

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

xv

(7)

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 A lternatives .

Create a Custom Excepti on Class .

Step-by-Step .

Discussion

.

Example .

Options and Alternatives .

Schedule a Task for Future Execution .

Step-by-Step .

Discussion

.

Example

,

.

Options and Alternatives .

x vi Herb Schildt 's Java Prog ramming Coo kboo k

Discussion . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . 420

Exam ple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . .. . . . . . . . . . . . 422

Options and Alternatives . . . . . . . . . .. . . . . . . .. . . . . . . . .. . . . . .. . . 42 4 Use a Scroll Bar .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426

Step-by-Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ... . . .

.

427

Discussion . . . . . . . . . . . . . . . . . . . . . . .. . . . . . .. . . . . .. . . . . . . . . . 427

Example . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . 429

Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 431

Use JScrollPane to Handle Scrolling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433

Step-by -Step . . . . . . .. . . . . . . . . . . . . . . . . . . . .. . . . . . . . . .. . . . . . .. . 433

Discussion . . . . . . . . .. . . . . . . . .. . . . . . . . . . . .. . . . . .. . . . . . . . . . . . . 433

Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 433

Options an d Alternatives . . . . .. . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . 436

Display Data in a JTable 438 Step-by-Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 439

Discussion . . . . . . .. . . . . . . .. . . . . . . . . . . . . . . . . . .. . . . . . . . . .. . 440

Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441

Op tions and Alternatives . . . . . . . . . . . .. . . . .. . . . . . . . . . . . .

.

. . . .. 444

H andle JTable Events . . . . . . . . 446

Step-by-Step . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . 447

Discussion . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . .. . . .. . . . . . . . 447

Example . . . . . . . . . . . .. . . . . . . . . . .

.

. . . . . . . .. . . . . . .. . . . . . . . . . .. 450

Options and Alte rnatives . . . . .. . . . . .. . . . . . . . . . . . . . . . .. . .

.

. . .. 455

Display Data in a ]Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456

Step-by-Step . . . . . . . .. . . . . . . .. . . . . . . . . . . . .. . . . . .. . . . . . . . . 458

Discussion . . .. .. .. . . . . .. .. .. . .. .. .. .. .. .. . .. .. . . .. .. .. .. 458

Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 461

Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464

Create a Main Men u . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 466

Step-by-Step . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . .. . . .. . . . . . . 467

Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 467

Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469

Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 471

9 Potpourri . . •• . . . . . . . . . . . . . . . . . . ... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473

Access a Resour ce via an HTTPConnection . . . . . . . . . . . . . . . . . . . . . . . . .. 474

Step-by-Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 474

Discussion .. . . . . . . . . . .. . . . . . .. . . . . . . . . . . . . .. . . . . . . . . .

.

. . . . . . 475

Example .. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476

Options and Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 479

Use a Semaphore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480

Step-by-Step . . . . . . . . . . .. . . . . . .. . . . . . . . . . . . .. . . . .. . . . . . . . . . . 481

Discussion .. . .. . .. . .. . .. .. .. .. . .. .. .. .. .. . . .. . .. .. .. .. .. ... 482

Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482

Options and Alternatives . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485

=

In dex .. .. ... .. ... ... ... ... ...

486 487 487 488 491 491 492 492 493 494 496 496 497 497 498 501 501 502 502 504 505 506 507 507 508

510

Sl1

xvii

References

Related documents