JavaFX
Rich Client
Programming on the
NetBeans Platform
Gail Anderson • Paul Anderson
/Addison-Wesley
Saddle River, NJ •
Boston •
Indianapolis • San Francisco
New York • Toronto •
Montreal •
London • Munich • Paris • Madrid
Capetown •
Sydney •
Tokyo •
Singapore • Mexico City
Contents
Foreword xvii
Preface xix
About the Authors xxiii
Chapter 1 A Tour of the NetBeans Platform 1
What You Will Learn I
I. I Background Basics 2
JavaFX Integration 2
1.2 The NetBeans Platform: The Big Picture 3
Module System API 3
Lookup API 5
Window System API 5
File System API 7
Nodes and Explorer Views 7
But Wait... There's More 8
1.3 FamilyTreeApp Reference Application 10
FamilyTreeApp and JavaFX 11
JavaFX 3D Integration Possibilities 15
1.4 Documentation 17
1.5 How to Get Java and the NetBeans Platform Software 17
1.6 Example Software Bundle 18
1.7 Key Point Summary 18
Chapter 2 Background Basics 1 9
What You Will Learn 20
2.1 JavaBeans and Properties 20
Creating a Java Application 25
Bound Properties 29
v
vi Contents
Coarse-Grained Notification for JavaBean Objects 33
2.2 Lambda Expressions 38
Lambda Expressions with Functional Interfaces 38
Functional Data Structures 39
2.3 Swing Basics 40
Creating a GUI-Form Project 41
Swing Form Designer 44
Program Structure 47
Single-Threaded Model 49
Java Logging Facility 51
Using Swing Components 54
Event Handlers and Loose Coupling 59
2.4 Improving the User Experience 63
2.5 Concurrency and Thread Safety 68
Thread-Safe Objects 68
Adding Listeners and Thread Safety 71
Concurrency in Swing 72
2.6 Swing Background Tasks 73
Introducing SwingWorker 73
Monitoring SwingWorker Status 76
2.7 Key Point Summary 80
What's Next? 81
Chapter 3 Introduction to JavaFX 83
What You Will Learn 83
3.1 What Is JavaFX? 84
A Bit of History 84
The Scene Graph Metaphor 85
Single-Threaded Model 86
3.2 Building JavaFX Programs 87
Creating a JavaFX Application 88
Java APIs 88
Creating a JavaFX FXML Application 93
CSS Files 98
Animation 100
Contents vii
3.3 JavaFX Properties 103
What Is a JavaFX Property? 103
Using Listeners with Observable Properties 105
Read-Only Properties 109
Binding 109
3.4 Putting It All Together 120
3.5 Key Point Summary 128
What's Next? 129
Chapter 4 Working with JavaFX 131
What You Will Learn 131
4.1 Creating JavaFX Properties 132
JavaFX Properties with Lazy Evaluation 133
Object Properties 134
Immutable Properties 135
Computed Properties 135
Methods equals() and hashCode() 138
4.2 Using JavaFX Properties in a JavaFX Application 139
Creating a JavaFX FXML Application 139
4.3 Observable Collections 148
4.4 JavaFX Applications 153
Program Structure 154
Scene Builder and FXML 155
JavaFX Controls 160
JavaFX Controller Class 165
4.5 Concurrency and Thread Safety 172
Concurrency in JavaFX 173
Observable Properties and Thread Safety 173
4.6 JavaFX Background Tasks 176
Worker and Task 176
Progresslndicator 178
4.7 Monitoring Background Tasks 182
Using Method updateValue() 183
Updating a Read-Only JavaFX Property 187
viii Contents
Updating the JavaFX Scene Graph from a Background Task 191
4.8 Key Point Summary 195
Chapter 5 A Taste of Modularity 199
What You Will Learn 199
5.1 Modular Architecture 199
Modules 200
NetBeans Runtime Container 201
5.2 Creating a NetBeans Platform Application 203
5.3 Creating Modules 208
Creating a NetBeans Module 209
Creating Additional Modules 211
5.4 Configuring a Module with Public Packages 211
5.5 Registering a Service Provider 214
Global Lookup 218
5.6 Configuring a Window for Selection 219
Porting Swing Ul Code to a TopComponent 223
Lookup API 224
Configuring the TopComponent 227
5.7 Configuring a Window with Form Editing 231
Another Look at Lookup 239
5.8 Module Life Cycle Annotations 242
Using @OnStart 242
Using @OnStop 243
5.9 What We Know So Far 244
5.10 Key Point Summary 245
What's Next? 247
Chapter 6 JavaFX Integration 249
What You Will Learn 249
6.1 JavaFX and the NetBeans Platform 250
Java 8 and JavaFX 8 Enhancements 251
Create a NetBeans Platform Application 252
Contents ix
Create a NetBeans Module 253
Add a Window to the Module 255
Add JavaFX Content to the TopComponent 256
The Magic of JFXPanel 259
SwingNode 260
6.2 Communication Strategies 261
Accessing the JavaFX Controller Instance 264
6.3 Integrating with the NetBeans Platform 265
Create a NetBeans Platform Application 267
Create NetBeans Platform Modules 268
Configure a Module with Public Packages 269
Register a Service Provider 271
Configure a Window with JavaFX for Selection 275
Configure a Window with JavaFX for Form Editing 283
6.4 Key Point Summary 289
Chapter 7 Nodes and Explorer Views 291
What You Will Learn 291
7.1 The NetBeans Model View Controller 292
7.2 Nodes 293
NodeListener and PropertyChangeListener 295
Building a Node Hierarchy 295
Displaying the Node Hierarchy 303
A Multi-Level Node Hierarchy 304
Using BeanNode 310
Creating Your Own Property Sheet 314
Using FilterNode 317
7.3 Explorer Views 323
Quick Search 323
BeanTreeView 324
OutlineView 324
Master-Detail View 327
PropertySheetView 331
7.4 Creating a Selection History Feature 332
Add Features to Your Application 335
X Contents
7.5 Point Summary 338
Key
341
Chapter 8 NetBeans Platform Window System
What You Will Learn 342
Window Framework Overview 342
8.1
Window Layout 342
Window Modes 345
TopComponents 346
Window Operations 346
Behavior 349
Limiting the Window System's
Window Switching 350
Window Tab Customization 350
Window Manager 350
8.2 TopComponent Basics 352
356
TopComponent Java Code
Window Header Animated Notifications 360
8.3 TopComponent Persistence 361
Windows2Local Folder 363
8.4 TopComponent Client Properties 363
8.5 Creating Non-Singleton TopComponents 364
Opening Windows from User Code 367
8.6 Window System Life Cycle Management 369
Using the Window Manager 373
Using @OnShowing 374
8.7 TopComponent Modes 376
8.8 Window Groups 381
Window Group Example 382
8.9 Window Layout 387
Creating TopComponents 395
A View-Only Window Layout 395
8.10 Window Layout Roles 401
RoleExample Application and Role-Based TopComponents 404
Credential Checking and Role Assignments 405
LoginTopComponent 408
Contents xi
8.11 Key Point Summary 410
Chapter 9 Action Framework 413
What You Will Learn 413
9.1 Type of Actions 414
Always- Enabled Actions 414
9.2 Actions and Lookup 421
Callback Actions 422
Context-Aware Actions 428
9.3 Editing the Node Hierarchy 444
Group Window Node Actions 445
Reorder and Index.Support 451
Implementing Drag and Drop 453
Implementing Cut, Copy, Paste, Delete 456
9.4 Inter-Window Drag and Drop 458
Trash Window Node Actions 458
Implementing Drag and Drop Delete 463
9.5 Key Point Summary 468
Chapter 10 Building a CRUD Application 471
What You Will Learn 471
10.1 Create-Read-Update-Delete Application 472
Defining Capabilities 474
Implementing Read 475
Implementing Delete 480
Implementing Create 482
Implementing Update 488
10.2 Using CRUD with a Database 501
Create Wrapped Libraries 502
JavaDB Server and Database 505
Implement FamilyTreeManager 506
10.3 Concurrency in the FamilyTreeApp Application 516
Concurrency with Read 517
Concurrency with Delete and Create 520
xii Contents
Concurrency with Update 522
10.4 Key Point Summary 523
Chapter 11 Dialogs 525
What You Will Learn 525
11.1 Dialog Overview 526
11.2 Standard Dialogs 527
NotifyDescriptor.Message 527
NotifyDescriptor.Confirmation 529
NotifyDescriptor.lnputLine 531
11.3 Customizing Standard Dialogs 531
11.4 Custom Dialogs 533
Error Handling 535
11.5 Custom Login Dialog 539
11.6 Putting It All Together 544
RoleExample Application 545
11.7 Key Point Summary 548
Chapter 12 Wizards 551
What You Will Learn 551
12.1 Wizard Overview 552
12.2 The Wizard Wizard 554
A Bare-Bones Wizard 555
Registering a Wizard's Action 558
12.3 Wizard Input 561
12.4 Wizard Validation 565
Coordinating Input with Other Panel Wizards
Visual Panel Updates 574
12.5 Simple Validation API 577
Prepare to Use the Validation Library 578
Using the Simple Validation API Library 580
Using a Custom Validator 583
12.6 Finishing Early Option 587
Contents xiii
12.7 Asynchronous Validation 592
12.8 Dynamic Sequence Wizards 598
Building the Dynamic Step Wizard 600
The PizzaWizardlterator 605
IdentifyCustomer Panel 609
BuildPizza Panel 610
Create the OrderPizzaAction 612
12.9 Wizard Instantiating Iterators 614
12.10 Key Point Summary 620
Chapter 13 File System 623
What You Will Learn 623
13.1 File System API 624
13.2 The File System API Overview 624
Exploring the FileSystem API 625
The Output Window 628
Create a Folder 630
Get or Create a File in a Folder 631
Write to and Read from Files 632
Rename and Delete Files 634
File System API Useful Methods 635
13.3 Monitoring File Changes 636
FileObject Attributes 637
Favorites Window 637
Implementing the FileChangeListener 646
13.4 Including a File with Your Application 649
Install a File in a Module 650
Using the InstalledFileLocator Service 653
Installing an NBM in NetBeans IDE 654
13.5 The Layer File and System FileSystem 654
Layer Files and Configuration 655
Exploring the System FileSystem 661
Using the Layer File for Inter-Module Communication 665
13.6 Key Point Summary 668
xiv Contents
Data System 671
Chapter 14
What You Will Learn 671
Data API Overview 672
14.1 System
and Node 673
FileObject, DataObject,
MIME and Lookup 674
FileObject Type
675
DataObject Factory and DataObject Lookup
676
Accessing FileObjects from DataObjects
DataObjects and Nodes 676
DataNode and Lookup 677
Using
14.2 New File Type 678
Creating a
Create a New File Type 681
Create and Edit a New FTR File 688
Provide Child Nodes Based on Content 694
697
14.3 Working with MultiView Windows
699
Using the Visual Library
Using JavaFX 708
14.4 XML-Based File Type 721
Creating an
Create a New XML File Type 725
Add the XML Text Editor 731
Add JavaFX Content 735
14.5 Key Point Summary 740
Chapter 15 JavaFX Charts 743
What You Will Learn 743
Charts and the NetBeans Platform 744
15.1 JavaFX
Overview 746
Application
Working with AbstractTableModel 748
Working with Swing JTable 752
Integrating JavaFX Charts 755
15.2 Introducing JavaFX Charts 759
JavaFX Chart Overview 759
15.3 Data Visualization with JavaFX Charts 764
Line Chart 764
Scatter Chart 767
Contents xv
Bar Chart 768
Area Chart 770
Stacked Area Chart 771
Stacked Bar Chart 772
Bubble Chart 773
Pie Chart 776
15.4 Adding Behaviors to JavaFX Charts 781
Accessing JavaFX Chart Nodes 782
Adding PieChart Features 784
15.5 Saving Charts 789
15.6 Key Point Summary 795
Chapter 16 Using Web Services 799
What You Will Learn 799
16.1 RESTful Web Services and the NetBeans Platform 800
16.2 Creating RESTful Web Services 802
Create Database 802
Create RESTful Web Service Application 803
RESTful Services from Database 804
Entity Classes and JavaFX Properties 807
Test the Web Services 809
16.3 A Java Application Web Service Client 810
16.4 RESTful Web Services in a NetBeans Platform Application 817
Generate RESTful Web Service Clients 817
Application Overview 821
Using JavaFX Services 821
RESTful Client Service Provider 827
Implementing a
JavaFX TableView 831
Chart Module 840
JavaFX
16.5 Key Point Summary 845
Chapter 1 7 Branding, Distribution, and
Internationalization 847
What You Will Learn 847
xvi Contents
17.1 What Is Branding? 848
Using the Branding Menu 848
Customizing the Application Title 852
Customizing the Splash Screen 855
17.2 Application Updates 857
Enable Updates of Your Application 857
Create an Update Center 858
Dynamically Uninstall a Module (Plugin) 860
Adding Modules to an Application 861
Install a Plugin 863
17.3 Application Distribution 864
Create an Installer 865
Installing the Application 867
Customizing the Installer Images 867
17.4 Application Internationalization 868
Internationalization and Java 869
Internationalization and the NetBeans Platform 870
Number Formatting 872
Editing Properties Files 873
Internationalization and JavaFX 873
Testing Target Locales 876
NetBeans Platform Application Internationalization 876
Customizing Resource Bundles 881
17.5 Key Point Summary 883
Index 885