pro javafx 2 : a definitive guide to rich clients with ... · pdf filecontents foreword xv...
TRANSCRIPT
Pro JavaFX 2
James L. Weaver
Weiqi Gao, Ph.D.
Stephen Chin
Dean Iverson
with Johan Vos, Ph.D.
Apress*
Contents
Foreword xv
About the Authors xvi
About the Technical Reviewer xviii
Acknowledgments xix
Chapter 1: Getting a Jump Start in JavaFX 1
avaFX Can't Bring Rich-Client Java Back by Itself 1
Brief History of JavaFX 2
oing to the Source: Oracle's JavaFX Web Site 4
Accessing the JavaFX SDK API 5
Obtaining the JavaFX SDK, 6
Other Available Tools 7
Developing Your First JavaFX Program: "Hello Earthrise" 7
Compiling and Running from the Command-Line 7
Understanding the Hello Earthrise Program 8
Building and Running the Program with NetBeans 17
Developing Your Second JavaFX Program: "More Cowbell!" 21
Building and Running the Audio Configuration Program 21
The Behavior of the Audio Configuration Program 22
Understanding the Audio Configuration Program 23
Colors and Gradients 27
iv
m CONTENTS
The Model Class for the Audio Configuration Example 28
Defining Change Listeners in the Model class 29
Surveying JavaFX Features 31
Summary 32
Resources, 33
Chapter 2: Creating a User Interface in JavaFX 35
Introduction to Node-Centric Uls 35
Setting the Stage 36
Understanding the Stage Class 36
Using the Stage Class: The StageCoach Example 37
Understanding the StageCoach Program 41
Making a Scene 50
Using the Scene Class: The OnTheScene Example 50
Understanding the OnTheScene Program 52
Handling Input Events 62
Surveying Mouse and Keyboard Events and Handlers 62
Understanding the KeyEvent Class 62
Understanding the MouseEvent Class 62
Animating Nodes in the Scene 63
Using a Timeline for Animation 64
Using the Transition Classes for Animation 71
The Zen of Node Collision Detection 80
Summary 91
Resources 92
Chapter 3: Properties and Bindings 93
Forerunners of JavaFX 2 Binding 93
A Motivating Example 94
v
951 CONTENTS
Understanding Key Interfaces and Concepts 99
Understanding the Observable Interface 100
Understanding the ObservableValue Interface 100
Understanding the WritableValue Interface 101
Understanding the ReadOnlyProperty Interface 101
Understanding the Property Interface 102
Understanding the Binding Interface 103
Type-Specific Specializations of Key Interfaces 105
A Common Theme for Type-Specific Interfaces 106
Commonly Used Classes 109
Creating Bindings 110
Understanding the Bindings Utility Class 110
Understanding the Fluent Interface API 114
Understanding the JavaFX Beans Convention 125
The JavaFX Beans Specification 125
Understanding the Eagerly Instantiated Properties Strategy 126
Understanding the Lazily Instantiated Properties Strategy 130
Using Selection Bindings 132
Summary 135
Resources 136
Chapter 4: Building Dynamic Ul Layouts in JavaFX ....137
Introducing JavaFX Reversi 137
Board Layout and Basic Rules 138
Building a JavaFX Model for Reversi 140
Dynamic Layout Techniques 141
Centering Text Using Bind 142
Centering Revisited Using a StackPane 143
Aligning to Edges Using StackPanes and TilePanes 145
vi
m CONTENTS
Using FlowPane and Boxes for Directional Alignment 148
Composing a Layout Using BorderPane 153
Creating Custom Regions 156
Building a Custom Square Region 156
Building a Resi2able Reversi Piece 159
Laying Out the Tiles Using a GridPane 162
Aligning and Stretching with AnchorPane 165
When to Use Different Layouts 167
A New Perspective on Reversi: The JavaFX 3D Scene Graph 169
Bringing Reversi to Life 172
Highlighting Legal Moves 172
Highlighting the Active Cell 175
Taking Turns 178
Additional Game Enhancements 180
Summary 181
Resources 181
SChapter 5: Using the JavaFX Ul Controls 183
Trying Out the JavaFX Ul Controls 183
Leveraging the JavaFX Ul Controls 192
Setting the Stage for the StarterApp Program 194
Creating a Menu and Defining Menu Items 196
Creating a Toolbar 197
Creating a TabPane and Defining Tabs 203
Creating a TableView 205
Creating an Accordion and Defining a TitledPane 208
Creating a TreeView 209
Creating a ListView and Assigning Items to a ListView 212
vii
CONTENTS
Creating a SplitPane 212
Defining a ScrollPane 213
Using a CheckBox 218
Defining a RadioButton 218
Creating a Hyperlink 219
Defining a ChoiceBox 219
Using a MenuButton 220
Creating a ContextMenu 221
Creating a SplitMenuButton 222
Defining a TextField 222
Using a PasswordField 223
Creating a TextArea 223
Creating a Slider 224
Defining a Progresslndicator 224
Defining a ScrollBar 225
Using a ProgressBar 225
Creating an HTMLEditor 226
Creating a Popup 227
UsingaWebView 228
Summary 229
Resources 229
Chapter 6: Collections and Concurrency 231
Understanding Observable Collections 231
Understanding ObservableList 232
Handling Change Events in ListChangeListener 236
Understanding ObservableMap 243
Using Factory and Utility Methods from FXCollections 248
viii
CONTENTS
Using the JavaFX Concurrency Framework 254
Identifying the Threads in a JavaFX Application 255
Fixing Unresponsive Ills 261
Understanding the javafx.concurrent Framework 267
Embedding JavaFX Scenes in Swing and SWT Applications 287
Summary 304
Resources 305
Chapter 7: Creating Charts in JavaFX 307
Structure of the JavaFX Chart API 307
Using the JavaFX PieChart 308
The Simple Example 308
Some Modifications 312
Using the XYChart 316
Using the ScatterChart 317
Using the LineChart 325
Using the BarChart 326
Using the AreaChart 328
Using the BubbleChart 329
Summary 334
Resources 334
Chapter 8: Using the Media Classes 335
The Foundation 335
Supported Media Formats 335
Working with Audio Clips 336
Controlling the Playback Parameters of an AudioClip 339
Constructing the Scene 340
AudioClip Wrap-Up 343
CONTENTS
Working with Media 344
Playing Audio 345
Error Handling 346
Displaying Metadata 347
Loading Media 350
Controlling Playback 356
Audio Equalization 368
MediaPlayerWrap-Up 379
Playing Video 380
Controlling the Size of a MediaView 381
MediaView and Effects 383
Using Markers 383
One Player, Multiple Views 385
Converting AudioPlayer into a VideoPlayer 388
Summary 390
Chapter 9: Accessing Web Services 391
Front-end and Back-end Platforms 391
Merging JavaFX and Java Enterprise Modules in the Same Environment 393
Using JavaFX to Call Remote (Web) Services 395
SOAP 395
REST 395
Summary 429
Chapter 10: JavaFX Languages and Markup 431
A Quick Comparison of Alternative Languages 431
Vanishing Circles in Java 432
Vanishing Circles in Alternative JVM Languages 435
x
3 CONTENTS
Making Your JavaFX Groovy 439
Introduction to GroovyFX 440
Properties in GroovyFX 445
GroovyFX Binding 446
GroovyFX API Enhancements 448
Scala and JavaFX 453
Getting Started with ScalaFX 453
ScalaFX Proxies and Implicit Conversions 458
JavaFX Properties in Scala 459
ScalaFX Bind APIs 461
API Enhancements 462
Visage, the JavaFX Language 465
Advantages of Using Visage 465
Getting Started with Visage 466
Constructing Uls with FXML Markup 468
Learning FXML by Example 468
Controlling FXML Applications 473
Summary 475
Resources 475
Appendix: The Visage Language in Depth 477
An Overview of Visage 477
Variables, Values, and Their Types 478
Variable Names 479
Variable Types 479
Primitive Types 480
Boolean Type 480
Integer Type 481
Character Type 484
xi
CONTENTS
Byte, Short, and Long Types 485
Float and Number Types 486
Double Types 488
String Type 488
Duration Type 491
Length Type 493
Angle Type 496
Color Type 497
Working with Sequences 499
Sequence Types 500
Constructing Sequences Using Explicit Sequence Expressions 501
Constructing Numeric Sequences Using Range Expressions 501
Manipulating Sequences 503
Comprehending Sequences 509
Using Utility Functions in visage.util.Sequences 511
Visage Expressions 512
Expressions and Their Types 512
Block Expression 513
Precedence and Groupings 514
Expression Separator 514
Variable and Constant Declarations 514
Assignment Operator 515
Compound Assignment Operators 516
Relational Operators 516
While Expression 518
Revisiting the for Expression 519
If Expression 520
xii
CONTENTS
Object Literals 522
Classes and Objects 523
The Object Literal Expression 523
Manipulating Objects 526
Handling Nulls in Visage 528
Creating Java Objects with the new Operator 529
Making of a Declarative Syntax 529
Working with Data Bindings 530
Bind Expression 530
Bidirectional Bindings and Lazy Bindings 533
Working with Functions 536
Function Definitions 536
Overloaded Functions 543
Function Types and Anonymous Functions 546
Bound Functions 549
Handling Exceptions 551
Working with Classes 555
Class Definitions 555
Creating Class Hierarchies 564
Organizing Visage Code 573
Scripts 574
Modules 575
A Special Provision for Classes 576
Packages 577
Import Directives 579
Access Modifiers 581
xiii
CONTENTS
Triggers 583
Accessing the Old and New Values in Trigger Blocks 584
Accessing Sequence Modification Information in Trigger Blocks 586
Debugging with Triggers 588
String Formatting and internationalization 589
Using String Format Specifications 589
Internationalizing Visage Programs 592
Leveraging Java from Visage 594
Instantiating Java Classes 594
Accessing Java Object Fields 595
Calling Java Methods 595
Accessing Static Fields and Methods 595
Quoting Visage Keywords 596
Accessing Nested Classes 596
Accessing Java Enums 597
Extending Java Classes and Interfaces 597
Dealing with Java Arrays 599
Iterating Through Java Collections 599
Visage Reflection 600
Mirror-Based Reflection 600
Entering the Reflection World 601
Invoking Instance and Script Functions 606
Resources 607
Index 609
xiv