Table of Contents

Preface xxi
Intended audience xxiv
How this book is organized xxvi
Conventions xxvii
Obtaining the source code xxix
Special thanks xxx
Review process for this book xxxii
About the cover illustration xxxv

PART I Getting started 1

1 A Java refresher 3
1.1 What is Java? 4
1.2 What is AWT? 5
1.3 The Java event models 6
Java 1.0 7
Java 1.1 and beyond 8
1.4 JavaBeans 11
1.5 A review of components, listeners, and events 13
1.6 Layout manager refresher 16
BorderLayout 16
CardLayout 17
FlowLayout 18
GridLayout 19
GridBagLayout 20
Combining multiple layouts 22
1.7 Minimum software requirements 23
1.8 Delivering a final product 25
1.9 Just-In-Time compilers 27
1.10 Chapter summary 28
2 Swing basics 29
2.1 What is JFC? 30
2.2 What is Swing? 30
Swing package overview 31
2.3 Why use Swing? 33
JavaBeans compliance 34
Lightweight framework 34
Interaction with external resources 36
2.4 Model-View-Controller architecture 36
How MVC works 37
What can MVC accomplish? 37
Custom rendering 38
Custom data models 38
2.5 Delegates 40
What are delegates? 40
The ComponentUI class 40
2.6 Pluggable LookAndFeel 41
2.7 Creating UI objects 43
Creating a simple component 43
2.8 JComponent, the mother of Swing 45
Pluggable look and feel 46
Keystroke handling 46
Action objects 46
Borders 46
Accessibility 47
Other features of JComponent 47
Controlling component size 47
JComponent hierarchy 48
JComponent constants 48
JComponent fields 49
JComponent constructors 49
JComponent significant method groupings 49
2.9 Chapter summary 53

PART II Using Swing components 55

3 Frames and panels 57
3.1 JFrame 58
A JFrame application 58
JFrame hierarchy 61
JFrame variables 61
JFrame constructors 62
JFrame significant method groupings 62
3.2 JApplet 62
A JApplet sample applet 63
JApplet hierarchy 64
JApplet variables 64
JApplet constructors 64
JApplet significant method groupings 64
3.3 Creating simple panels 65
JPanel hierarchy 66
JPanel constructors 66
3.4 Simple border types 66
Creating a custom border class 68
3.5 Compound border creation 70
3.6 Swing layout managers 72
3.7 JRootPane in detail 76
JRootPane hierarchy 77
JRootPane variables 77
JRootPane constructors 78
JRootPane significant method groupings 78
3.8 Layered panes 78
Layered pane basics 79
A layered pane example 80
JLayeredPane hierarchy 83
JLayeredPane constants 83
JLayeredPane variables 84
JLayeredPane constructors 84
JLayeredPane significant method groupings 84
3.9 Chapter summary 84
4 Tabbed panes, scroll panes, and split panes 87
4.1 Tabbed panes 88
Creating a tabbed pane 90
Adding and inserting pages 90
Removing pages 92
Selecting pages 92
Adding images to tabs 93
Enabling and disabling tabs 93
A complete JTabbedPane example 94
JTabbedPane hierarchy 98
JTabbedPane variables 98
JTabbedPane constructors 98
JTabbedPane significant method groupings 99
4.2 Scrolling panes 100
An example using JScrollPane 102
Controlling the scroll bars 104
Controlling the scrolling programmatically 105
Controlling the size of scroll bars 105
Creating scrollable components 106
JScrollPane hierarchy 108
JScrollPane constructors 108
JScrollPane significant method groupings 109
4.3 Split panes 109
Setting the divider size 113
Controlling the divider position 113
Intercepting JSplitPane events 114
JSplitPane hierarchy 115
JSplitPane constants 115
JSplitPane variables 116
JSplitPane constructors 117
JSplitPane significant method groupings 117
4.4 Advanced scrolling 118
4.5 Chapter summary 123
5 Labels and buttons 125
5.1 Basic user interface components 126
5.2 Labels 126
A JLabel sample program 127
Setting fonts and colors 129
Text alignment 129
Adding an image to a label 130
JLabel hierarchy 132
JLabel variables 132
JLabel constructors 132
JLabel significant method groupings 133
5.3 AbstractButton 134
Listening for button events 134
Adding icons to buttons 137
Enabling and disabling buttons 139
Adding a keyboard mnemonic 139
AbstractButton variables 141
AbstractButton significant method groupings 141
5.4 Push buttons 143
Default buttons 143
JButton constructors 144
The JButton API 144
5.5 Toggle buttons 144
JToggleButton hierarchy 147
JToggleButton constructors 147
5.6 Check boxes 148
Special layout considerations 148
JCheckBox hierarchy 151
JCheckBox constructors 151
5.7 Radio buttons 152
JRadioButton hierarchy 152
JRadioButton constructors 152
5.8 Chapter summary 153
6 Text management 155
6.1 JTextComponent 156
Clipboard operations 158
Saving and loading 158
JTextComponent hierarchy 159
JTextComponent constants 159
JTextComponent constructors 159
JTextComponent significant method groupings 159
6.2 Document handling 161
Document constants 162
Document significant method groupings 163
6.3 Text fields and password fields 165
Associating keyboard mnemonics 166
Special event handling 167
JTextField hierarchy 171
JTextField constructors 171
JPasswordField hierarchy 172
JPasswordField Features 172
JPasswordField constructors 173
JPasswordField significant method groupings 173
6.4 Text areas 173
JTextArea hierarchy 176
JTextArea constructors 176
JTextArea significant method groupings 177
6.5 Combo boxes 177
Adding and removing list items 180
Selecting items 181
Allowing field editing 182
Other combo box tricks 182
JComboBox hierarchy 182
JComboBox constructors 182
JComboBox significant method groupings 183
6.6 Viewing HTML and other content types 184
Listening for hyperlink changes 187
JEditorPane hierarchy 189
JEditorPane constructors 189
JEditorPane significant method groupings 189
6.7 Viewing RTF format 190
6.8 Simple document processing 193
6.9 Chapter summary 197
7 Progress bars, scroll bars, and sliders 199
7.1 Progress bars 200
JProgressBar hierarchy 205
JProgressBar variables 205
JProgressBar constructors 206
JProgressBar significant method groupings 206
7.2 Scroll bars 207
JScrollBar hierarchy 211
JScrollBar variables 211
JScrollBar constructors 211
JScrollBar significant method groupings 212
7.3 Sliders 212
Listening for slider activity 216
JSlider hierarchy 217
JSlider variables 217
JSlider constructors 218
JSlider significant method groupings 218
7.4 Chapter summary 219
8 Menus, toolbars, and actions 221
8.1 Menu bars 224
Creating application menus 224
JMenuBar hierarchy 226
JMenuBar constructors 227
JMenuBar significant method groupings 227
8.2 Menus 227
Creating cascading menus 228
JMenu hierarchy 231
JMenu variables 231
JMenu constructors 231
JMenu significant method groupings 232
8.3 Menu items 233
Adding graphics to menu items 239
Adding ToolTips 239
Menu item keyboard mnemonics 240
Responding to a menu selection 241
JMenuItem hierarchy 241
JMenuItem constructors 241
JMenuItem significant method groupings 242
8.4 Check box menu items 242
Managing check box state 244
JCheckBoxMenuItem hierarchy 245
JCheckBoxMenuItem variables 245
JCheckBoxMenuItem constructors 245
JCheckBoxMenuItem significant method groupings 246
8.5 Radio button menu items 246
JRadioButtonMenuItem hierarchy 248
JRadioButtonMenuItem variables 248
JRadioButtonMenuItem constructors 248
8.6 Pop-up menus 249
JPopupMenu hierarchy 253
JPopupMenu constructors 253
JPopupMenu significant method groupings 253
8.7 Toolbars 254
Toolbar basics 255
A toolbar with many faces 258
Docking and undocking toolbars 259
Adding other components to a toolbar 260
JToolBar hierarchy 261
JToolBar constructors 261
JToolBar significant method groupings 261
8.8 Actions 261
Action basics 262
Common actions for menus and toolbars 263
8.9 Chapter summary 266
9 Dialogs and internal frames 267
9.1 Simple dialogs 269
Handling the close operation 272
Modal vs. nonmodal dialogs 273
A more advanced JDialog example 275
JDialog hierarchy 280
JDialog variables 280
JDialog constructors 280
JDialog significant method groupings 281
9.2 Option dialogs 282
JOptionPane hierarchy 286
JOptionPane constants 286
JOptionPane variables 287
JOptionPane constructors 289
JOptionPane significant method groupings 290
9.3 The color chooser 293
The JColorChooser dialog 293
JColorChooser as a component 296
JColorChooser hierarchy 299
JColorChooser constants 299
JColorChooser constructors 299
JColorChooser significant method groupings 300
9.4 The file chooser 300
JFileChooser hierarchy 305
JFileChooser variables 305
JFileChooser constructors 306
JFileChooser significant method groupings 306
9.5 Internal frames 307
JInternalFrame hierarchy 312
JInternalFrame constants 312
JInternalFrame variables 313
JInternalFrame constructors 314
JInternalFrame significant method groupings 314
9.6 Chapter summary 316
10 List boxes 317
10.1 A simple JList example 318
10.2 A more advanced JList example 320
10.3 Listening for list activity 325
10.4 Custom data model 325
10.5 Basic custom list rendering 328
10.6 Advanced custom list rendering 332
10.7 JList class information 335
JList hierarchy 335
JList constructors 335
JList significant method groupings 336
10.8 Chapter summary 338
11 Trees 341
11.1 Basic tree implementation 342
A basic JTree example 343
Adding new tree items 345
Custom data models 348
Custom rendering 351
Editing tree nodes 357
11.2 Listening for tree actions 360
Listening for tree selections 360
Listening for tree expansions 362
11.3 Tips and tricks for enhancing trees 364
Hiding the root node 365
Expanding and collapsing the tree 365
Selecting and deselecting nodes 365
11.4 JTree class information 366
JTree hierarchy 366
JTree constants 366
JTree variables 367
JTree constructors 368
JTree significant method groupings 369
11.5 Chapter summary 372
12 Tables 375
12.1 Basic table use 376
The simplest of table examples 377
A more complex table example 379
12.2 Adding a custom data model 382
A simple data model example 383
12.3 Adding custom rendering 386
Simple table cell rendering 386
12.4 Rendering column headers 392
Simple header rendering 392
12.5 Listening for table actions 397
Detecting table selections 397
Detecting column property changes 399
12.6 Other tips and tricks 402
Eliminating the annoying gray background 403
Intercepting mouse and keyboard events 403
12.7 Putting it all together 405
12.8 JTable class information 421
JTable hierarchy 421
JTable constants 422
JTable variables 422
JTable constructors 423
JTable significant method groups 424
12.9 Chapter summary 427

PART III Advanced topics 429

13 Creating custom look and feel 431
13.1 Model-view-controller architecture revisited 432
13.2 Why create a look and feel? 433
Corporate standard user interface 433
Ease of portability 433
New component creation 434
13.3 UIManager and JFC look and feel 434
Listing available look-and-feel libraries 435
Changing the current look and feel 437
13.4 Creating a look 440
Building a new button 440
Building chrome menus 454
Supporting themes 462
13.5 Chapter summary 468
14 Optimizing JFC applications 469
14.1 Why optimize? 470
14.2 Using optimization tools 470
A poorly performing test case 470
Sun?s JDK profiler 471
Profiling with Java WorkShop 474
Profiling with OptimizeIt 475
A custom profiler class 477
Native compilation?the easy way out 479
14.3 General rules for Java optimization 481
Loop optimization 481
String handling 483
Numerical data handling 485
Native code 487
Other tips for optimizing Java code 487
14.4 Optimizing JFC applications 488
Adding threads to a JFC application 488
Using JFC timers to reduce CPU usage 490
Other tips for optimizing JFC applications 491
14.5 Chapter summary 492
 
Appendix A Visual development tools 495
Appendix B Other tools 505
Appendix C Sources of information 515
Appendix D References 519
Index 523