You are on page 1of 32

Learn JavaFX 17

Building User Experience and


Interfaces with Java

Second Edition

Kishori Sharan
Peter Späth
Learn JavaFX 17: Building User Experience and Interfaces with Java
Kishori Sharan Peter Späth
Montgomery, AL, USA Leipzig, Sachsen, Germany

ISBN-13 (pbk): 978-1-4842-7847-5 ISBN-13 (electronic): 978-1-4842-7848-2


https://doi.org/10.1007/978-1-4842-7848-2

Copyright © 2022 by Kishori Sharan and Peter Späth


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not
identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Steve Anglin
Development Editor: Laura Berendson
Coordinating Editor: Mark Powers
Cover designed by eStudioCalamar
Cover image by Onder Ortel on Unsplash (www.unsplash.com)
Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004,
U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.
springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer
Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail booktranslations@springernature.com; for reprint,
paperback, or audio rights, please e-mail bookpermissions@springernature.com.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales
web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub via the book’s product page, located at www.apress.com/9781484278475. For more
detailed information, please visit http://www.apress.com/source-­code.
Printed on acid-free paper
To my father-in-law Mr. Jim Baker
—Kishori Sharan

To my niece Alina
—Peter Späth
Table of Contents

About the Authors�������������������������������������������������������������������������������������������������xxix


About the Technical Reviewer������������������������������������������������������������������������������xxxi
Acknowledgments����������������������������������������������������������������������������������������������xxxiii
Introduction���������������������������������������������������������������������������������������������������������xxxv


■Chapter 1: Getting Started������������������������������������������������������������������������������������� 1
What Is JavaFX?��������������������������������������������������������������������������������������������������������������� 1
History of JavaFX������������������������������������������������������������������������������������������������������������� 3
System Requirements������������������������������������������������������������������������������������������������������ 4
JavaFX Runtime Library��������������������������������������������������������������������������������������������������� 4
JavaFX Source Code��������������������������������������������������������������������������������������������������������� 5
Your First JavaFX Application������������������������������������������������������������������������������������������� 5
Starting an Eclipse Project��������������������������������������������������������������������������������������������������������������������� 5
Setting Up a Module Info������������������������������������������������������������������������������������������������������������������������ 7
Creating the HelloJavaFX Class�������������������������������������������������������������������������������������������������������������� 8
Overriding the start() Method����������������������������������������������������������������������������������������������������������������� 8
Showing the Stage��������������������������������������������������������������������������������������������������������������������������������� 9
Launching the Application�������������������������������������������������������������������������������������������������������������������� 10
Adding a Scene to the Stage���������������������������������������������������������������������������������������������������������������� 10

Improving the HelloFX Application��������������������������������������������������������������������������������� 12


Passing Parameters to a JavaFX Application����������������������������������������������������������������� 15
Case 1��������������������������������������������������������������������������������������������������������������������������������������������������� 16
Case 2��������������������������������������������������������������������������������������������������������������������������������������������������� 16

v
■ Table of Contents

Launching a JavaFX Application������������������������������������������������������������������������������������� 17


The Life Cycle of a JavaFX Application��������������������������������������������������������������������������� 19
Terminating a JavaFX Application���������������������������������������������������������������������������������� 21
Summary������������������������������������������������������������������������������������������������������������������������ 22

■Chapter 2: Properties and Bindings��������������������������������������������������������������������� 23
What Is a Property?�������������������������������������������������������������������������������������������������������� 23
What Is a Binding?��������������������������������������������������������������������������������������������������������� 25
Understanding Binding Support in JavaBeans��������������������������������������������������������������� 26
Understanding Properties in JavaFX������������������������������������������������������������������������������ 29
Using Properties in JavaFX Beans���������������������������������������������������������������������������������� 32
Understanding the Property Class Hierarchy����������������������������������������������������������������� 36
Handling Property Invalidation Events���������������������������������������������������������������������������� 39
Handling Property Change Events���������������������������������������������������������������������������������� 40
Handling Invalidation and Change Events���������������������������������������������������������������������� 43
Using Bindings in JavaFX����������������������������������������������������������������������������������������������� 44
Unidirectional and Bidirectional Bindings���������������������������������������������������������������������� 47
Understanding the Binding API��������������������������������������������������������������������������������������� 51
The High-Level Binding API������������������������������������������������������������������������������������������������������������������ 51
Using the Low-Level Binding API���������������������������������������������������������������������������������������������������������� 64

Using Bindings to Center a Circle����������������������������������������������������������������������������������� 66


Summary������������������������������������������������������������������������������������������������������������������������ 67

■Chapter 3: Observable Collections���������������������������������������������������������������������� 69
What Are Observable Collections?��������������������������������������������������������������������������������� 69
Understanding ObservableList��������������������������������������������������������������������������������������� 70
Creating an ObservableList������������������������������������������������������������������������������������������������������������������� 72
Observing an ObservableList for Invalidations������������������������������������������������������������������������������������� 74
Observing an ObservableList for Changes�������������������������������������������������������������������������������������������� 75

Understanding ObservableSet���������������������������������������������������������������������������������������� 84
Creating an ObservableSet������������������������������������������������������������������������������������������������������������������� 85

vi
■ Table of Contents

Understanding ObservableMap�������������������������������������������������������������������������������������� 85
Creating an ObservableMap����������������������������������������������������������������������������������������������������������������� 86

Properties and Bindings for JavaFX Collections������������������������������������������������������������� 87


Understanding ObservableList Property and Binding��������������������������������������������������������������������������� 87
Binding to List Properties and Content������������������������������������������������������������������������������������������������� 90
Understanding ObservableSet Property and Binding��������������������������������������������������������������������������� 94
Understanding ObservableMap Property and Binding�������������������������������������������������������������������������� 95
Summary������������������������������������������������������������������������������������������������������������������������ 97

■Chapter 4: Managing Stages������������������������������������������������������������������������������� 99
Knowing the Details of Your Screens����������������������������������������������������������������������������� 99
What Is a Stage?���������������������������������������������������������������������������������������������������������� 101
Showing the Primary Stage������������������������������������������������������������������������������������������ 102
Setting the Bounds of a Stage�������������������������������������������������������������������������������������� 104
Initializing the Style of a Stage������������������������������������������������������������������������������������� 108
Moving an Undecorated Stage������������������������������������������������������������������������������������� 111
Initializing Modality of a Stage������������������������������������������������������������������������������������� 113
Setting the Opacity of a Stage�������������������������������������������������������������������������������������� 116
Resizing a Stage����������������������������������������������������������������������������������������������������������� 116
Showing a Stage in Full-Screen Mode������������������������������������������������������������������������� 117
Showing a Stage and Waiting for It to Close���������������������������������������������������������������� 118
Summary���������������������������������������������������������������������������������������������������������������������� 120

■Chapter 5: Making Scenes��������������������������������������������������������������������������������� 123
What Is a Scene?���������������������������������������������������������������������������������������������������������� 123
Graphics Rendering Modes������������������������������������������������������������������������������������������ 125
Setting the Cursor for a Scene������������������������������������������������������������������������������������� 126
The Focus Owner in a Scene���������������������������������������������������������������������������������������� 127
Understanding the Platform Class�������������������������������������������������������������������������������� 128
Knowing the Host Environment������������������������������������������������������������������������������������ 130
Summary���������������������������������������������������������������������������������������������������������������������� 132

vii
■ Table of Contents


■Chapter 6: Understanding Nodes����������������������������������������������������������������������� 133
What Is a Node?����������������������������������������������������������������������������������������������������������� 133
The Cartesian Coordinate System�������������������������������������������������������������������������������� 134
Cartesian Coordinate System of a Node����������������������������������������������������������������������� 135
The Concept of Bounds and Bounding Box������������������������������������������������������������������ 136
Knowing the Bounds of a Node������������������������������������������������������������������������������������ 138
The layoutBounds Property���������������������������������������������������������������������������������������������������������������� 140
The boundsInLocal Property��������������������������������������������������������������������������������������������������������������� 143
The boundsInParent Property������������������������������������������������������������������������������������������������������������� 144
Bounds of a Group�������������������������������������������������������������������������������������������������������� 144
A Detailed Example on Bounds������������������������������������������������������������������������������������ 144
Positioning a Node Using layoutX and layoutY������������������������������������������������������������� 150
Setting the Size of a Node�������������������������������������������������������������������������������������������� 151
Resizable Nodes��������������������������������������������������������������������������������������������������������������������������������� 153
Nonresizable Nodes���������������������������������������������������������������������������������������������������������������������������� 161

Storing User Data in a Node����������������������������������������������������������������������������������������� 161


What Is a Managed Node?������������������������������������������������������������������������������������������� 162
Transforming Bounds Between Coordinate Spaces����������������������������������������������������� 166
Summary���������������������������������������������������������������������������������������������������������������������� 170

■Chapter 7: Playing with Colors�������������������������������������������������������������������������� 171
Understanding Colors��������������������������������������������������������������������������������������������������� 172
Using the Color Class�������������������������������������������������������������������������������������������������������������������������� 173
Using the ImagePattern Class������������������������������������������������������������������������������������������������������������ 174

Understanding Linear Color Gradient��������������������������������������������������������������������������� 177


Using the LinearGradient Class���������������������������������������������������������������������������������������������������������� 178
Defining Linear Color Gradients Using a String Format���������������������������������������������������������������������� 183

Understanding Radial Color Gradient��������������������������������������������������������������������������� 185


Using the RadialGradient Class���������������������������������������������������������������������������������������������������������� 186
Defining Radial Color Gradients in String Format������������������������������������������������������������������������������� 192

Summary���������������������������������������������������������������������������������������������������������������������� 193
viii
■ Table of Contents


■Chapter 8: Styling Nodes����������������������������������������������������������������������������������� 195
What Is a Cascading Style Sheet?�������������������������������������������������������������������������������� 195
What Are Styles, Skins, and Themes?�������������������������������������������������������������������������� 196
A Quick Example���������������������������������������������������������������������������������������������������������� 196
Naming Conventions in JavaFX CSS���������������������������������������������������������������������������� 198
Adding Style Sheets����������������������������������������������������������������������������������������������������� 198
Default Style Sheet������������������������������������������������������������������������������������������������������� 199
Adding Inline Styles������������������������������������������������������������������������������������������������������ 199
Priorities of Styles for a Node��������������������������������������������������������������������������������������� 201
Inheriting CSS Properties��������������������������������������������������������������������������������������������� 204
Types of CSS Properties����������������������������������������������������������������������������������������������� 206
The inherit Type���������������������������������������������������������������������������������������������������������������������������������� 207
The boolean Type�������������������������������������������������������������������������������������������������������������������������������� 207
The string Type����������������������������������������������������������������������������������������������������������������������������������� 207
The number and integer Types����������������������������������������������������������������������������������������������������������� 208
The size Type�������������������������������������������������������������������������������������������������������������������������������������� 208
The length and percentage Types������������������������������������������������������������������������������������������������������� 208
The angle Type������������������������������������������������������������������������������������������������������������������������������������ 208
The duration Type������������������������������������������������������������������������������������������������������������������������������� 209
The point Type������������������������������������������������������������������������������������������������������������������������������������ 209
The color-stop Type���������������������������������������������������������������������������������������������������������������������������� 209
The URI Type��������������������������������������������������������������������������������������������������������������������������������������� 209
The effect Type����������������������������������������������������������������������������������������������������������������������������������� 209
The font Type�������������������������������������������������������������������������������������������������������������������������������������� 210
The paint and color Types������������������������������������������������������������������������������������������������������������������� 211

Specifying Background Colors������������������������������������������������������������������������������������� 213


Specifying Borders������������������������������������������������������������������������������������������������������� 215
Border Colors�������������������������������������������������������������������������������������������������������������������������������������� 215
Border Widths������������������������������������������������������������������������������������������������������������������������������������� 216
Border Radii���������������������������������������������������������������������������������������������������������������������������������������� 216

ix
■ Table of Contents

Border Insets�������������������������������������������������������������������������������������������������������������������������������������� 216


Border Styles�������������������������������������������������������������������������������������������������������������������������������������� 217

Understanding Style Selectors������������������������������������������������������������������������������������� 219


Using Class Selectors������������������������������������������������������������������������������������������������������������������������� 219
Class Selector for the root Node��������������������������������������������������������������������������������������������������������� 222
Using ID Selectors������������������������������������������������������������������������������������������������������������������������������ 224
Combining ID and Class Selectors������������������������������������������������������������������������������������������������������ 225
The Universal Selector������������������������������������������������������������������������������������������������������������������������ 226
Grouping Multiple Selectors��������������������������������������������������������������������������������������������������������������� 226
Descendant Selectors������������������������������������������������������������������������������������������������������������������������� 227
Child Selectors������������������������������������������������������������������������������������������������������������������������������������ 227
State-Based Selectors������������������������������������������������������������������������������������������������������������������������ 228
Using JavaFX Class Names As Selectors�������������������������������������������������������������������������������������������� 229

Looking Up Nodes in a Scene Graph���������������������������������������������������������������������������� 229


Summary���������������������������������������������������������������������������������������������������������������������� 230

■Chapter 9: Event Handling��������������������������������������������������������������������������������� 231
What Is an Event?��������������������������������������������������������������������������������������������������������� 231
Event Class Hierarchy��������������������������������������������������������������������������������������������������� 232
Event Targets���������������������������������������������������������������������������������������������������������������� 233
Event Types������������������������������������������������������������������������������������������������������������������ 234
Event Processing Mechanism�������������������������������������������������������������������������������������� 235
Event Target Selection������������������������������������������������������������������������������������������������������������������������ 235
Event Route Construction������������������������������������������������������������������������������������������������������������������� 236
Event Route Traversal������������������������������������������������������������������������������������������������������������������������� 236

Handling Events����������������������������������������������������������������������������������������������������������� 239


Creating Event Filters and Handlers��������������������������������������������������������������������������������������������������� 239
Registering Event Filters and Handlers���������������������������������������������������������������������������������������������� 239

Execution Order of Event Filters and Handlers������������������������������������������������������������� 245


Consuming Events�������������������������������������������������������������������������������������������������������� 248
Handling Input Events�������������������������������������������������������������������������������������������������� 251

x
■ Table of Contents

Handling Mouse Events������������������������������������������������������������������������������������������������ 252


Getting Mouse Location���������������������������������������������������������������������������������������������������������������������� 253
Representing Mouse Buttons������������������������������������������������������������������������������������������������������������� 255
State of Mouse Buttons���������������������������������������������������������������������������������������������������������������������� 255
Hysteresis in GUI Applications������������������������������������������������������������������������������������������������������������ 256
State of Modifier Keys������������������������������������������������������������������������������������������������������������������������ 257
Picking Mouse Events on Bounds������������������������������������������������������������������������������������������������������� 257
Mouse Transparency��������������������������������������������������������������������������������������������������������������������������� 260
Synthesized Mouse Events����������������������������������������������������������������������������������������������������������������� 261
Handling Mouse-Entered and Mouse-Exited Events�������������������������������������������������������������������������� 262
Handling Key Events����������������������������������������������������������������������������������������������������� 267
Handling Key-Pressed and Key-Released Events������������������������������������������������������������������������������� 268
Handling the Key-Typed Event������������������������������������������������������������������������������������������������������������ 270

Handling Window Events���������������������������������������������������������������������������������������������� 271


Summary���������������������������������������������������������������������������������������������������������������������� 274

■Chapter 10: Understanding Layout Panes���������������������������������������������������������� 277
What Is a Layout Pane?������������������������������������������������������������������������������������������������ 277
Layout Pane Classes���������������������������������������������������������������������������������������������������� 278
Adding Children to a Layout Pane�������������������������������������������������������������������������������� 280
Utility Classes and Enums�������������������������������������������������������������������������������������������� 281
The Insets Class���������������������������������������������������������������������������������������������������������������������������������� 281
The HPos Enum���������������������������������������������������������������������������������������������������������������������������������� 282
The VPos Enum����������������������������������������������������������������������������������������������������������������������������������� 282
The Pos Enum������������������������������������������������������������������������������������������������������������������������������������� 283
The HorizontalDirection Enum������������������������������������������������������������������������������������������������������������ 283
The VerticalDirection Enum���������������������������������������������������������������������������������������������������������������� 283
The Orientation Enum������������������������������������������������������������������������������������������������������������������������� 283
The Side Enum������������������������������������������������������������������������������������������������������������������������������������ 283
The Priority Enum������������������������������������������������������������������������������������������������������������������������������� 283

xi
■ Table of Contents

Understanding Group��������������������������������������������������������������������������������������������������� 283


Creating a Group Object��������������������������������������������������������������������������������������������������������������������� 284
Rendering Nodes in a Group��������������������������������������������������������������������������������������������������������������� 284
Positioning Nodes in a Group������������������������������������������������������������������������������������������������������������� 285
Applying Effects and Transformations to a Group������������������������������������������������������������������������������� 286
Styling a Group with CSS�������������������������������������������������������������������������������������������������������������������� 286
Understanding Region�������������������������������������������������������������������������������������������������� 286
Setting Backgrounds�������������������������������������������������������������������������������������������������������������������������� 288
Setting Padding���������������������������������������������������������������������������������������������������������������������������������� 290
Setting Borders����������������������������������������������������������������������������������������������������������������������������������� 290
Setting Margins���������������������������������������������������������������������������������������������������������������������������������� 299
Understanding Panes��������������������������������������������������������������������������������������������������� 299
Understanding HBox����������������������������������������������������������������������������������������������������� 300
Creating HBox Objects������������������������������������������������������������������������������������������������������������������������ 300
HBox Properties���������������������������������������������������������������������������������������������������������������������������������� 301
Setting Constraints for Children in HBox�������������������������������������������������������������������������������������������� 303
Understanding VBox����������������������������������������������������������������������������������������������������� 305
Creating VBox Objects������������������������������������������������������������������������������������������������������������������������ 305
VBox Properties���������������������������������������������������������������������������������������������������������������������������������� 306
Setting Constraints for Children in VBox��������������������������������������������������������������������������������������������� 308
Understanding FlowPane��������������������������������������������������������������������������������������������� 310
Creating FlowPane Objects����������������������������������������������������������������������������������������������������������������� 311
FlowPane Properties��������������������������������������������������������������������������������������������������������������������������� 311
Content Bias of a FlowPane���������������������������������������������������������������������������������������������������������������� 314
Understanding BorderPane������������������������������������������������������������������������������������������ 314
Creating BorderPane Objects������������������������������������������������������������������������������������������������������������� 316
BorderPane Properties����������������������������������������������������������������������������������������������������������������������� 317
Setting Constraints for Children in BorderPane���������������������������������������������������������������������������������� 317
Understanding StackPane�������������������������������������������������������������������������������������������� 318
Creating StackPane Objects��������������������������������������������������������������������������������������������������������������� 319
StackPane Properties������������������������������������������������������������������������������������������������������������������������� 320
Setting Constraints for Children��������������������������������������������������������������������������������������������������������� 321

xii
■ Table of Contents

Understanding TilePane����������������������������������������������������������������������������������������������� 322


Creating TilePane Objects������������������������������������������������������������������������������������������������������������������� 324
TilePane Properties����������������������������������������������������������������������������������������������������������������������������� 324
Setting Constraints for Children in TilePane��������������������������������������������������������������������������������������� 327

Understanding GridPane���������������������������������������������������������������������������������������������� 329


Creating GridPane Objects������������������������������������������������������������������������������������������������������������������ 330
Making Grid Lines Visible������������������������������������������������������������������������������������������������������������������� 330
Adding Children to GridPane��������������������������������������������������������������������������������������������������������������� 330
Creating Forms Using GridPanes�������������������������������������������������������������������������������������������������������� 334
GridPane Properties���������������������������������������������������������������������������������������������������������������������������� 337
The hgap and vgap Properties������������������������������������������������������������������������������������������������������������ 338
Customizing Columns and Rows�������������������������������������������������������������������������������������������������������� 339
Setting Constraints on Children in GridPane��������������������������������������������������������������������������������������� 343
Understanding AnchorPane������������������������������������������������������������������������������������������ 346
Creating AnchorPane Objects������������������������������������������������������������������������������������������������������������� 346
Setting Constraints for Children in AnchorPane��������������������������������������������������������������������������������� 348

Understanding TextFlow����������������������������������������������������������������������������������������������� 350


Creating TextFlow Objects������������������������������������������������������������������������������������������������������������������ 350
TextFlow Properties���������������������������������������������������������������������������������������������������������������������������� 352
Setting Constraints for Children in TextFlow��������������������������������������������������������������������������������������� 353

Snapping to Pixel��������������������������������������������������������������������������������������������������������� 353


Summary���������������������������������������������������������������������������������������������������������������������� 354

■Chapter 11: Model-View-Controller Pattern������������������������������������������������������ 357
What Is the Model-View-Controller Pattern?���������������������������������������������������������������� 357
A Model-View-Presenter Example�������������������������������������������������������������������������������� 360
The Requirements������������������������������������������������������������������������������������������������������������������������������ 360
The Design������������������������������������������������������������������������������������������������������������������������������������������ 361
The Implementation���������������������������������������������������������������������������������������������������������������������������� 361
Putting Them Together������������������������������������������������������������������������������������������������������������������������ 363

Summary���������������������������������������������������������������������������������������������������������������������� 363

xiii
■ Table of Contents


■Chapter 12: Understanding Controls����������������������������������������������������������������� 365
What Is a Control?�������������������������������������������������������������������������������������������������������� 365
Understanding the Control Class Hierarchy������������������������������������������������������������������ 366
Labeled Controls���������������������������������������������������������������������������������������������������������� 368
Positioning Graphic and Text�������������������������������������������������������������������������������������������������������������� 370
Understanding Mnemonics and Accelerators������������������������������������������������������������������������������������� 370

Understanding the Label Control���������������������������������������������������������������������������������� 373


Understanding Buttons������������������������������������������������������������������������������������������������� 374
Understanding Command Buttons������������������������������������������������������������������������������� 375
Understanding the Button Control������������������������������������������������������������������������������������������������������ 375
Understanding the Hyperlink Control�������������������������������������������������������������������������������������������������� 376
Understanding the MenuButton Control��������������������������������������������������������������������������������������������� 377

Understanding Choice Buttons������������������������������������������������������������������������������������� 378


Understanding the ToggleButton Control�������������������������������������������������������������������������������������������� 379
Understanding the RadioButton Control��������������������������������������������������������������������������������������������� 381
Understanding the CheckBox Control������������������������������������������������������������������������������������������������� 382

Understanding the Hybrid Button Control��������������������������������������������������������������������� 384


Making Selections from a List of Items������������������������������������������������������������������������ 385
Understanding the ChoiceBox Control�������������������������������������������������������������������������� 386
Using Domain Objects in ChoiceBox��������������������������������������������������������������������������������������������������� 389
Allowing Nulls in ChoiceBox��������������������������������������������������������������������������������������������������������������� 391
Using Separators in ChoiceBox���������������������������������������������������������������������������������������������������������� 393
Styling a ChoiceBox with CSS������������������������������������������������������������������������������������������������������������ 394

Understanding the ComboBox Control������������������������������������������������������������������������� 394


Detecting Value Change in ComboBox������������������������������������������������������������������������������������������������ 396
Using Domain Objects in Editable ComboBox������������������������������������������������������������������������������������� 396
Customizing the Height of a Pop-Up List�������������������������������������������������������������������������������������������� 397
Using Nodes As Items in ComboBox��������������������������������������������������������������������������������������������������� 397
Using a Cell Factory in ComboBox������������������������������������������������������������������������������������������������������ 398
Styling ComboBox with CSS��������������������������������������������������������������������������������������������������������������� 402

xiv
■ Table of Contents

Understanding the ListView Control����������������������������������������������������������������������������� 403


Orientation of a ListView�������������������������������������������������������������������������������������������������������������������� 405
Selection Model in ListView���������������������������������������������������������������������������������������������������������������� 405
Using Cell Factory in ListView������������������������������������������������������������������������������������������������������������ 407
Using Editable ListView���������������������������������������������������������������������������������������������������������������������� 408
Handling Events While Editing a ListView������������������������������������������������������������������������������������������� 411
Styling ListView with CSS������������������������������������������������������������������������������������������������������������������� 412
Understanding the ColorPicker Control������������������������������������������������������������������������ 413
Using the ColorPicker Control������������������������������������������������������������������������������������������������������������� 415
Styling ColorPicker with CSS�������������������������������������������������������������������������������������������������������������� 417

Understanding the DatePicker Control������������������������������������������������������������������������� 417


Using the DatePicker Control�������������������������������������������������������������������������������������������������������������� 418
Styling DatePicker with CSS��������������������������������������������������������������������������������������������������������������� 422

Understanding Text Input Controls������������������������������������������������������������������������������� 422


Positioning and Moving Caret������������������������������������������������������������������������������������������������������������� 423
Making Text Selection������������������������������������������������������������������������������������������������������������������������� 424
Modifying the Content������������������������������������������������������������������������������������������������������������������������ 425
Cutting, Copying, and Pasting Text������������������������������������������������������������������������������������������������������ 425
An Example����������������������������������������������������������������������������������������������������������������������������������������� 425
Styling TextInputControl with CSS������������������������������������������������������������������������������������������������������ 426

Understanding the TextField Control���������������������������������������������������������������������������� 426


Styling TextField with CSS������������������������������������������������������������������������������������������������������������������ 428

Understanding the PasswordField Control������������������������������������������������������������������� 429


Understanding the TextArea Control����������������������������������������������������������������������������� 429
Styling TextArea with CSS������������������������������������������������������������������������������������������������������������������ 431

Showing the Progress of a Task����������������������������������������������������������������������������������� 432


Styling ProgressIndicator with CSS���������������������������������������������������������������������������������������������������� 433
Styling ProgressIndicator and ProgressBar with CSS������������������������������������������������������������������������ 434

Understanding the TitledPane Control�������������������������������������������������������������������������� 435


Styling TitledPane with CSS��������������������������������������������������������������������������������������������������������������� 437

xv
■ Table of Contents

Understanding the Accordion Control�������������������������������������������������������������������������� 438


Styling Accordion with CSS���������������������������������������������������������������������������������������������������������������� 440

Understanding the Pagination Control�������������������������������������������������������������������������� 440


Styling Pagination with CSS��������������������������������������������������������������������������������������������������������������� 443

Understanding the Tool Tip Control������������������������������������������������������������������������������� 444


Styling Tooltip with CSS���������������������������������������������������������������������������������������������������������������������� 447

Providing Scrolling Features in Controls���������������������������������������������������������������������� 447


Understanding the ScrollBar Control�������������������������������������������������������������������������������������������������� 447
Understanding the ScrollPane Control������������������������������������������������������������������������������������������������ 448

Keeping Things Separate��������������������������������������������������������������������������������������������� 451


Understanding the Separator Control������������������������������������������������������������������������������������������������� 451
Styling Separator with CSS���������������������������������������������������������������������������������������������������������������� 452
Understanding the SplitPane Control�������������������������������������������������������������������������������������������������� 453
Styling SplitPane with CSS����������������������������������������������������������������������������������������������������������������� 454

Understanding the Slider Control��������������������������������������������������������������������������������� 455


Styling Slider with CSS����������������������������������������������������������������������������������������������������������������������� 458

Understanding Menus�������������������������������������������������������������������������������������������������� 460


Using Menu Bars�������������������������������������������������������������������������������������������������������������������������������� 462
Using Menus��������������������������������������������������������������������������������������������������������������������������������������� 462
Using Menu Items������������������������������������������������������������������������������������������������������������������������������� 465
Putting All Parts of Menus Together���������������������������������������������������������������������������������������������������� 471
Styling Menus Using CSS������������������������������������������������������������������������������������������������������������������� 472

Understanding the ContextMenu Control��������������������������������������������������������������������� 472


Styling ContextMenu with CSS����������������������������������������������������������������������������������������������������������� 475

Understanding the ToolBar Control������������������������������������������������������������������������������� 475


Styling a Toolbar with CSS������������������������������������������������������������������������������������������������������������������ 476

Understanding TabPane and Tab���������������������������������������������������������������������������������� 477


Creating Tabs�������������������������������������������������������������������������������������������������������������������������������������� 478
Setting the Title and Content of Tabs�������������������������������������������������������������������������������������������������� 478
Creating TabPanes������������������������������������������������������������������������������������������������������������������������������ 479
Adding Tabs to a TabPane������������������������������������������������������������������������������������������������������������������� 479

xvi
■ Table of Contents

Putting TabPanes and Tabs Together�������������������������������������������������������������������������������������������������� 480


Understanding Tab Selection�������������������������������������������������������������������������������������������������������������� 481
Closing Tabs in a TabPane������������������������������������������������������������������������������������������������������������������ 482
Positioning Tabs in a TabPane������������������������������������������������������������������������������������������������������������ 484
Sizing Tabs in a TabPane�������������������������������������������������������������������������������������������������������������������� 485
Using Recessed and Floating TabPanes��������������������������������������������������������������������������������������������� 485
Styling Tab and TabPane with CSS����������������������������������������������������������������������������������������������������� 486

Understanding the HTMLEditor Control������������������������������������������������������������������������ 488


Creating an HTMLEditor���������������������������������������������������������������������������������������������������������������������� 489
Using an HTMLEditor�������������������������������������������������������������������������������������������������������������������������� 489
Styling HTMLEditor with CSS�������������������������������������������������������������������������������������������������������������� 489

Choosing Files and Directories������������������������������������������������������������������������������������� 490


The FileChooser Dialog����������������������������������������������������������������������������������������������������������������������� 490
The DirectoryChooser Dialog�������������������������������������������������������������������������������������������������������������� 493

Summary���������������������������������������������������������������������������������������������������������������������� 494

■Chapter 13: Understanding TableView��������������������������������������������������������������� 497
What Is a TableView?��������������������������������������������������������������������������������������������������� 497
Creating a TableView���������������������������������������������������������������������������������������������������� 500
Adding Columns to a TableView��������������������������������������������������������������������������������������������������������� 501
Customizing TableView Placeholder��������������������������������������������������������������������������������������������������� 504
Populating a TableColumn with Data�������������������������������������������������������������������������������������������������� 505
Using a Map As Items in a TableView��������������������������������������������������������������������������� 508
Showing and Hiding Columns������������������������������������������������������������������������������������������������������������� 508
Reordering Columns in a TableView��������������������������������������������������������������������������������������������������� 509

Sorting Data in a TableView������������������������������������������������������������������������������������������ 510


Sorting Data by Users������������������������������������������������������������������������������������������������������������������������� 510
Sorting Data Programmatically����������������������������������������������������������������������������������������������������������� 511

Customizing Data Rendering in Cells��������������������������������������������������������������������������� 514


Selecting Cells and Rows in a TableView��������������������������������������������������������������������� 517

xvii
■ Table of Contents

Editing Data in a TableView������������������������������������������������������������������������������������������ 518


Editing Data Using a Check Box��������������������������������������������������������������������������������������������������������� 519
Editing Data Using a Choice Box��������������������������������������������������������������������������������������������������������� 520
Editing Data Using a Combo Box�������������������������������������������������������������������������������������������������������� 521
Editing Data Using a TextField������������������������������������������������������������������������������������������������������������ 521
Editing Data in TableCell Using Any Control���������������������������������������������������������������������������������������� 522

Adding and Deleting Rows in a TableView������������������������������������������������������������������� 523


Scrolling in a TableView����������������������������������������������������������������������������������������������� 524
Resizing a TableColumn����������������������������������������������������������������������������������������������� 525
Styling a TableView with CSS��������������������������������������������������������������������������������������� 526
Summary���������������������������������������������������������������������������������������������������������������������� 527

■Chapter 14: Understanding 2D Shapes�������������������������������������������������������������� 529
What Are 2D Shapes?��������������������������������������������������������������������������������������������������� 529
Drawing 2D Shapes������������������������������������������������������������������������������������������������������ 531
Drawing Lines������������������������������������������������������������������������������������������������������������������������������������� 532
Drawing Rectangles��������������������������������������������������������������������������������������������������������������������������� 533
Drawing Circles���������������������������������������������������������������������������������������������������������������������������������� 535
Drawing Ellipses��������������������������������������������������������������������������������������������������������������������������������� 537
Drawing Polygons������������������������������������������������������������������������������������������������������������������������������� 539
Drawing Polylines������������������������������������������������������������������������������������������������������������������������������� 541
Drawing Arcs�������������������������������������������������������������������������������������������������������������������������������������� 543
Drawing Quadratic Curves������������������������������������������������������������������������������������������������������������������ 546
Drawing Cubic Curves������������������������������������������������������������������������������������������������������������������������ 548

Building Complex Shapes Using the Path Class����������������������������������������������������������� 550


The MoveTo Path Element������������������������������������������������������������������������������������������������������������������ 551
The LineTo Path Element�������������������������������������������������������������������������������������������������������������������� 551
The HLineTo and VLineTo Path Elements�������������������������������������������������������������������������������������������� 554
The ArcTo Path Element���������������������������������������������������������������������������������������������������������������������� 555
The QuadCurveTo Path Element��������������������������������������������������������������������������������������������������������� 558
The CubicCurveTo Path Element��������������������������������������������������������������������������������������������������������� 559

xviii
■ Table of Contents

The ClosePath Path Element��������������������������������������������������������������������������������������������������������������� 560


The Fill Rule for a Path����������������������������������������������������������������������������������������������������������������������� 560

Drawing Scalable Vector Graphics������������������������������������������������������������������������������� 563


The “moveTo” Command�������������������������������������������������������������������������������������������������������������������� 566
The “lineto” Commands���������������������������������������������������������������������������������������������������������������������� 567
The “arcto” Command������������������������������������������������������������������������������������������������������������������������ 567
The “Quadratic Bezier curveto” Command����������������������������������������������������������������������������������������� 568
The “Cubic Bezier curveto” Command����������������������������������������������������������������������������������������������� 569
The “closepath” Command����������������������������������������������������������������������������������������������������������������� 569

Combining Shapes������������������������������������������������������������������������������������������������������� 570


Understanding the Stroke of a Shape�������������������������������������������������������������������������� 571
Styling Shapes with CSS���������������������������������������������������������������������������������������������� 576
Summary���������������������������������������������������������������������������������������������������������������������� 577

■Chapter 15: Understanding Text Nodes������������������������������������������������������������� 579
What Is a Text Node?���������������������������������������������������������������������������������������������������� 579
Creating a Text Node���������������������������������������������������������������������������������������������������� 580
Understanding the Text Origin�������������������������������������������������������������������������������������� 581
Displaying Multiline Text����������������������������������������������������������������������������������������������� 584
Setting Text Fonts��������������������������������������������������������������������������������������������������������� 585
Creating Fonts������������������������������������������������������������������������������������������������������������������������������������ 586
Accessing Installed Fonts������������������������������������������������������������������������������������������������������������������� 588
Using Custom Fonts���������������������������������������������������������������������������������������������������������������������������� 590
Setting Text Fill and Stroke������������������������������������������������������������������������������������������� 592
Applying Text Decorations�������������������������������������������������������������������������������������������� 593
Applying Font Smoothing��������������������������������������������������������������������������������������������� 594
Styling a Text Node with CSS��������������������������������������������������������������������������������������� 594
Summary���������������������������������������������������������������������������������������������������������������������� 595

xix
■ Table of Contents


■Chapter 16: Understanding 3D Shapes�������������������������������������������������������������� 597
What Are 3D Shapes?��������������������������������������������������������������������������������������������������� 597
Checking Support for 3D���������������������������������������������������������������������������������������������� 598
The 3D Coordinate System������������������������������������������������������������������������������������������� 599
Rendering Order of Nodes�������������������������������������������������������������������������������������������� 599
Using Predefined 3D Shapes���������������������������������������������������������������������������������������� 603
Specifying the Shape Material������������������������������������������������������������������������������������������������������������ 606
Specifying the Draw Mode of Shapes������������������������������������������������������������������������������������������������� 609
Specifying the Face Culling for Shapes���������������������������������������������������������������������������������������������� 611
Using Cameras������������������������������������������������������������������������������������������������������������� 612
Using Light Sources����������������������������������������������������������������������������������������������������� 616
Creating Subscenes����������������������������������������������������������������������������������������������������� 617
Creating User-Defined Shapes������������������������������������������������������������������������������������� 619
Creating a 3D Triangle������������������������������������������������������������������������������������������������������������������������ 624
Creating a 3D Rectangle��������������������������������������������������������������������������������������������������������������������� 628
Creating a Tetrahedron����������������������������������������������������������������������������������������������������������������������� 629

Summary���������������������������������������������������������������������������������������������������������������������� 632

■Chapter 17: Applying Effects����������������������������������������������������������������������������� 635
What Is an Effect?�������������������������������������������������������������������������������������������������������� 635
Chaining Effects����������������������������������������������������������������������������������������������������������� 637
Shadowing Effects������������������������������������������������������������������������������������������������������� 638
The DropShadow Effect���������������������������������������������������������������������������������������������������������������������� 638
The InnerShadow Effect��������������������������������������������������������������������������������������������������������������������� 642
The Shadow Effect������������������������������������������������������������������������������������������������������������������������������ 644

Blurring Effects������������������������������������������������������������������������������������������������������������ 645


The BoxBlur Effect������������������������������������������������������������������������������������������������������������������������������ 645
The GaussianBlur Effect��������������������������������������������������������������������������������������������������������������������� 646
The MotionBlur Effect������������������������������������������������������������������������������������������������������������������������� 647

The Bloom Effect���������������������������������������������������������������������������������������������������������� 648


The Glow Effect������������������������������������������������������������������������������������������������������������ 649
xx
■ Table of Contents

The Reflection Effect���������������������������������������������������������������������������������������������������� 650


The SepiaTone Effect���������������������������������������������������������������������������������������������������� 651
The DisplacementMap Effect��������������������������������������������������������������������������������������� 652
The ColorInput Effect���������������������������������������������������������������������������������������������������� 656
The ColorAdjust Effect�������������������������������������������������������������������������������������������������� 657
The ImageInput Effect�������������������������������������������������������������������������������������������������� 657
The Blend Effect����������������������������������������������������������������������������������������������������������� 658
The Lighting Effect������������������������������������������������������������������������������������������������������� 660
Customizing the Surface Texture�������������������������������������������������������������������������������������������������������� 661
Understanding Reflection Types��������������������������������������������������������������������������������������������������������� 662
Understanding the Light Source��������������������������������������������������������������������������������������������������������� 664

The PerspectiveTransform Effect��������������������������������������������������������������������������������� 669


Summary���������������������������������������������������������������������������������������������������������������������� 670

■Chapter 18: Understanding Transformations����������������������������������������������������� 673
What Is a Transformation?�������������������������������������������������������������������������������������������� 673
The Translation Transformation������������������������������������������������������������������������������������ 675
The Rotation Transformation���������������������������������������������������������������������������������������� 678
The Scale Transformation��������������������������������������������������������������������������������������������� 682
The Shear Transformation�������������������������������������������������������������������������������������������� 686
Applying Multiple Transformations������������������������������������������������������������������������������� 689
Summary���������������������������������������������������������������������������������������������������������������������� 691

■Chapter 19: Understanding Animation�������������������������������������������������������������� 693
What Is Animation?������������������������������������������������������������������������������������������������������ 693
Understanding Animation Classes�������������������������������������������������������������������������������� 694
Understanding Utility Classes��������������������������������������������������������������������������������������� 695
Understanding the Duration Class������������������������������������������������������������������������������������������������������ 695
Understanding the KeyValue Class����������������������������������������������������������������������������������������������������� 697
Understanding the KeyFrame Class���������������������������������������������������������������������������������������������������� 698

Understanding the Timeline Animation������������������������������������������������������������������������ 699

xxi
■ Table of Contents

Controlling an Animation���������������������������������������������������������������������������������������������� 701


Playing an Animation�������������������������������������������������������������������������������������������������������������������������� 701
Delaying the Start of an Animation����������������������������������������������������������������������������������������������������� 702
Stopping an Animation����������������������������������������������������������������������������������������������������������������������� 702
Pausing an Animation������������������������������������������������������������������������������������������������������������������������� 702
Knowing the State of an Animation���������������������������������������������������������������������������������������������������� 702
Looping an Animation������������������������������������������������������������������������������������������������������������������������� 703
Auto Reversing an Animation������������������������������������������������������������������������������������������������������������� 703
Attaching an onFinished Action���������������������������������������������������������������������������������������������������������� 704
Knowing the Duration of an Animation����������������������������������������������������������������������������������������������� 704
Adjusting the Speed of an Animation������������������������������������������������������������������������������������������������� 704
Understanding Cue Points�������������������������������������������������������������������������������������������� 705
Understanding Transitions�������������������������������������������������������������������������������������������� 706
Understanding the Fade Transition����������������������������������������������������������������������������������������������������� 706
Understanding the Fill Transition�������������������������������������������������������������������������������������������������������� 708
Understanding the Stroke Transition�������������������������������������������������������������������������������������������������� 709
Understanding the Translate Transition���������������������������������������������������������������������������������������������� 709
Understanding the Rotate Transition�������������������������������������������������������������������������������������������������� 710
Understanding the Scale Transition���������������������������������������������������������������������������������������������������� 711
Understanding the Path Transition������������������������������������������������������������������������������������������������������ 712
Understanding the Pause Transition��������������������������������������������������������������������������������������������������� 713
Understanding the Sequential Transition�������������������������������������������������������������������������������������������� 713
Understanding the Parallel Transition������������������������������������������������������������������������������������������������� 714
Understanding Interpolators����������������������������������������������������������������������������������������� 715
Understanding the Linear Interpolator������������������������������������������������������������������������������������������������ 716
Understanding the Discrete Interpolator�������������������������������������������������������������������������������������������� 716
Understanding the Ease-In Interpolator���������������������������������������������������������������������������������������������� 716
Understanding the Ease-Out Interpolator������������������������������������������������������������������������������������������� 716
Understanding the Ease-Both Interpolator����������������������������������������������������������������������������������������� 716
Understanding the Spline Interpolator������������������������������������������������������������������������������������������������ 716
Understanding the Tangent Interpolator��������������������������������������������������������������������������������������������� 717
Summary���������������������������������������������������������������������������������������������������������������������� 717
xxii
■ Table of Contents


■Chapter 20: Understanding Charts�������������������������������������������������������������������� 719
What Is a Chart?����������������������������������������������������������������������������������������������������������� 719
Understanding the Chart API���������������������������������������������������������������������������������������� 719
Styling Charts with CSS����������������������������������������������������������������������������������������������� 721
Data Used in Chart Examples��������������������������������������������������������������������������������������� 722
Understanding the PieChart����������������������������������������������������������������������������������������� 722
Customizing Pie Slices����������������������������������������������������������������������������������������������������������������������� 726
Styling the PieChart with CSS������������������������������������������������������������������������������������������������������������� 726
Understanding the XYChart������������������������������������������������������������������������������������������ 730
Representing Axes in an XYChart������������������������������������������������������������������������������������������������������� 730
Adding Data to an XYChart����������������������������������������������������������������������������������������������������������������� 731
Understanding the BarChart����������������������������������������������������������������������������������������� 733
Styling the BarChart with CSS������������������������������������������������������������������������������������������������������������ 736
Understanding the StackedBarChart���������������������������������������������������������������������������� 737
Styling the StackedBarChart with CSS����������������������������������������������������������������������������������������������� 738
Understanding the ScatterChart����������������������������������������������������������������������������������� 739
Styling the ScatterChart with CSS������������������������������������������������������������������������������������������������������ 741
Understanding the LineChart���������������������������������������������������������������������������������������� 742
Styling the LineChart with CSS����������������������������������������������������������������������������������������������������������� 743
Understanding the BubbleChart����������������������������������������������������������������������������������� 744
Styling the BubbleChart with CSS������������������������������������������������������������������������������������������������������ 745
Understanding the AreaChart��������������������������������������������������������������������������������������� 746
Styling the AreaChart with CSS���������������������������������������������������������������������������������������������������������� 747
Understanding the StackedAreaChart�������������������������������������������������������������������������� 748
Styling the StackedAreaChart with CSS��������������������������������������������������������������������������������������������� 749
Customizing XYChart Appearance�������������������������������������������������������������������������������� 750
Setting Alternate Row/Column Fill������������������������������������������������������������������������������������������������������ 750
Showing Zero Line Axes��������������������������������������������������������������������������������������������������������������������� 751
Showing Grid Lines����������������������������������������������������������������������������������������������������������������������������� 751
Formatting Numeric Tick Labels��������������������������������������������������������������������������������������������������������� 751
Summary���������������������������������������������������������������������������������������������������������������������� 753
xxiii
■ Table of Contents


■Chapter 21: Understanding the Image API�������������������������������������������������������� 755
What Is the Image API?������������������������������������������������������������������������������������������������ 755
Loading an Image��������������������������������������������������������������������������������������������������������� 756
Specifying the Image-Loading Properties������������������������������������������������������������������������������������������ 757
Reading the Loaded Image Properties����������������������������������������������������������������������������������������������� 758
Viewing an Image��������������������������������������������������������������������������������������������������������� 759
Multiple Views of an Image���������������������������������������������������������������������������������������������������������������� 761
Viewing an Image in a Viewport��������������������������������������������������������������������������������������������������������� 762
Understanding Image Operations��������������������������������������������������������������������������������� 764
Pixel Formats�������������������������������������������������������������������������������������������������������������������������������������� 764
Reading Pixels from an Image������������������������������������������������������������������������������������������������������������ 766
Writing Pixels to an Image������������������������������������������������������������������������������������������������������������������ 768
Creating an Image from Scratch��������������������������������������������������������������������������������������������������������� 771
Saving a New Image to a File System������������������������������������������������������������������������������������������������ 773
Taking the Snapshot of a Node and a Scene���������������������������������������������������������������� 774
Taking the Snapshot of a Node����������������������������������������������������������������������������������������������������������� 774
Taking the Snapshot of a Scene��������������������������������������������������������������������������������������������������������� 776
Summary���������������������������������������������������������������������������������������������������������������������� 776

■Chapter 22: Drawing on a Canvas��������������������������������������������������������������������� 779
What Is the Canvas API?����������������������������������������������������������������������������������������������� 779
Creating a Canvas�������������������������������������������������������������������������������������������������������� 780
Drawing on the Canvas������������������������������������������������������������������������������������������������ 780
Drawing Basic Shapes������������������������������������������������������������������������������������������������������������������������ 780
Drawing Text��������������������������������������������������������������������������������������������������������������������������������������� 781
Drawing Paths������������������������������������������������������������������������������������������������������������������������������������ 782
Drawing Images���������������������������������������������������������������������������������������������������������������������������������� 782
Writing Pixels�������������������������������������������������������������������������������������������������������������������������������������� 783
Clearing the Canvas Area��������������������������������������������������������������������������������������������� 783
Saving and Restoring the Drawing States�������������������������������������������������������������������� 784
A Canvas Drawing Example������������������������������������������������������������������������������������������ 785
Summary���������������������������������������������������������������������������������������������������������������������� 788
xxiv
■ Table of Contents


■Chapter 23: Understanding Drag and Drop�������������������������������������������������������� 789
What Is a Press-Drag-Release Gesture?���������������������������������������������������������������������� 789
A Simple Press-Drag-Release Gesture����������������������������������������������������������������������������������������������� 790
A Full Press-Drag-Release Gesture���������������������������������������������������������������������������������������������������� 792
A Drag-and-Drop Gesture������������������������������������������������������������������������������������������������������������������� 793

Understanding the Data Transfer Modes���������������������������������������������������������������������� 794


Understanding the Dragboard�������������������������������������������������������������������������������������� 795
The Example Application���������������������������������������������������������������������������������������������� 798
Initiating the Drag-and-Drop Gesture������������������������������������������������������������������������������������������������� 798
Detecting a Drag Gesture������������������������������������������������������������������������������������������������������������������� 799
Dropping the Source onto the Target�������������������������������������������������������������������������������������������������� 800
Completing the Drag-and-Drop Gesture��������������������������������������������������������������������������������������������� 800

Providing Visual Clues�������������������������������������������������������������������������������������������������� 801


A Complete Drag-and-Drop Example��������������������������������������������������������������������������� 801
Transferring an Image�������������������������������������������������������������������������������������������������� 802
Transferring Custom Data Types����������������������������������������������������������������������������������� 803
Summary���������������������������������������������������������������������������������������������������������������������� 805

■Chapter 24: Understanding Concurrency in JavaFX������������������������������������������ 807
The Need for a Concurrency Framework���������������������������������������������������������������������� 807
Understanding the Concurrency Framework API���������������������������������������������������������� 814
Understanding the Worker<V> Interface��������������������������������������������������������������������� 814
State Transitions for a Worker������������������������������������������������������������������������������������������������������������ 815
Properties of a Worker������������������������������������������������������������������������������������������������������������������������ 815
Utility Classes for Examples��������������������������������������������������������������������������������������������������������������� 816

Using the Task<V> Class��������������������������������������������������������������������������������������������� 819


Creating a Task����������������������������������������������������������������������������������������������������������������������������������� 819
Updating Task Properties�������������������������������������������������������������������������������������������������������������������� 819
Listening to Task Transition Events����������������������������������������������������������������������������������������������������� 820
Cancelling a Task�������������������������������������������������������������������������������������������������������������������������������� 820

xxv
■ Table of Contents

Running a Task����������������������������������������������������������������������������������������������������������������������������������� 821


A Prime Finder Task Example������������������������������������������������������������������������������������������������������������� 821

Using the Service<V> Class����������������������������������������������������������������������������������������� 822


Creating the Service��������������������������������������������������������������������������������������������������������������������������� 823
Updating Service Properties��������������������������������������������������������������������������������������������������������������� 823
Listening to Service Transition Events������������������������������������������������������������������������������������������������ 823
Cancelling the Service������������������������������������������������������������������������������������������������������������������������ 823
Starting the Service���������������������������������������������������������������������������������������������������������������������������� 824
Resetting the Service������������������������������������������������������������������������������������������������������������������������� 824
Restarting the Service������������������������������������������������������������������������������������������������������������������������ 824
The Prime Finder Service Example����������������������������������������������������������������������������������������������������� 824

Using the ScheduledService<V> Class������������������������������������������������������������������������ 825


Creating the ScheduledService���������������������������������������������������������������������������������������������������������� 825
Updating ScheduledService Properties���������������������������������������������������������������������������������������������� 826
Listening to ScheduledService Transition Events������������������������������������������������������������������������������� 827
The Prime Finder ScheduledService Example������������������������������������������������������������������������������������ 827

Summary���������������������������������������������������������������������������������������������������������������������� 831

■Chapter 25: Playing Audios and Videos������������������������������������������������������������� 833
Understanding the Media API��������������������������������������������������������������������������������������� 833
Playing Short Audio Clips��������������������������������������������������������������������������������������������� 834
Playing Media��������������������������������������������������������������������������������������������������������������� 837
Creating a Media Object��������������������������������������������������������������������������������������������������������������������� 837
Creating a MediaPlayer Object����������������������������������������������������������������������������������������������������������� 838
Creating a MediaView Node��������������������������������������������������������������������������������������������������������������� 838
Combining Media, MediaPlayer, and MediaView�������������������������������������������������������������������������������� 839

A Media Player Example����������������������������������������������������������������������������������������������� 839


Handling Playback Errors��������������������������������������������������������������������������������������������� 842
State Transitions of the MediaPlayer���������������������������������������������������������������������������� 843
Repeating Media Playback������������������������������������������������������������������������������������������� 845
Tracking Media Time���������������������������������������������������������������������������������������������������� 846

xxvi
■ Table of Contents

Controlling the Playback Rate�������������������������������������������������������������������������������������� 847


Controlling the Playback Volume���������������������������������������������������������������������������������� 847
Positioning the MediaPlayer����������������������������������������������������������������������������������������� 848
Marking Positions in the Media������������������������������������������������������������������������������������ 848
Showing Media Metadata��������������������������������������������������������������������������������������������� 849
Customizing the MediaView����������������������������������������������������������������������������������������� 849
Developing a Media Player Application������������������������������������������������������������������������ 850
Summary���������������������������������������������������������������������������������������������������������������������� 850

■Chapter 26: Understanding FXML���������������������������������������������������������������������� 851
What Is FXML?�������������������������������������������������������������������������������������������������������������� 851
Editing FXML Documents��������������������������������������������������������������������������������������������� 852
FXML Basics����������������������������������������������������������������������������������������������������������������� 853
Creating the FXML File����������������������������������������������������������������������������������������������������������������������� 854
Adding UI Elements���������������������������������������������������������������������������������������������������������������������������� 854
Importing Java Types in FXML������������������������������������������������������������������������������������������������������������ 855
Setting Properties in FXML����������������������������������������������������������������������������������������������������������������� 856
Specifying FXML Namespace������������������������������������������������������������������������������������������������������������� 858
Assigning an Identifier to an Object���������������������������������������������������������������������������������������������������� 859
Adding Event Handlers����������������������������������������������������������������������������������������������������������������������� 859
Loading FXML Documents������������������������������������������������������������������������������������������������������������������ 859
Using a Controller in FXML����������������������������������������������������������������������������������������������������������������� 861

Creating Objects in FXML��������������������������������������������������������������������������������������������� 866


Using the no-args Constructor����������������������������������������������������������������������������������������������������������� 866
Using the static valueOf() Method������������������������������������������������������������������������������������������������������ 866
Using a Factory Method���������������������������������������������������������������������������������������������������������������������� 867
Using Builders������������������������������������������������������������������������������������������������������������������������������������ 868

Creating Reusable Objects in FXML����������������������������������������������������������������������������� 874


Specifying Locations in Attributes�������������������������������������������������������������������������������� 875
Using Resource Bundles���������������������������������������������������������������������������������������������� 875
Including FXML Files���������������������������������������������������������������������������������������������������� 877
xxvii
■ Table of Contents

Using Constants����������������������������������������������������������������������������������������������������������� 880


Referencing Another Element��������������������������������������������������������������������������������������� 881
Copying Elements��������������������������������������������������������������������������������������������������������� 882
Binding Properties in FXML������������������������������������������������������������������������������������������ 883
Creating Custom Controls�������������������������������������������������������������������������������������������� 883
Summary���������������������������������������������������������������������������������������������������������������������� 886

■Chapter 27: Understanding the Print API���������������������������������������������������������� 887
What Is the Print API?��������������������������������������������������������������������������������������������������� 887
Listing Available Printers���������������������������������������������������������������������������������������������� 888
Getting the Default Printer�������������������������������������������������������������������������������������������� 889
Printing Nodes�������������������������������������������������������������������������������������������������������������� 889
Showing the Page Setup and Print Dialogs������������������������������������������������������������������ 893
Customizing PrinterJob Settings���������������������������������������������������������������������������������� 895
Setting Page Layout����������������������������������������������������������������������������������������������������� 896
Printing a Web Page����������������������������������������������������������������������������������������������������� 898
Summary���������������������������������������������������������������������������������������������������������������������� 898

■Appendix: Additional Controls and Resources�������������������������������������������������� 901
Additional Controls������������������������������������������������������������������������������������������������������� 901
The TreeView Control�������������������������������������������������������������������������������������������������������������������������� 901
The TreeTableView Control����������������������������������������������������������������������������������������������������������������� 903
The WebView Control�������������������������������������������������������������������������������������������������������������������������� 905

Further Reading������������������������������������������������������������������������������������������������������������ 906



■Index ������������������������������������������������������������������������������������������������������������������ 907

xxviii
About the Authors

Kishori Sharan has earned a Master of Science in Computer Information


Systems degree from Troy State University, Alabama. He is a Sun Certified
Java 2 programmer. He has vast experience in providing training to
professional developers in Java, JSP, EJB, and web technology. He has
over ten years of experience in implementing enterprise-level Java
applications.

Peter Späth graduated in 2002 as a physicist and soon afterward became an IT consultant, mainly for
Java-related projects. In 2016, he decided to concentrate on writing books on various aspects, but with a
main focus on software development. With two books about graphics and sound processing, three books on
Android app development, and several books on Java development, the author continues his effort in writing
software development–related literature.

xxix
About the Technical Reviewer

Preethi Vasudev earned an MS in Computer Information Systems and Cyber Security from Auburn


University, Alabama. She is an Oracle-certified Java 8 programmer with more than 15 years of industry
experience in investment banking, healthcare, and other areas. She is interested in Java and related
technologies and enjoys participating in coding competitions.

xxxi
Acknowledgments

My heartfelt thanks are due to my father-in-law, Jim Baker, for displaying extraordinary patience in reading
the initial draft of the first few chapters of the book and his valuable suggestions for improving the material.
I thank my friend Richard Castillo for his hard work in reading the initial draft of the first 12 chapters
of the book and weeding out several mistakes. Richard was instrumental in running all examples and
pointing out errors. I also thank him for allowing me to use a few pictures in this book from his website
www.digitizedchaos.com.
My wife, Ellen, was always patient when I spent long hours at my computer desk working on this book.
I thank her for all her support to me in writing this book. She also deserves my sincere thanks for letting me
sometimes seclude myself on weekends so I could focus on this book.
I also thank my family members and friends for their encouragement and support to me in writing this
book: my elder brothers, Janki Sharan and Dr. Sita Sharan; my sister and brother-in-law, Ratna and Abhay;
my nephews Babalu, Dabalu, Gaurav, Saurav, and Chitranjan; my friends Shivashankar Ravindranath,
Kannan Somasekar, Mahbub Choudhury, Biju Nair, Srinivas Kakkera, Anil Kumar Singh, Chris Coley, Willie
Baptiste, Rahul Jain, Larry Brewster, Greg Langham, LaTondra Okeke, Dinesh Sankala, Rahul Nagpal, Ravi
Datla, and many more friends not mentioned here.
I thank the president of my company Up and Running Inc., Josh Bush, for his support and my
wonderful, supportive coworkers—Preethi Vasudeva, Tanina Jones, Ann Wedgeworth, William Barnett, and
Shannah Glenn—for their encouragement.
My sincere thanks are due to the wonderful team at Apress for their support during the publication of
this book. Thanks to Mark Powers, the senior coordinating editor, and Matthew Moodie, the development
editor, for providing excellent support. Thanks also to the technical reviewers Jeff Friesen, David Coffin,
Wallace Jackson, Massimo Nardone, and Tri Phan for their insights and feedback during the editing process;
they were instrumental in weeding out many technical errors and improving the material. I also thank the
copy editors, Mary Bearden, Lori Cavanaugh, Lori Jacobs, and Karen Jameson, for their extraordinary efforts
in editing the book and applying many corrections during a very short span of time. Last but not least,
my sincere thanks to Steve Anglin, the lead editor at Apress, for taking the initiative for the publication of
this book.

—Kishori Sharan

Many thanks to the Apress staff for offering me to write the JavaFX version 17 edition of this book. Thanks
also to my friends for their patience concerning updates to my latest composition work, a classic style Tango
for violin and cello. This work had to stand behind a little bit, so the JavaFX book’s schedule could be held.

—Peter Späth

xxxiii
Introduction

Java had the support for developing GUI applications since its version 1.0 using the AWT (Abstract Window
Toolkit). Later, AWT was replaced by Swing, which gave a little better user experience, but still lacked the
modern-looking widgets and the support for a developer’s productivity. Both AWT and Swing lacked the
first-class support for data binding, efficient GUI rendering engines, easy-to-use 2D and 3D libraries for
developers, and style sheet support. JavaFX was first released in 2008 as the tool to use for developing rich
Internet applications (RIAs); it used a statically typed declarative language called JavaFX Script, which did
not attract a lot of attention from Java developers. JavaFX 2.0, released in 2011, caught the Java community’s
attention when it dropped the support for JavaFX Script and supported writing JavaFX programs using the
Java programming language. In its current version, JavaFX 17 is supported as an open source add-on that
can be downloaded from the Gluon company’s website. Now JavaFX 17 is considered a real successor for
Swing for building the GUI application using the Java platform.
Learn JavaFX 17 shows you how to start developing rich client desktop applications in JavaFX 17 using
your Java skills. It provides comprehensive coverage of the JavaFX 17 features. Each chapter starts with an
introduction to the topic at hand. A step-by-step discussion of the topic with small snippets of code follows.
At the end of the topic’s discussion, a complete program is presented. Special care has been taken to present
the topics in such a way that chapters can be read serially. The book contains numerous pictures to aid you
in visualizing the GUI that is built at every step in the discussion.
The book starts with an introduction to JavaFX and its history. It lists the system requirements and the
steps to start developing JavaFX applications. It shows you how to create a Hello World application in JavaFX,
explaining every line of code in the process. Later in the book, advanced topics such as 2D and 3D graphics,
charts, FXML, advanced controls, and printing are discussed.
I faced a few hurdles while writing the first edition of this book. As JavaFX 8 was being developed,
JavaFX 2, the version before JavaFX 8, was the first release of JavaFX that used the Java programming
language to write JavaFX code. There were a few bugs in JavaFX 2. Sometimes, it took me a couple of days
of hard work to create an example to work with, only to realize that there was a bug in it. Later, if something
did not work, I would look at the JIRA bug reports for JavaFX before spending too much time researching
it myself. I had to fix bugs as I found them. It took me 18 months to finish this book, and, in the end, it was
satisfying to see that what I had produced was a lot of useful material covering almost every topic in JavaFX
so fully that readers could use it to learn and build a rich client application quickly using JavaFX. I hope you
will enjoy the book and benefit greatly from it.
I believe that programming is simple if you learn it that way. Keeping this in mind, I kept the examples
in the book as simple as possible, presenting them in as few lines as I could. The examples focus on the topic
being discussed. I do not present complex GUI in my examples, keeping in mind that this could obscure the
learning process of the topic at hand. I have seen books that contain examples that run four or five pages
long, sometimes even longer; readers of such books (myself included) often get lost in trying to understand
the logic of the program, thus forgetting what they were trying to learn in that section. Therefore, simple
programs in this book are intended to help you learn JavaFX faster. The book includes many ready-to-run
programs and even more pictures. Having more pictures than programs is evident from my approach in
keeping the readers’ interest the first priority. Almost every time I discuss a snippet of code producing a UI,
I include the picture of the results of the UI, so readers are not left to their imaginations as to what the code
snippet will produce. Having to run every snippet of code to see the output can hinder the learning rhythm.

xxxv
■ Introduction

Structure of the Book
The book contains 27 chapters covering all topics—from basic to advanced—in JavaFX. Chapters are
arranged in an order that aids you to quickly learn JavaFX. I have used an incremental approach to teach
JavaFX, assuming no prior GUI development knowledge. Each chapter starts with a section introducing
the topic to be discussed in the chapter. Each section contains a bit of background of the features being
discussed, followed with code snippets and a complete program.

What You Will Learn


This book will help you to learn
• What JavaFX 17 is and its history
• How to develop rich client desktop applications using JavaFX 17
• How to use properties, collections, colors, and styles
• How to use controls and handle events to build modern GUI applications
• How to use advanced controls such as TreeView, TableView, and TreeTableView
• How to access web pages in JavaFX applications
• How to draw 2D and 3D shapes and apply effects and transformations
• How to create animations and charts using the JavaFX 17 APIs
• How to add audios and videos to your applications
• How to create GUIs in JavaFX using FXML
• How to provide the printing capabilities using the JavaFX Print API

Who Is This Book For?


Learn JavaFX 17 was written for Java developers, with beginning to intermediate-level Java skills, who want
to learn how to develop modern desktop GUI applications using JavaFX 17.

Source Code
Source code for this book can be accessed at github.com/apress/learn-javafx17.

xxxvi

You might also like