About the Authors .................................................................................................. xiii
About the Technical Reviewer .................................................................................xv
Contributors ...........................................................................................................xvii
Chapter 1: Getting Started with Client Java ............................................................. 1
Java Client Technology in Action ................................................................................................... 1
Java Clients in Business .......................................................................................................... 2
Gaming and 3D ............................................................................................................................. 3
Mobile Conference Apps ............................................................................................................... 5
A Modern Approach to Client Java ................................................................................................ 7
Target Mobile First ................................................................................................................... 7
Build for the Cloud ................................................................................................................... 9
Package Your Platform .......................................................................................................... 11
Setting Up Your Environment ...................................................................................................... 12
macOS JDK Installation ......................................................................................................... 13
Windows JDK Installation ...................................................................................................... 14
Linux JDK Installation ............................................................................................................ 18
JavaFX Installation ................................................................................................................ 18
Your First Modern Java Client ..................................................................................................... 20
Coding Client Apps with IntelliJ IDEA .................................................................................... 20
Rapid Application Development with Scene Builder .............................................................. 25
The Path to Modern Client Development .................................................................................... 32
Chapter 2: JavaFX Fundamentals ............................................................................................... 33
JavaFX Stage and Scene Graph .................................................................................................. 33
JavaFX Is Single-Threaded .................................................................................................... 34
Hierarchical Node Structure .................................................................................................. 34
A Simple Shape Example ...................................................................................................... 36
Color .................................................................................................................................. 38
Text Is a Shape .................................................................................................................... 39
The JavaFX Coordinate System ............................................................................................... 39
Layout Controls ..................................................................................................................... 40
StackPane ............................................................................................................................. 40
AnchorPane ........................................................................................................................... 41
GridPane ................................................................................................................................ 41
FlowPane and TilePane ......................................................................................................... 42
BorderPane ............................................................................................................................ 42
SplitPane ............................................................................................................................... 43
HBox, VBox, and ButtonBar .................................................................................................... 43
Make a Scene ........................................................................................................................ 43
Enhancing the MyShapes Application ......................................................................................... 44
Linear Gradient ...................................................................................................................... 44
DropShadow .......................................................................................................................... 45
Reflection .............................................................................................................................. 46
Configuring Actions ..................................................................................................................... 47
Animation .................................................................................................................................... 48
JavaFX Properties ....................................................................................................................... 50
Property Listeners ................................................................................................................. 51
Subscription-Based Listeners ................................................................................................... 54
Binding .................................................................................................................................. 55
Using FXML ................................................................................................................................. 57
Controller Class ..................................................................................................................... 61
Putting It All Together .................................................................................................................. 66
Master-Detail UI ..................................................................................................................... 66
Observable Lists .................................................................................................................... 71
Person UI Application Actions ................................................................................................ 74
Person UI with Records ......................................................................................................... 79
Key Point Summary ..................................................................................................................... 81
Chapter 3: Properties and Bindings ................................................................................................ 85
Key Concepts .............................................................................................................................. 86
Observable and InvalidationListener ..................................................................................... 87
ObservableValue and ChangeListener ................................................................................... 87
Subscription .......................................................................................................................... 88
WritableValue and ReadOnlyProperty .................................................................................... 88
JavaFX Properties .................................................................................................................. 88
Creating Bindings .................................................................................................................. 95
JavaFX Bindings .................................................................................................................... 95
Create Bindings by Direct Extension ..................................................................................... 96
Type-Specific Specializations ..................................................................................................... 98
Factory Methods in Bindings .................................................................................................... 101
Create Bindings with the Fluent API .......................................................................................... 104
Observable Collections ............................................................................................................. 109
Factory and Utility Methods in FXCollections ...................................................................... 109
Change Listeners for Observable Collections ...................................................................... 114
Create Bindings for Observable Collections ........................................................................ 129
JavaFX Beans ................................................................................................................... 129
Eagerly Instantiated Property .............................................................................................. 130
Half-Lazily Instantiated Property ......................................................................................... 132
Fully Lazily Instantiated Property ........................................................................................ 133
Selection Bindings ............................................................................................................... 134
Adapting Java Beans ........................................................................................................... 137
Summary .................................................................................................................................. 143
Resources ................................................................................................................................. 144
Chapter 4: JavaFX Controls Deep Dive ......................................................................................... 147
The UI Controls Module ............................................................................................................. 147
What Is a UI Control? ................................................................................................................. 148
JavaFX Basic Controls ............................................................................................................... 149
Labeled Controls .................................................................................................................. 149
Text Input Controls ............................................................................................................... 155
Other Simple Controls ......................................................................................................... 158
Container Controls .............................................................................................................. 160
Accordion and TitledPane ...................................................................................................... 160
ButtonBar ............................................................................................................................ 161
ScrollPane ........................................................................................................................... 162
SplitPane ............................................................................................................................. 164
TabPane ............................................................................................................................... 165
ToolBar ................................................................................................................................. 167
Other Controls ...................................................................................................................... 168
HTMLEditor .......................................................................................................................... 168
Pagination ............................................................................................................................ 168
ScrollBar .............................................................................................................................. 170
Separator ............................................................................................................................. 170
Spinner ................................................................................................................................ 171
Tooltip .................................................................................................................................. 172
Popup Controls ...................................................................................................................... 173
Menu-Based Controls .............................................................................................................. 173
ComboBox-Based Controls ...................................................................................................... 181
JavaFX Dialogs ...................................................................................................................... 185
Alert ..................................................................................................................................... 186
ChoiceDialog ........................................................................................................................ 187
TextInputDialog .................................................................................................................... 187
Dialog and DialogPane ........................................................................................................... 188
Advanced Controls ................................................................................................................ 189
ListView ............................................................................................................................... 190
TreeView .............................................................................................................................. 195
TableView ............................................................................................................................ 197
TreeTableView ...................................................................................................................... 204
Selection and Focus Models ..................................................................................................... 207
SelectionModel .................................................................................................................... 207
FocusModel ......................................................................................................................... 208
Summary ........................................................................................................................... 209
Acknowledgments ............................................................................................................... 209
Chapter 5: Mastering Visual and CSS Design ............................................................................ 211
Introduction to Cascading Style Sheets .................................................................................... 211
Selectors Based on Class Name .......................................................................................... 214
Selectors Based on Custom Style Classes ....................................................,,,...................... 215
Selectors Based on Object ID .............................................................................................. 215
Applying CSS Styles ........................................................................................................... 215
Loading CSS Stylesheets ..................................................................................................... 215
Applying CSS Styles to JavaFX Nodes ...............................................................,,,................. 217
Advanced CSS Techniques .................................................................................................. 220
Using Descendant Selectors ................................................................................................ 220
Using Pseudo-classes ......................................................................................................... 220
Using Imports ...................................................................................................................... 221
Font Loading in the Stylesheet ............................................................................................ 221
Reusing Styles ..................................................................................................................... 222
Using Advanced Color Definitions ........................................................................................ 223
Using Linear Gradients ........................................................................................................ 223
Using Radial Gradients ........................................................................................................ 224
Using Image Patterns .......................................................................................................... 224
Using RGB Color Definitions ................................................................................................ 225
Using HSB Color Definitions ................................................................................................ 227
Using Color Functions .......................................................................................................... 227
Using Effect Definitions ....................................................................................................... 228
Useful Tips and Tricks .......................................................................................................... 230
Advanced CSS API ..................................................................................................................... 233
Platform Preferences API .......................................................................................................... 241
CSS in JavaFX Applications: Summary ..................................................................................... 244
Chapter 6: High-Performance Graphics ......................................................................................... 245
Using Canvas ............................................................................................................................ 246
Giving Life to a Canvas Application ........................................................................................... 252
Particle Systems ....................................................................................................................... 257
Fractals ..................................................................................................................................... 267
High Performance ..................................................................................................................... 280
Conclusion ................................................................................................................................ 292
Chapter 7: Bridging Swing and JavaFX ....................................................................................... 293
Integrating JavaFX into Swing .................................................................................................. 293
JFXPanel: Swing Component with JavaFX Inside ....................................................................... 294
Threading ............................................................................................................................ 299
Interaction Between Swing and JavaFX .................................................................................... 300
Drag and Drop with JavaFX and Swing ..................................................................................... 308
JavaFX 3D Integrated in Swing ................................................................................................. 311
Integrating Swing into JavaFX .................................................................................................. 313
Migration Strategies ................................................................................................................. 315
Large-Scale Integrations ........................................................................................................... 315
Conclusion ................................................................................................................................ 316
Chapter 8: JavaFX 3D ................................................................................................................. 317
Prerequisites ............................................................................................................................. 317
Getting Started with Shapes ..................................................................................................... 318
Shape3D .............................................................................................................................. 323
Sphere ................................................................................................................................. 324
Box ...................................................................................................................................... 325
Cylinder ............................................................................................................................... 325
Creating User-Defined 3D Shapes ............................................................................................ 326
Camera ................................................................................................................................ 339
ParallelCamera .................................................................................................................... 340
PerspectiveCamera ............................................................................................................. 340
Light .............................................................................................................................. 344
Understanding the LightBase Class ..................................................................................... 344
Understanding the AmbientLight Class ............................................................................... 345
Understanding the PointLight Class .................................................................................... 345
Material ......................................................................................................................... 353
Understanding the PhongMaterial Class ............................................................................. 354
Adding Texture to 3D Shapes ............................................................................................... 357
Interacting with JavaFX 3D Scenes .......................................................................................... 361
Understanding the PickResult Class .................................................................................... 362
Third-Party Software: FXyz 3D .................................................................................................. 367
FXyz 3D Sample ................................................................................................................... 368
Conclusion ................................................................................................................................ 370
Chapter 9: JavaFX, the Web, and Cloud Infrastructure ................................................................... 371
Integrating with the Web ........................................................................................................... 373
Displaying a Web Page ........................................................................................................ 373
Adding Navigation and History ............................................................................................ 375
Showing Loading Progress .................................................................................................. 377
From Web Sites to APIs ........................................................................................................ 378
Building for the Cloud ............................................................................................................... 378
Architecture of a JavaFX Cloud Application ......................................................................... 379
Use Case: Querying OpenWeather ....................................................................................... 381
Conclusion ................................................................................................................................ 398
Chapter 10: Packaging Apps for the Desktop ................................................................................ 399
Web vs. Desktop Applications ................................................................................................... 399
Evolutions in Application Deployment ....................................................................................... 401
The jpackage Tool ..................................................................................................................... 403
What Is jpackage? ............................................................................................................... 403
Using jpackage .................................................................................................................... 405
Using GraalVM’s Native Image .................................................................................................. 429
Platform Requirements ........................................................................................................ 430
The Code .............................................................................................................................. 432
Maven Project ...................................................................................................................... 433
Gradle Project ...................................................................................................................... 435
Build the Project .................................................................................................................. 436
Compile ............................................................................................................................... 437
Conclusion ................................................................................................................................ 440
Chapter 11: Native Mobile Apps for iOS and Android ..................................................................... 441
Why JavaFX on Mobile .............................................................................................................. 442
Different Approaches for Mobile Apps ......................................................................................... 443
OS-Specific Native Controls ...................................................................................................... 444
Mobile Websites ........................................................................................................................ 444
Device Native Rendering ........................................................................................................... 444
Hello, JavaFX on iOS and Android ............................................................................................. 446
GluonFX Plugins to Reduce Complexity ...................................................................................... 446
The Development Flow ............................................................................................................ 447
The Code .............................................................................................................................. 449
How Does It Work? .................................................................................................................... 462
Using the Plugin Options ........................................................................................................... 463
bundlesList .......................................................................................................................... 463
resourcesList ....................................................................................................................... 464
reflectionList ........................................................................................................................ 464
jniList ................................................................................................................................... 465
runAgent Task/Goal ............................................................................................................. 465
Creating Real Mobile-Looking Apps .......................................................................................... 465
Different Stylesheets ........................................................................................................... 466
Mobile-Specific Controls ..................................................................................................... 470
Summary .................................................................................................................................. 480
Chapter 12: JavaFX 23 on Raspberry Pi ......................................................................................... 481
Intro to Raspberry Pi ................................................................................................................. 481
Getting Started with a Raspberry Pi .......................................................................................... 482
Initial Kit .............................................................................................................................. 482
Java 21 ................................................................................................................................ 490
Installing JavaFX 23 ............................................................................................................ 492
Running JavaFX Applications Remotely .............................................................................. 503
Creating JavaFX Native Images ........................................................................................... 511
Working with Dependencies ................................................................................................ 515
Deploy and Test ................................................................................................................... 550
Conclusions ........................................................................................................................... 552
Chapter 13: Machine Learning and JavaFX .................................................................................. 553
What Is Machine Learning ........................................................................................................ 553
Supervised Learning ................................................................................................................. 554
Unsupervised Learning ............................................................................................................. 554
Artificial Neural Networks ......................................................................................................... 554
Convolutional Neural Networks ................................................................................................. 556
Eclipse DeepLearning4J: Java API for Neural Networks ........................................................... 558
Training Neural Networks from a JavaFX Application ............................................................... 561
Read an Image from JavaFX to a Neural Network .................................................................... 566
Detecting Objects in a Video ..................................................................................................... 571
Chapter 14: Scientific Applications Using JavaFX ........................................................................ 581
JavaFX for Space Exploration ................................................................................................... 581
JavaFX for Quantum Computing ............................................................................................... 583
Using JShell .......................................................................................................................... 586
Using JShell ......................................................................................................................... 588
About ND4J .......................................................................................................................... 591
Using ND4J in JShell ........................................................................................................... 595
Using JavaFX in JShell ........................................................................................................ 600
Conclusion ........................................................................................................................ 611
Index ................................................................................................................................ 613
Build enhanced visual experiences and design and deploy modern, easy-to-maintain, client applications across a variety of platforms. This book will show you how these applications can take advantage of the latest user interface components, 3D technology, and cloud services to create immersive visualizations and allow high-value data manipulation.
The Definitive Guide to Modern Java Clients with JavaFX is a professional reference for building Java applications for desktop, mobile, and embedded in the Cloud age. It offers end-to-end coverage of the latest features in JavaFX and Java 13. After reading this book, you will be equipped to upgrade legacy client applications, develop cross-platform applications in Java, and build enhanced desktop and mobile native clients.
Create modern client applications in Java using the latest JavaFX and Java 13
Build enterprise clients that will enable integration with existing cloud services
Use advanced visualization and 3D features
Deploy on desktop, mobile, and embedded devices
Professional Java developers who are interested in learning the latest client Java development techniques to fill out their skillset.