Table Of Content00 427x fm 11/22/02 2:25 PM Page i
Michael Landy,
Saleem Siddiqui,
Jeff Swisher, et al.
JBuilder
Developer’s
Guide
800 East 96th Street, Indianapolis, Indiana 46240
00 427x fm 11/22/02 2:25 PM Page ii
JBuilder Developer’s Guide Associate Publisher
Michael Stephens
Copyright 2003 by Sams Publishing
Acquisitions Editor
All rights reserved. No part of this book shall be reproduced, stored
Carol Ackerman
in a retrieval system, or transmitted by any means, electronic,
mechanical, photocopying, recording, or otherwise, without
Development Editor
written permission from the publisher. No patent liability is
Songlin Qiu
assumed with respect to the use of the information contained
herein. Although every precaution has been taken in the prepara- Managing Editor
tion of this book, the publisher and author assume no responsibil- Charlotte Clapp
ity for errors or omissions. Nor is any liability assumed for damages
resulting from the use of the information contained herein. Project Editor
George E. Nedeff
International Standard Book Number: 0-672-32427-X
Copy Editor
Library of Congress Catalog Card Number: 2002104738
Geneil Breeze
Printed in the United States of America
Indexer
First Printing: December 2002 Erika Millen
05 04 03 02 4 3 2 1
Proofreader
Katie Robinson
Trademarks
Technical Editors
All terms mentioned in this book that are known to be trademarks David Sampson
or service marks have been appropriately capitalized. Sams Todd Story
Publishing cannot attest to the accuracy of this information. Use of
a term in this book should not be regarded as affecting the validity Team Coordinator
of any trademark or service mark. Lynne Williams
Multimedia Developer
Warning and Disclaimer
Dan Scherf
Every effort has been made to make this book as complete and as
Interior Designer
accurate as possible, but no warranty or fitness is implied. The
Gary Adair
information provided is on an “as is” basis. The authors and the
publisher shall have neither liability nor responsibility to any
Cover Designer
person or entity with respect to any loss or damages arising from
Alan Clements
the information contained in this book or from the use of the CD
or programs accompanying it. Page Layout
Michelle Mitchell
00 427x fm 11/22/02 2:25 PM Page iii
Contents at a Glance
Introduction.................................................................................1
Part I Starting Out—A First Look at JBuilder
1 Viva Java......................................................................................7
2 Exploring JBuilder........................................................................15
3 Employing the JBuilder Designers...................................................43
Part II JBuilder for Application Design, Modeling, and Testing
4 Modeling....................................................................................63
5 Unit Testing................................................................................81
6 Debugging ................................................................................107
7 Refactoring................................................................................131
8 Documenting in JBuilder.............................................................155
9 Deployment..............................................................................173
Part III JBuilder for Java Client Applications
10 AWT.........................................................................................191
11 Swing.......................................................................................217
12 Understanding Layout Managers...................................................245
13 Drawing with Java......................................................................277
14 Files, Streams, and Serialization ....................................................287
15 Database Connectivity................................................................323
Part IV JBuilder for Web-based Applications
16 Servlets.....................................................................................373
17 Java Server Pages........................................................................411
18 Tag Libraries..............................................................................445
19 InternetBeans Express .................................................................487
00 427x fm 11/22/02 2:25 PM Page iv
Part V JBuilder for Enterprise Applications
20 XML.........................................................................................503
21 An Architectural Overview of Enterprise JavaBeans...........................551
22 Developing Session Beans............................................................565
23 Developing Entity Beans..............................................................611
24 Message-Driven Beans.................................................................683
25 CORBA.....................................................................................703
26 Debugging Your Enterprise Application..........................................739
27 Development and Deployment for WebLogic and WebSphere............761
28 Web Services..............................................................................785
29 Using JBuilder with J2ME.............................................................817
30 Team Development with JBuilder..................................................839
31 Struts and JBuilder......................................................................863
Index.......................................................................................893
00 427x fm 11/22/02 2:25 PM Page v
Table of Contents
Introduction 1
Part I Starting Out—A First Look at JBuilder
1 Viva Java 7
From Oak to J2EE—A Brief History of Java ........................................8
Borland JBuilder, a First Look ........................................................10
What Is Next? .............................................................................11
Summary ...................................................................................12
2 Exploring JBuilder 15
The JBuilder Integrated Development Environment ..........................15
The Main Menu ..........................................................................17
File Menu ...........................................................................17
Edit, Search, and View Menus ................................................17
The Edit, Format Menu Option .............................................18
Project Menu ......................................................................21
Tools Menu ........................................................................30
The Other Menus ................................................................34
The Toolbar ................................................................................35
In Practice ..................................................................................36
The Welcome Project ...........................................................36
The Hello World Project .......................................................37
Summary ...................................................................................41
Reflections .........................................................................42
3 Employing the JBuilder Designers 43
Screen Designer ..........................................................................43
Activating a Designer ...........................................................44
Writing Event Handlers ................................................................50
How an Event Is Processed ....................................................51
Using Anonymous Adapters ..................................................52
Using Standard Adapters .......................................................53
Changing Adapter Settings ....................................................53
00 427x fm 11/22/02 2:25 PM Page vi
vi JBuilder Developer’s Guide
Menu Designer ...........................................................................54
Menu Designer Buttons ........................................................55
Building a Menu with the Menu Designer ...............................55
Implementing a Menu Action ................................................57
Creating a Pop-Up Menu ......................................................58
In Practice ..................................................................................59
Creating the Application Framework .......................................60
Summary ...................................................................................60
Reflections .........................................................................60
Part II JBuilder for Application Design, Modeling, and Testing
4 Modeling 63
A “Warp 6” Overview of UML .......................................................64
Elements of UML .................................................................64
Class Diagrams in UML ........................................................65
Showing Packages in Class Diagrams .......................................67
JBuilder and UML ........................................................................68
Class Diagrams in JBuilder ....................................................68
Package Diagrams in JBuilder .................................................71
Design Guidelines—Modeling .......................................................71
“Avoid Circular Dependencies” ..............................................72
“Do One Thing—Do It Well” .................................................73
“Encapsulation: Show Operations and Hide Data” .....................73
“Think at the Appropriate Level of Abstraction” .......................74
In Practice ..................................................................................74
Modeling the Server .............................................................76
Modeling the Client .............................................................78
Summary ...................................................................................79
Reflections .........................................................................80
5 Unit Testing 81
JBuilder Test Options ...................................................................82
JUnit Testing Framework ..............................................................84
Creating a Test with JUnit .....................................................85
Running a Test with JUnit .....................................................86
JBuilder Test Runner, Test Cases, and Test Suites ...............................88
Creating a Test Case Using JBuilder ........................................89
Creating a Suite Using JBuilder ..............................................92
00 427x fm 11/22/02 2:25 PM Page vii
Contents vii
Comparison, JDBC, and JNDI Fixtures ............................................95
Comparison Fixture .............................................................95
JDBC Fixture .......................................................................97
JNDI Fixture ......................................................................100
In Practice ................................................................................100
Step 1: Opening the Unit Testing Sample Project .....................101
Step 2: Adding a Unit Test Case to the Project ........................101
Step 3: Adding a Comparison Fixture to the Project .................104
Summary .................................................................................106
Reflections ........................................................................106
6 Debugging 107
Integrated Debugger ...................................................................107
Debug Controls .................................................................108
Debug Views .....................................................................110
Debug Session and Status ....................................................111
Fundamental Debugging .............................................................112
Real-Time Error Checking ....................................................112
Compiler Errors .................................................................113
Setting Breakpoints ............................................................113
More Debugging Options ............................................................117
Breakpoint Options ............................................................117
Class Tracing .....................................................................118
In Practice ................................................................................119
Step 1: Opening the Debugging Example Project .....................119
Step 2: Fixing the Syntax Errors ............................................120
Step 3: Fixing the Compile-Time Errors ..................................122
Step 4: Fixing the Runtime Errors, Setting Breakpoints,
Evaluating Variables, and Modifying Variables ......................123
Summary .................................................................................128
Reflections ........................................................................129
7 Refactoring 131
What Is Refactoring? ..................................................................131
Move Refactor ...........................................................................132
Rename Refactor .......................................................................135
Renaming a Package ...........................................................137
Renaming a Class or Interface ..............................................140
Renaming a Method ...........................................................141
Renaming a Field or a Local Variable .....................................143
Renaming a Property ..........................................................143
00 427x fm 11/22/02 2:25 PM Page viii
viii JBuilder Developer’s Guide
Changing Parameters .................................................................143
Extract Method .........................................................................145
Introduce Variable .....................................................................146
Optimize Imports ......................................................................148
Surround with Try/Catch ............................................................150
In Practice ................................................................................151
Step 1: Opening the Example Project and Reviewing the Code ..151
Step 2: Moving the Class moveExample.....................................152
Step 3: Renaming the Class moveExample .................................152
Step 4: Renaming a Variable ................................................153
Summary .................................................................................154
Reflections ........................................................................154
8 Documenting in JBuilder 155
Adding Comments .....................................................................155
JavadocTags ..............................................................................157
JavadocProvides More than Documentation ...................................158
Documentation Doesn’t Match Code ............................................159
Automatic JavadocTemplates .......................................................159
Using the JavaDoc Wizard ...........................................................160
Design Guidelines ......................................................................169
Documentation Usage Focused .............................................169
Style Guide .......................................................................170
Summary .................................................................................172
Reflections ........................................................................172
9 Deployment 173
Java Archive Files .......................................................................173
Manifest File .............................................................................174
Java Archive Tool .......................................................................175
Viewing an Archive ............................................................176
Updating an Archive ..........................................................176
Extracting an Archive .........................................................177
JBuilder’s Archive Builder ............................................................177
Quick Deployment .............................................................183
Design Guidelines ......................................................................185
Package Sealing .................................................................186
In Practice ................................................................................186
Summary .................................................................................187
Reflections ........................................................................187
00 427x fm 11/22/02 2:25 PM Page ix
Contents ix
Part III JBuilder for Java Client Applications
10 AWT 191
AWT Basics ...............................................................................191
Applications Versus Applets .........................................................192
Basic GUI Logic .................................................................192
A Simple Example ..............................................................193
Working with AWT Containers ....................................................195
Creating a Container ..........................................................196
Common Container Methods ..............................................198
Working with AWT Components .................................................198
Button ..............................................................................200
Canvas ..............................................................................202
Checkbox ...........................................................................204
CheckboxGroup ....................................................................205
Choice ..............................................................................206
Label................................................................................207
List .................................................................................207
Scrollbar ..........................................................................209
TextField ..........................................................................209
TextArea ...........................................................................210
Common Component Methods ...........................................211
Separate User Interface Logic ...............................................211
In Practice ................................................................................211
Creating an Applet with the Applet Wizard ............................212
Building the Interface .........................................................213
Accepting Input .................................................................214
Summary .................................................................................214
Reflections ........................................................................215
11 Swing 217
Why Use Swing Instead of AWT? ..................................................217
Design Guideline—Model View Controller Architecture ...................219
Simple MVC with Pull Semantics ..........................................219
MVC with Push Semantics ..................................................220
MVC Meets Swing ..............................................................222
Swing Component Hierarchy .......................................................224
Working with Swing in JBuilder ...................................................228
00 427x fm 11/22/02 2:25 PM Page x
x JBuilder Developer’s Guide
In Practice ................................................................................231
Creating a Project and Application .......................................232
Designing the User Interface of the JFrameand JPanels .............232
Designing the dataobjectsPackage and Classes .......................235
Designing the CommonOperationsClass ....................................239
Connecting the User Interface to CommonOperations ..................241
Summary .................................................................................243
Reflections ........................................................................244
12 Understanding Layout Managers 245
Uncovering Layout Managers ......................................................246
Layout Managers Available ..........................................................247
Using a Layout Manager .............................................................249
Changing Layout Properties .................................................249
Meeting of Components and Layout Managers .......................249
Using the XYLayoutManager ........................................................250
When to Use XYLayoutManager ............................................250
Aligning the Components ...................................................251
An Example of Using XYLayout ..............................................251
Using the BorderLayoutManager ...................................................251
When to Use BorderLayout ...................................................252
An Example of Using BorderLayout ........................................252
Using the FlowLayoutManager ......................................................253
When to Use FlowLayout ......................................................253
Important Properties ..........................................................254
An Example of Using FlowLayout ...........................................254
Using VerticalFlowLayout ............................................................255
When to Use VerticalFlowLayout ...........................................255
Properties .........................................................................255
An Example of Using VerticalFlowLayout ................................255
Using the BoxLayout2Manager ......................................................256
Using the GridLayoutManager ......................................................257
When to Use GridLayout ......................................................257
Properties .........................................................................257
An Example of Using GridLayout ...........................................257
Using the CardLayoutManager ......................................................259
When to Use CardLayout ......................................................259
Properties .........................................................................260