42
Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

Embed Size (px)

Citation preview

Page 1: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

Learn JavaFX 8Building User Experience and

Interfaces with Java 8

Kishori Sharan

Page 2: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

Learn JavaFX 8: Building User Experience and Interfaces with Java 8

Copyright © 2015 by Kishori Sharan

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. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law.

ISBN-13 (pbk): 978-1-4842-1143-4

ISBN-13 (electronic): 978-1-4842-1142-7

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: Welmoed SpahrLead Editor: Steve AnglinTechnical Reviewers: Jeff Friesen, David Coffin, Wallace Jackson, Massimo Nardone, and Tri PhanEditorial Board: Steve Anglin, Louise Corrigan, Jonathan Gennick, Robert Hutchinson,

Michelle Lowman, James Markham, Susan McDermott, Matthew Moodie, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Gwenan Spearing, Steve Weiss

Coordinating Editor: Mark PowersCopy Editors: Mary Bearden, Lori Cavanaugh, Lori Jacobs, and Karen JamesonCompositor: SPi GlobalIndexer: SPi GlobalArtist: SPi GlobalCover Designer: Anna Ishchenko

Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], 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 [email protected], or visit www.apress.com.

Apress and friends of ED books 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 Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales.

Any source code or other supplementary material referenced by the author in this text is available to readers at www.apress.com/9781484211434. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/.

Page 3: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

To my father-in-law Mr. Jim Baker

Page 4: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan
Page 5: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

v

Contents at a Glance

About the Author �������������������������������������������������������������������������������������������������xxxv

About the Technical Reviewers �������������������������������������������������������������������������xxxvii

Acknowledgments ����������������������������������������������������������������������������������������������xxxix

Introduction ������������������������������������������������������������������������������������������������������������ xli

Chapter 1: Getting Started ■ ������������������������������������������������������������������������������������� 1

Chapter 2: Properties and Bindings ■ ��������������������������������������������������������������������� 25

Chapter 3: Observable Collections ■ ����������������������������������������������������������������������� 83

Chapter 4: Managing Stages ■ ����������������������������������������������������������������������������� 127

Chapter 5: Making Scenes ■ ��������������������������������������������������������������������������������� 149

Chapter 6: Understanding Nodes ■ ����������������������������������������������������������������������� 163

Chapter 7: Playing with Colors ■ �������������������������������������������������������������������������� 201

Chapter 8: Styling Nodes ■ ����������������������������������������������������������������������������������� 223

Chapter 9: Event Handling ■ ��������������������������������������������������������������������������������� 259

Chapter 10: Understanding Layout Panes ■ ���������������������������������������������������������� 303

Chapter 11: Model-View-Controller Pattern ■ ������������������������������������������������������ 419

Chapter 12: Understanding Controls ■ ����������������������������������������������������������������� 435

Chapter 13: Understanding TableView ■ ��������������������������������������������������������������� 617

Chapter 14: Understanding TreeView ■ ���������������������������������������������������������������� 663

Chapter 15: Understanding TreeTableView ■ �������������������������������������������������������� 689

Chapter 16: Browsing Web Pages ■ ���������������������������������������������������������������������� 711

Page 6: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents at a GlanCe

vi

Chapter 17: Understanding 2D Shapes ■ �������������������������������������������������������������� 741

Chapter 18: Understanding Text Nodes ■ ������������������������������������������������������������� 789

Chapter 19: Understanding 3D Shapes ■ �������������������������������������������������������������� 805

Chapter 20: Applying Effects ■ ����������������������������������������������������������������������������� 841

Chapter 21: Understanding Transformations ■ ����������������������������������������������������� 899

Chapter 22: Understanding Animation ■ �������������������������������������������������������������� 917

Chapter 23: Understanding Charts ■ �������������������������������������������������������������������� 953

Chapter 24: Understanding the Image API ■ �������������������������������������������������������� 997

Chapter 25: Drawing on a Canvas ■ ������������������������������������������������������������������� 1033

Chapter 26: Understanding Drag and Drop ■ ������������������������������������������������������ 1043

Chapter 27: Understanding Concurrency in JavaFX ■ ���������������������������������������� 1071

Chapter 28: Playing Audios and Videos ■ ����������������������������������������������������������� 1101

Chapter 29: Understanding FXML ■ �������������������������������������������������������������������� 1120

Chapter 30: Understanding the Print API ■ �������������������������������������������������������� 1157

Index ������������������������������������������������������������������������������������������������������������������� 1173

Page 7: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

vii

Contents

About the Author �������������������������������������������������������������������������������������������������xxxv

About the Technical Reviewers �������������������������������������������������������������������������xxxvii

Acknowledgments ����������������������������������������������������������������������������������������������xxxix

Introduction ������������������������������������������������������������������������������������������������������������ xli

Chapter 1: Getting Started ■ ������������������������������������������������������������������������������������� 1

What Is JavaFX? ��������������������������������������������������������������������������������������������������������������� 1

History of JavaFX ������������������������������������������������������������������������������������������������������������� 3

System Requirements ������������������������������������������������������������������������������������������������������ 4

JavaFX Runtime Library ��������������������������������������������������������������������������������������������������� 4

JavaFX Source Code ��������������������������������������������������������������������������������������������������������� 4

Your First JavaFX Application ������������������������������������������������������������������������������������������� 4

Creating the HelloJavaFX Class �������������������������������������������������������������������������������������������������������������� 4

Overriding the start() Method ����������������������������������������������������������������������������������������������������������������� 5

Showing the Stage ��������������������������������������������������������������������������������������������������������������������������������� 6

Launching the Application ���������������������������������������������������������������������������������������������������������������������� 7

Adding the main() Method ���������������������������������������������������������������������������������������������������������������������� 8

Adding a Scene to the Stage ������������������������������������������������������������������������������������������������������������������ 9

Improving the HelloFX Application���������������������������������������������������������������������������������� 10

Using the NetBeans IDE �������������������������������������������������������������������������������������������������� 13

Creating a New JavaFX Project ������������������������������������������������������������������������������������������������������������ 13

Opening an Existing JavaFX Project ����������������������������������������������������������������������������������������������������� 14

Running a JavaFX Project from the NetBeans IDE �������������������������������������������������������������������������������� 15

Page 8: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

viii

Passing Parameters to a JavaFX Application ����������������������������������������������������������������� 15

Case 1 ��������������������������������������������������������������������������������������������������������������������������������������������������� 17

Case 2 ��������������������������������������������������������������������������������������������������������������������������������������������������� 17

Case 3 ��������������������������������������������������������������������������������������������������������������������������������������������������� 18

Case 4 ��������������������������������������������������������������������������������������������������������������������������������������������������� 18

Launching a JavaFX Application ������������������������������������������������������������������������������������� 19

The Life Cycle of a JavaFX Application ��������������������������������������������������������������������������� 21

Terminating a JavaFX Application ���������������������������������������������������������������������������������� 23

Summary ������������������������������������������������������������������������������������������������������������������������ 24

Chapter 2: Properties and Bindings ■ ��������������������������������������������������������������������� 25

What Is a Property? �������������������������������������������������������������������������������������������������������� 25

What Is a Binding? �������������������������������������������������������������������������������������������������������� 27

Understanding Bindings Support in JavaBeans ������������������������������������������������������������� 28

Understanding Properties in JavaFX ������������������������������������������������������������������������������ 31

Using Properties in JavaFX Beans ���������������������������������������������������������������������������������� 33

Lazily Instantiating Property Objects ������������������������������������������������������������������������������ 38

Understanding the Property Class Hierarchy������������������������������������������������������������������ 40

Handling Property Invalidation Events ���������������������������������������������������������������������������� 43

Handling Property Change Events ���������������������������������������������������������������������������������� 46

Avoiding Memory Leaks in Listeners ����������������������������������������������������������������������������� 48

Handling Invalidation and Change Events ���������������������������������������������������������������������� 53

Using Bindings in JavaFX ����������������������������������������������������������������������������������������������� 55

Unidirectional and Bidirectional Bindings ��������������������������������������������������������������������� 58

Understanding the Binding API ��������������������������������������������������������������������������������������� 62

The High-Level Binding API ������������������������������������������������������������������������������������������������������������������ 62

Using the Low-Level Binding API ���������������������������������������������������������������������������������������������������������� 77

Using Bindings to Center a Circle ���������������������������������������������������������������������������������� 80

Summary ������������������������������������������������������������������������������������������������������������������������ 82

Page 9: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

ix

Chapter 3: Observable Collections ■ ����������������������������������������������������������������������� 83

What Are Observable Collections? ��������������������������������������������������������������������������������� 83

Understanding ObservableList ��������������������������������������������������������������������������������������� 84

Creating an ObservableList ������������������������������������������������������������������������������������������������������������������� 86

Observing an ObservableList for Invalidations ������������������������������������������������������������������������������������� 87

Observing an ObservableList for Changes �������������������������������������������������������������������������������������������� 89

Understanding ObservableSet �������������������������������������������������������������������������������������� 101

Creating an ObservableSet ����������������������������������������������������������������������������������������������������������������� 102

Observing an ObservableSet for Invalidations ������������������������������������������������������������������������������������ 103

Observing an ObservableSet for Changes ������������������������������������������������������������������������������������������ 105

Understanding ObservableMap ������������������������������������������������������������������������������������ 106

Creating an ObservableMap ��������������������������������������������������������������������������������������������������������������� 107

Observing an ObservableMap for Invalidations ���������������������������������������������������������������������������������� 108

Observing an ObservableMap for Changes ���������������������������������������������������������������������������������������� 110

Properties and Bindings for JavaFX Collections ����������������������������������������������������������� 112

Understanding ObservableList Property and Binding ������������������������������������������������������������������������� 112

Understanding ObservableSet Property and Binding�������������������������������������������������������������������������� 121

Understanding ObservableMap Property and Binding ������������������������������������������������������������������������ 124

Summary ���������������������������������������������������������������������������������������������������������������������� 126

Chapter 4: Managing Stages ■ ����������������������������������������������������������������������������� 127

Knowing the Details of Your Screens ��������������������������������������������������������������������������� 127

What Is a Stage? ���������������������������������������������������������������������������������������������������������� 129

Showing the Primary Stage ������������������������������������������������������������������������������������������ 130

Setting the Bounds of a Stage �������������������������������������������������������������������������������������� 132

Initializing the Style of a Stage ������������������������������������������������������������������������������������� 136

Moving an Undecorated Stage ������������������������������������������������������������������������������������� 139

Initializing Modality of a Stage ������������������������������������������������������������������������������������� 141

Setting the Opacity of a Stage �������������������������������������������������������������������������������������� 144

Page 10: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

x

Resizing a Stage ����������������������������������������������������������������������������������������������������������� 144

Showing a Stage in Full-Screen Mode ������������������������������������������������������������������������� 145

Showing a Stage and Waiting for It to Close ���������������������������������������������������������������� 145

Summary ���������������������������������������������������������������������������������������������������������������������� 148

Chapter 5: Making Scenes ■ ��������������������������������������������������������������������������������� 149

What Is a Scene? ���������������������������������������������������������������������������������������������������������� 149

Graphics Rendering Modes ������������������������������������������������������������������������������������������ 151

Setting the Cursor for a Scene ������������������������������������������������������������������������������������� 152

The Focus Owner in a Scene ���������������������������������������������������������������������������������������� 153

Using Builder Classes ��������������������������������������������������������������������������������������������������� 153

Understanding the Platform Class �������������������������������������������������������������������������������� 156

Knowing the Host Environment ������������������������������������������������������������������������������������ 158

Summary ���������������������������������������������������������������������������������������������������������������������� 161

Chapter 6: Understanding Nodes ■ ����������������������������������������������������������������������� 163

What Is a Node? ����������������������������������������������������������������������������������������������������������� 163

The Cartesian Coordinate System ������������������������������������������������������������������������������� 163

Cartesian Coordinate System of a Node ����������������������������������������������������������������������� 165

The Concept of Bounds and Bounding Box ������������������������������������������������������������������ 166

Knowing the Bounds of a Node ������������������������������������������������������������������������������������ 168

The layoutBounds Property ���������������������������������������������������������������������������������������������������������������� 170

The boundsInLocal Property ��������������������������������������������������������������������������������������������������������������� 172

The boundsInParent Property ������������������������������������������������������������������������������������������������������������� 174

Bounds of a Group �������������������������������������������������������������������������������������������������������� 174

A Detailed Example on Bounds ������������������������������������������������������������������������������������ 174

Positioning a Node Using layoutX and layoutY ������������������������������������������������������������� 179

Setting the Size of a Node �������������������������������������������������������������������������������������������� 180

Resizable Nodes ��������������������������������������������������������������������������������������������������������������������������������� 182

Nonresizable Nodes ���������������������������������������������������������������������������������������������������������������������������� 189

Page 11: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

xi

Storing User Data in a Node ����������������������������������������������������������������������������������������� 190

What Is a Managed Node? ������������������������������������������������������������������������������������������� 191

Transforming Bounds between Coordinate Spaces ����������������������������������������������������� 195

Summary ���������������������������������������������������������������������������������������������������������������������� 198

Chapter 7: Playing with Colors ■ �������������������������������������������������������������������������� 201

Understanding Colors ��������������������������������������������������������������������������������������������������� 201

Using the Color Class �������������������������������������������������������������������������������������������������������������������������� 202

Using the ImagePattern Class ������������������������������������������������������������������������������������������������������������ 203

Understanding Linear Color Gradient ��������������������������������������������������������������������������� 206

Using the LinearGradient Class����������������������������������������������������������������������������������������������������������� 207

Defining Linear Color Gradients Using a String Format ���������������������������������������������������������������������� 212

Understanding Radial Color Gradient ��������������������������������������������������������������������������� 214

Using the RadialGradient Class����������������������������������������������������������������������������������������������������������� 215

Defining Radial Color Gradients in String Format ������������������������������������������������������������������������������� 220

Summary ���������������������������������������������������������������������������������������������������������������������� 221

Chapter 8: Styling Nodes ■ ����������������������������������������������������������������������������������� 223

What Is a Cascading Style Sheet? �������������������������������������������������������������������������������� 223

What are Styles, Skins, and Themes? �������������������������������������������������������������������������� 224

A Quick Example ���������������������������������������������������������������������������������������������������������� 224

Naming Conventions in JavaFX CSS ���������������������������������������������������������������������������� 226

Adding Style Sheets ����������������������������������������������������������������������������������������������������� 226

Default Style Sheet ������������������������������������������������������������������������������������������������������� 227

Adding Inline Styles ������������������������������������������������������������������������������������������������������ 228

Priorities of Styles for a Node ��������������������������������������������������������������������������������������� 230

Inheriting CSS Properties �������������������������������������������������������������������������������������������� 233

Types of CSS Properties ����������������������������������������������������������������������������������������������� 235

The inherit Type ���������������������������������������������������������������������������������������������������������������������������������� 235

The boolean Type �������������������������������������������������������������������������������������������������������������������������������� 235

The string Type ����������������������������������������������������������������������������������������������������������������������������������� 235

Page 12: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

xii

The number Type �������������������������������������������������������������������������������������������������������������������������������� 236

The angle Type ������������������������������������������������������������������������������������������������������������������������������������ 236

The point Type ������������������������������������������������������������������������������������������������������������������������������������ 236

The color-stop Type ���������������������������������������������������������������������������������������������������������������������������� 236

The URI Type ��������������������������������������������������������������������������������������������������������������������������������������� 237

The effect Type ����������������������������������������������������������������������������������������������������������������������������������� 237

The font Type �������������������������������������������������������������������������������������������������������������������������������������� 237

The paint Type ������������������������������������������������������������������������������������������������������������������������������������ 238

Specifying Background Colors ������������������������������������������������������������������������������������� 241

Specifying Borders ������������������������������������������������������������������������������������������������������� 242

Border Colors �������������������������������������������������������������������������������������������������������������������������������������� 243

Border Widths ������������������������������������������������������������������������������������������������������������������������������������� 243

Border Radii ���������������������������������������������������������������������������������������������������������������������������������������� 244

Border Insets �������������������������������������������������������������������������������������������������������������������������������������� 244

Border Styles �������������������������������������������������������������������������������������������������������������������������������������� 244

Understanding Style Selectors ������������������������������������������������������������������������������������� 247

Using Class Selectors ������������������������������������������������������������������������������������������������������������������������� 247

Class Selector for the root Node ��������������������������������������������������������������������������������������������������������� 250

Using ID Selectors ������������������������������������������������������������������������������������������������������������������������������ 251

Combining ID and Class Selectors ������������������������������������������������������������������������������������������������������ 253

The Universal Selector ������������������������������������������������������������������������������������������������������������������������ 253

Grouping Multiple Selectors ��������������������������������������������������������������������������������������������������������������� 253

Descendant Selectors ������������������������������������������������������������������������������������������������������������������������� 254

Child Selectors ������������������������������������������������������������������������������������������������������������������������������������ 254

State-Based Selectors ������������������������������������������������������������������������������������������������������������������������ 255

Using JavaFX Class Names as Selectors �������������������������������������������������������������������������������������������� 256

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

Using Compiled Style Sheets ��������������������������������������������������������������������������������������� 257

Summary ���������������������������������������������������������������������������������������������������������������������� 257

Page 13: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

xiii

Chapter 9: Event Handling ■ ��������������������������������������������������������������������������������� 259

What Is an Event? ��������������������������������������������������������������������������������������������������������� 259

Event Class Hierarchy ��������������������������������������������������������������������������������������������������� 260

Event Targets ���������������������������������������������������������������������������������������������������������������� 261

Event Types ������������������������������������������������������������������������������������������������������������������ 261

Event Processing Mechanism �������������������������������������������������������������������������������������� 262

Event Target Selection ������������������������������������������������������������������������������������������������������������������������ 263

Event Route Construction ������������������������������������������������������������������������������������������������������������������� 263

Event Route Traversal ������������������������������������������������������������������������������������������������������������������������� 264

Handling Events ����������������������������������������������������������������������������������������������������������� 266

Creating Event Filters and Handlers ��������������������������������������������������������������������������������������������������� 267

Registering Event Filters and Handlers ���������������������������������������������������������������������������������������������� 267

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

Consuming Events �������������������������������������������������������������������������������������������������������� 276

Handling Input Events �������������������������������������������������������������������������������������������������� 279

Handling Mouse Events ������������������������������������������������������������������������������������������������ 280

Getting Mouse Location ��������������������������������������������������������������������������������������������������������������������� 281

Representing Mouse Buttons ������������������������������������������������������������������������������������������������������������� 282

State of Mouse Buttons ���������������������������������������������������������������������������������������������������������������������� 283

Hysteresis in GUI Applications ������������������������������������������������������������������������������������������������������������ 284

State of Modifier Keys ������������������������������������������������������������������������������������������������������������������������ 284

Picking Mouse Events on Bounds ������������������������������������������������������������������������������������������������������� 285

Mouse Transparency ��������������������������������������������������������������������������������������������������������������������������� 287

Synthesized Mouse Events ����������������������������������������������������������������������������������������������������������������� 289

Handling Mouse Entered and Exited Events ��������������������������������������������������������������������������������������� 289

Handling Key Events ����������������������������������������������������������������������������������������������������� 294

Handling Key-pressed and Key-released Events �������������������������������������������������������������������������������� 295

Handling the Key-typed Event ������������������������������������������������������������������������������������������������������������ 297

Handling Window Events ���������������������������������������������������������������������������������������������� 299

Summary ���������������������������������������������������������������������������������������������������������������������� 301

Page 14: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

xiv

Chapter 10: Understanding Layout Panes ■ ���������������������������������������������������������� 303

What Is a Layout Pane? ������������������������������������������������������������������������������������������������ 303

Layout Pane Classes ���������������������������������������������������������������������������������������������������� 304

Adding Children to a Layout Pane �������������������������������������������������������������������������������� 306

Utility Classes and Enums �������������������������������������������������������������������������������������������� 307

The Insets Class ���������������������������������������������������������������������������������������������������������������������������������� 307

The HPos Enum ���������������������������������������������������������������������������������������������������������������������������������� 308

The VPos Enum ����������������������������������������������������������������������������������������������������������������������������������� 308

The Pos Enum ������������������������������������������������������������������������������������������������������������������������������������� 308

The HorizontalDirection Enum ������������������������������������������������������������������������������������������������������������ 308

The VerticalDirection Enum ���������������������������������������������������������������������������������������������������������������� 309

The Orientation Enum ������������������������������������������������������������������������������������������������������������������������� 309

The Side Enum ������������������������������������������������������������������������������������������������������������������������������������ 309

The Priority Enum ������������������������������������������������������������������������������������������������������������������������������� 309

Understanding Group ��������������������������������������������������������������������������������������������������� 309

Creating a Group Object���������������������������������������������������������������������������������������������������������������������� 310

Rendering Nodes in a Group ��������������������������������������������������������������������������������������������������������������� 310

Positioning Nodes in a Group�������������������������������������������������������������������������������������������������������������� 311

Applying Effects and Transformations to a Group ������������������������������������������������������������������������������� 312

Styling a Group with CSS �������������������������������������������������������������������������������������������������������������������� 313

Understanding Region �������������������������������������������������������������������������������������������������� 313

Setting Backgrounds �������������������������������������������������������������������������������������������������������������������������� 315

Setting Padding ���������������������������������������������������������������������������������������������������������������������������������� 319

Setting Borders ����������������������������������������������������������������������������������������������������������������������������������� 319

Setting Margins ���������������������������������������������������������������������������������������������������������������������������������� 332

Understanding Panes ��������������������������������������������������������������������������������������������������� 332

Understanding HBox ����������������������������������������������������������������������������������������������������� 334

Creating HBox Objects ������������������������������������������������������������������������������������������������������������������������ 334

HBox Properties ���������������������������������������������������������������������������������������������������������������������������������� 336

Setting Constraints for Children in HBox �������������������������������������������������������������������������������������������� 339

Page 15: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

xv

Understanding VBox ����������������������������������������������������������������������������������������������������� 342

Creating VBox Objects ������������������������������������������������������������������������������������������������������������������������ 342

VBox Properties ���������������������������������������������������������������������������������������������������������������������������������� 344

Setting Constraints for Children in VBox ��������������������������������������������������������������������������������������������� 348

Understanding FlowPane ��������������������������������������������������������������������������������������������� 350

Creating FlowPane Objects ����������������������������������������������������������������������������������������������������������������� 351

FlowPane Properties ��������������������������������������������������������������������������������������������������������������������������� 353

Content Bias of a FlowPane ���������������������������������������������������������������������������������������������������������������� 358

Understanding BorderPane ������������������������������������������������������������������������������������������ 358

Creating BorderPane Objects�������������������������������������������������������������������������������������������������������������� 359

BorderPane Properties������������������������������������������������������������������������������������������������������������������������ 362

Setting Constraints for Children in BorderPane ���������������������������������������������������������������������������������� 362

Understanding StackPane �������������������������������������������������������������������������������������������� 363

Creating StackPane Objects ��������������������������������������������������������������������������������������������������������������� 363

StackPane Properties ������������������������������������������������������������������������������������������������������������������������� 367

Setting Constraints for Children ��������������������������������������������������������������������������������������������������������� 369

Understanding TilePane������������������������������������������������������������������������������������������������ 371

Creating TilePane Objects ������������������������������������������������������������������������������������������������������������������� 372

TilePane Properties ����������������������������������������������������������������������������������������������������������������������������� 374

Setting Constraints for Children in TilePane ��������������������������������������������������������������������������������������� 377

Understanding GridPane����������������������������������������������������������������������������������������������� 380

Creating GridPane Objects ������������������������������������������������������������������������������������������������������������������ 381

Making Grid Lines Visible ������������������������������������������������������������������������������������������������������������������� 381

Adding Children to GridPane ��������������������������������������������������������������������������������������������������������������� 381

Creating Forms Using GridPanes �������������������������������������������������������������������������������������������������������� 387

GridPane Properties ���������������������������������������������������������������������������������������������������������������������������� 390

The hgap and vgap Properties ������������������������������������������������������������������������������������������������������������ 392

Customizing Columns and Rows �������������������������������������������������������������������������������������������������������� 393

Setting Constraints on Children in GridPane ��������������������������������������������������������������������������������������� 399

Page 16: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

xvi

Understanding AnchorPane ������������������������������������������������������������������������������������������ 404

Creating AnchorPane Objects ������������������������������������������������������������������������������������������������������������� 405

Setting Constraints for Children in AnchorPane ��������������������������������������������������������������������������������� 406

Understanding TextFlow ����������������������������������������������������������������������������������������������� 410

Creating TextFlow Objects ������������������������������������������������������������������������������������������������������������������ 411

TextFlow Properties ���������������������������������������������������������������������������������������������������������������������������� 414

Setting Constraints for Children in TextFlow ��������������������������������������������������������������������������������������� 415

Snapping to Pixel ��������������������������������������������������������������������������������������������������������� 415

Summary ���������������������������������������������������������������������������������������������������������������������� 416

Chapter 11: Model-View-Controller Pattern ■ ������������������������������������������������������ 419

What Is the Model-View-Controller Pattern? ���������������������������������������������������������������� 419

A Model-View-Presenter Example �������������������������������������������������������������������������������� 422

The Requirements ������������������������������������������������������������������������������������������������������������������������������ 422

The Design ������������������������������������������������������������������������������������������������������������������������������������������ 423

The Implementation ���������������������������������������������������������������������������������������������������������������������������� 423

Putting Them Together ������������������������������������������������������������������������������������������������������������������������ 433

Summary ���������������������������������������������������������������������������������������������������������������������� 434

Chapter 12: Understanding Controls ■ ����������������������������������������������������������������� 435

What Is a Control? �������������������������������������������������������������������������������������������������������� 435

Understanding Control Classes Hierarchy �������������������������������������������������������������������� 436

Labeled Controls ���������������������������������������������������������������������������������������������������������� 437

Positioning Graphic and Text �������������������������������������������������������������������������������������������������������������� 439

Understanding Mnemonics and Accelerators ������������������������������������������������������������������������������������� 440

Understanding the Label Control ���������������������������������������������������������������������������������� 442

Understanding Buttons ������������������������������������������������������������������������������������������������ 444

Understanding Command Buttons ������������������������������������������������������������������������������� 445

Understanding the Button Control ������������������������������������������������������������������������������������������������������ 445

Understanding the Hyperlink Control �������������������������������������������������������������������������������������������������� 448

Understanding the MenuButton Control ��������������������������������������������������������������������������������������������� 449

Page 17: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

xvii

Understanding Choice Buttons ������������������������������������������������������������������������������������� 451

Understanding the ToggleButton Control �������������������������������������������������������������������������������������������� 452

Understanding the RadioButton Control ��������������������������������������������������������������������������������������������� 455

Understanding the CheckBox Control ������������������������������������������������������������������������������������������������� 458

Understanding the Hybrid Button Control ��������������������������������������������������������������������� 462

Making Selections from a List of Items ������������������������������������������������������������������������ 464

Understanding the ChoiceBox Control �������������������������������������������������������������������������� 464

Using Domain Objects in ChoiceBox ��������������������������������������������������������������������������������������������������� 469

Allowing Nulls in ChoiceBox ��������������������������������������������������������������������������������������������������������������� 471

Using Separators in ChoiceBox ���������������������������������������������������������������������������������������������������������� 473

Styling a ChoiceBox with CSS ������������������������������������������������������������������������������������������������������������ 474

Understanding the ComboBox Control ������������������������������������������������������������������������� 474

Detecting Value Change in ComboBox ������������������������������������������������������������������������������������������������ 477

Using Domain Objects in Editable ComboBox ������������������������������������������������������������������������������������� 477

Customizing the Height of Pop-up List ����������������������������������������������������������������������������������������������� 479

Using Nodes as Items in ComboBox ��������������������������������������������������������������������������������������������������� 480

Using a Cell Factory in ComboBox ������������������������������������������������������������������������������������������������������ 481

Styling ComboBox with CSS ��������������������������������������������������������������������������������������������������������������� 484

Understanding the ListView Control ����������������������������������������������������������������������������� 485

Orientation of a ListView��������������������������������������������������������������������������������������������������������������������� 487

Selection Model in ListView ���������������������������������������������������������������������������������������������������������������� 488

Using Cell Factory in ListView ������������������������������������������������������������������������������������������������������������ 492

Using Editable ListView ���������������������������������������������������������������������������������������������������������������������� 494

Handling Events While Editing a ListView ������������������������������������������������������������������������������������������� 501

Styling ListView with CSS ������������������������������������������������������������������������������������������������������������������� 503

Understanding the ColorPicker Control ������������������������������������������������������������������������ 504

Using the ColorPicker Control ������������������������������������������������������������������������������������������������������������� 506

Styling ColorPicker with CSS �������������������������������������������������������������������������������������������������������������� 509

Understanding the DatePicker Control ������������������������������������������������������������������������� 509

Using the DatePicker Control �������������������������������������������������������������������������������������������������������������� 510

Styling DatePicker with CSS ��������������������������������������������������������������������������������������������������������������� 515

Page 18: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

xviii

Understanding Text Input Controls ������������������������������������������������������������������������������� 515

Positioning and Moving Caret ������������������������������������������������������������������������������������������������������������� 516

Making Text Selection ������������������������������������������������������������������������������������������������������������������������� 517

Modifying the Content ������������������������������������������������������������������������������������������������������������������������ 518

Cutting, Copying, and Pasting Text ������������������������������������������������������������������������������������������������������ 519

An Example ����������������������������������������������������������������������������������������������������������������������������������������� 519

Styling TextInputControl with CSS ������������������������������������������������������������������������������������������������������ 520

Understanding the TextField Control ���������������������������������������������������������������������������� 521

Styling TextField with CSS ������������������������������������������������������������������������������������������������������������������ 524

Understanding the PasswordField Control ������������������������������������������������������������������� 524

Understanding the TextArea Control ����������������������������������������������������������������������������� 525

Styling TextArea with CSS ������������������������������������������������������������������������������������������������������������������� 528

Showing the Progress of a Task ����������������������������������������������������������������������������������� 529

Styling ProgressIndicator with CSS ���������������������������������������������������������������������������������������������������� 532

Styling ProgressIndicator and Bar with CSS ��������������������������������������������������������������������������������������� 532

Understanding the TitledPane Control �������������������������������������������������������������������������� 533

Styling TitledPane with CSS ���������������������������������������������������������������������������������������������������������������� 536

Understanding the Accordion Control ��������������������������������������������������������������������������� 538

Styling Accordion with CSS ���������������������������������������������������������������������������������������������������������������� 540

Understanding the Pagination Control �������������������������������������������������������������������������� 540

Styling Pagination with CSS ��������������������������������������������������������������������������������������������������������������� 544

Understanding the Tool Tip Control ������������������������������������������������������������������������������� 545

Styling Tooltip with CSS ���������������������������������������������������������������������������������������������������������������������� 549

Providing Scrolling Features in Controls ���������������������������������������������������������������������� 550

Understanding the ScrollBar Control �������������������������������������������������������������������������������������������������� 550

Understanding the ScrollPane Control ������������������������������������������������������������������������������������������������ 551

Keeping Things Separate ��������������������������������������������������������������������������������������������� 554

Understanding the Separator Control ������������������������������������������������������������������������������������������������� 554

Styling Separator with CSS����������������������������������������������������������������������������������������������������������������� 556

Understanding the SplitPane Control �������������������������������������������������������������������������������������������������� 556

Styling SplitPane with CSS ����������������������������������������������������������������������������������������������������������������� 559

Page 19: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

xix

Understanding the Slider Control ��������������������������������������������������������������������������������� 559

Styling Slider with CSS ����������������������������������������������������������������������������������������������������������������������� 564

Understanding Menus �������������������������������������������������������������������������������������������������� 566

Using Menu Bars �������������������������������������������������������������������������������������������������������������������������������� 567

Using Menus ��������������������������������������������������������������������������������������������������������������������������������������� 568

Using Menu Items ������������������������������������������������������������������������������������������������������������������������������� 571

Putting All Parts of Menus Together ���������������������������������������������������������������������������������������������������� 577

Styling Menus Using CSS ������������������������������������������������������������������������������������������������������������������� 581

Understanding the ContextMenu Control ��������������������������������������������������������������������� 581

Styling ContextMenu with CSS ����������������������������������������������������������������������������������������������������������� 585

Understanding the ToolBar Control ������������������������������������������������������������������������������� 586

Styling a Toolbar with CSS ������������������������������������������������������������������������������������������������������������������ 588

Understanding TabPane and Tab ���������������������������������������������������������������������������������� 589

Creating Tabs �������������������������������������������������������������������������������������������������������������������������������������� 590

Setting the Title and Content of Tabs �������������������������������������������������������������������������������������������������� 591

Creating TabPanes ������������������������������������������������������������������������������������������������������������������������������ 592

Adding Tabs to a TabPane ������������������������������������������������������������������������������������������������������������������� 592

Putting TabPanes and Tabs Together �������������������������������������������������������������������������������������������������� 592

Understanding Tab Selection �������������������������������������������������������������������������������������������������������������� 595

Closing Tabs in a TabPane ������������������������������������������������������������������������������������������������������������������ 597

Positioning Tabs in a TabPane ������������������������������������������������������������������������������������������������������������ 601

Sizing Tabs in a TabPane �������������������������������������������������������������������������������������������������������������������� 602

Using Recessed and Floating TabPanes ��������������������������������������������������������������������������������������������� 602

Styling Tab and TabPane with CSS ����������������������������������������������������������������������������������������������������� 603

Understanding the HTMLEditor Control ������������������������������������������������������������������������ 605

Creating an HTMLEditor ���������������������������������������������������������������������������������������������������������������������� 606

Using an HTMLEditor �������������������������������������������������������������������������������������������������������������������������� 606

Styling HTMLEditor with CSS �������������������������������������������������������������������������������������������������������������� 607

Page 20: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

xx

Choosing Files and Directories ������������������������������������������������������������������������������������� 608

The FileChooser Dialog ����������������������������������������������������������������������������������������������������������������������� 608

The DirectoryChooser Dialog �������������������������������������������������������������������������������������������������������������� 613

Summary ���������������������������������������������������������������������������������������������������������������������� 613

Chapter 13: Understanding TableView ■ ��������������������������������������������������������������� 617

What Is a TableView ? ��������������������������������������������������������������������������������������������������� 617

Creating a TableView ���������������������������������������������������������������������������������������������������� 620

Adding Columns to a TableView ���������������������������������������������������������������������������������������������������������� 620

Customizing TableView Placeholder ��������������������������������������������������������������������������������������������������� 623

Populating a TableColumn with Data �������������������������������������������������������������������������������������������������� 624

Using a Map as Items in a TableView ��������������������������������������������������������������������������� 629

Showing and Hiding Columns ������������������������������������������������������������������������������������������������������������� 631

Reordering Columns in a TableView ��������������������������������������������������������������������������������������������������� 632

Sorting Data in a TableView ������������������������������������������������������������������������������������������ 632

Sorting Data by Users ������������������������������������������������������������������������������������������������������������������������� 632

Sorting Data Programmatically ����������������������������������������������������������������������������������������������������������� 633

Customizing Data Rendering in Cells ��������������������������������������������������������������������������� 637

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

Editing Data in a TableView ������������������������������������������������������������������������������������������ 643

Editing Data Using a Check Box���������������������������������������������������������������������������������������������������������� 643

Editing Data Using a Choice Box ��������������������������������������������������������������������������������������������������������� 644

Editing Data Using a Combo Box �������������������������������������������������������������������������������������������������������� 645

Editing Data Using a TextField ������������������������������������������������������������������������������������������������������������ 645

Editing Data in TableCell Using any Control ���������������������������������������������������������������������������������������� 649

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

Scrolling in a TableView ����������������������������������������������������������������������������������������������� 657

Resizing a TableColumn ����������������������������������������������������������������������������������������������� 658

Styling a TableView with CSS ��������������������������������������������������������������������������������������� 659

Summary ���������������������������������������������������������������������������������������������������������������������� 661

Page 21: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

xxi

Chapter 14: Understanding TreeView ■ ���������������������������������������������������������������� 663

What Is a TreeView ? ����������������������������������������������������������������������������������������������������� 663

Creating a TreeView ������������������������������������������������������������������������������������������������������ 664

Hiding the Root Node ��������������������������������������������������������������������������������������������������� 667

Understanding the TreeItem ��������������������������������������������������������������������������������������������������������������� 667

Handling TreeItem Events ��������������������������������������������������������������������������������������������� 668

Adding and Removing Nodes ��������������������������������������������������������������������������������������� 669

Customizing Cells in a TreeView ����������������������������������������������������������������������������������� 672

Editing Data in a TreeView �������������������������������������������������������������������������������������������� 674

Editing Data using a Check Box ���������������������������������������������������������������������������������������������������������� 675

Editing Data Using a Choice Box ��������������������������������������������������������������������������������������������������������� 677

Editing Data Using a Combo Box �������������������������������������������������������������������������������������������������������� 678

Editing Data Using a TextField ������������������������������������������������������������������������������������������������������������ 678

Loading TreeItems on Demand ������������������������������������������������������������������������������������� 680

Scrolling to a TreeItem ������������������������������������������������������������������������������������������������� 684

TreeView Selection Model �������������������������������������������������������������������������������������������� 685

Styling TreeView with CSS�������������������������������������������������������������������������������������������� 685

Summary ���������������������������������������������������������������������������������������������������������������������� 687

Chapter 15: Understanding TreeTableView ■ �������������������������������������������������������� 689

What Is a TreeTableView? ��������������������������������������������������������������������������������������������� 689

Model for TreeTableView ���������������������������������������������������������������������������������������������� 690

Creating a TreeTableView ��������������������������������������������������������������������������������������������� 691

Sorting Data in a TreeTableView ����������������������������������������������������������������������������������� 697

Populating a TreeTableColumn with Data ��������������������������������������������������������������������� 697

Showing and Hiding Columns �������������������������������������������������������������������������������������� 698

Customizing Data Rendering in Cells ��������������������������������������������������������������������������� 698

Selecting Cells and Rows in a TreeTableView �������������������������������������������������������������� 700

Page 22: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

xxii

Editing Data in a TableView ������������������������������������������������������������������������������������������ 701

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

Scrolling in a TreeTableView ����������������������������������������������������������������������������������������� 708

Styling TreeTableView with CSS ����������������������������������������������������������������������������������� 709

Summary ���������������������������������������������������������������������������������������������������������������������� 710

Chapter 16: Browsing Web Pages ■ ���������������������������������������������������������������������� 711

What Is a WebView? ����������������������������������������������������������������������������������������������������� 711

Creating a Web Browser Component ���������������������������������������������������������������������������� 712

Setting Properties for a WebView ��������������������������������������������������������������������������������� 715

Enhancing the Web Browser Application ���������������������������������������������������������������������� 716

Accessing Browsing History ����������������������������������������������������������������������������������������� 722

Handling JavaScript UI Requests ��������������������������������������������������������������������������������� 725

Executing JavaScript Code from JavaFX ���������������������������������������������������������������������� 733

Executing JavaFX Code from JavaScript ���������������������������������������������������������������������� 736

Accessing the DOM ������������������������������������������������������������������������������������������������������ 739

Setting the User-Agent HTTP Header ��������������������������������������������������������������������������� 739

Setting a User Style Sheet �������������������������������������������������������������������������������������������� 739

Styling a WebView with CSS ���������������������������������������������������������������������������������������� 740

Summary ���������������������������������������������������������������������������������������������������������������������� 740

Chapter 17: Understanding 2D Shapes ■ �������������������������������������������������������������� 741

What Are 2D Shapes? ��������������������������������������������������������������������������������������������������� 741

Drawing 2D Shapes ������������������������������������������������������������������������������������������������������ 743

Drawing Lines ������������������������������������������������������������������������������������������������������������������������������������� 743

Drawing Rectangles ��������������������������������������������������������������������������������������������������������������������������� 745

Drawing Circles ���������������������������������������������������������������������������������������������������������������������������������� 747

Drawing Ellipses ��������������������������������������������������������������������������������������������������������������������������������� 748

Drawing Polygons ������������������������������������������������������������������������������������������������������������������������������� 750

Page 23: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

xxiii

Drawing Polylines ������������������������������������������������������������������������������������������������������������������������������� 752

Drawing Arcs �������������������������������������������������������������������������������������������������������������������������������������� 755

Drawing Quadratic Curves ������������������������������������������������������������������������������������������������������������������ 757

Drawing Cubic Curves ������������������������������������������������������������������������������������������������������������������������ 759

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

The MoveTo Path Element ������������������������������������������������������������������������������������������������������������������ 762

The LineTo Path Element �������������������������������������������������������������������������������������������������������������������� 762

The HLineTo and VLineTo Path Elements �������������������������������������������������������������������������������������������� 765

The ArcTo Path Element ���������������������������������������������������������������������������������������������������������������������� 765

The QuadCurveTo Path Element ��������������������������������������������������������������������������������������������������������� 769

The CubicCurveTo Path Element ��������������������������������������������������������������������������������������������������������� 769

The ClosePath Path Element ��������������������������������������������������������������������������������������������������������������� 770

The Fill Rule for a Path ����������������������������������������������������������������������������������������������������������������������� 771

Drawing Scalable Vector Graphics ������������������������������������������������������������������������������� 774

The “moveTo” Command �������������������������������������������������������������������������������������������������������������������� 777

The “lineto” Commands ���������������������������������������������������������������������������������������������������������������������� 777

The “arcto” Command ������������������������������������������������������������������������������������������������������������������������ 778

The “Quadratic Bezier curveto” Command ����������������������������������������������������������������������������������������� 778

The “Cubic Bezier curveto” Command ����������������������������������������������������������������������������������������������� 779

The “closepath” Command ����������������������������������������������������������������������������������������������������������������� 780

Combining Shapes ������������������������������������������������������������������������������������������������������� 780

Understanding the Stroke of a Shape �������������������������������������������������������������������������� 782

Styling Shapes with CSS ���������������������������������������������������������������������������������������������� 786

Summary ���������������������������������������������������������������������������������������������������������������������� 787

Chapter 18: Understanding Text Nodes ■ ������������������������������������������������������������� 789

What Is a Text Node? ���������������������������������������������������������������������������������������������������� 789

Creating a Text Node ���������������������������������������������������������������������������������������������������� 790

Understanding the Text Origin �������������������������������������������������������������������������������������� 791

Displaying Multiline Text ����������������������������������������������������������������������������������������������� 793

Page 24: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

xxiv

Setting Text Fonts ��������������������������������������������������������������������������������������������������������� 795

Creating Fonts ������������������������������������������������������������������������������������������������������������������������������������ 795

Accessing Installed Fonts ������������������������������������������������������������������������������������������������������������������� 798

Using Custom Fonts ��������������������������������������������������������������������������������������������������������������������������� 799

Setting Text Fill and Stroke ������������������������������������������������������������������������������������������� 801

Applying Text Decorations �������������������������������������������������������������������������������������������� 802

Applying Font Smoothing ��������������������������������������������������������������������������������������������� 803

Styling a Text Node with CSS ��������������������������������������������������������������������������������������� 803

Summary ���������������������������������������������������������������������������������������������������������������������� 804

Chapter 19: Understanding 3D Shapes ■ �������������������������������������������������������������� 805

What Are 3D Shapes? ��������������������������������������������������������������������������������������������������� 805

Checking Support for 3D ���������������������������������������������������������������������������������������������� 806

The 3D Coordinate System ������������������������������������������������������������������������������������������� 807

Rendering Order of Nodes �������������������������������������������������������������������������������������������� 807

Using Predefined 3D Shapes ���������������������������������������������������������������������������������������� 810

Specifying the Shape Material ������������������������������������������������������������������������������������������������������������ 814

Specifying the Draw Mode of Shapes ������������������������������������������������������������������������������������������������� 817

Specifying the Face Culling for Shapes ���������������������������������������������������������������������������������������������� 819

Using Cameras ������������������������������������������������������������������������������������������������������������� 820

Using Light Sources ����������������������������������������������������������������������������������������������������� 823

Creating Subscenes ����������������������������������������������������������������������������������������������������� 824

Creating User-Defined Shapes ������������������������������������������������������������������������������������� 826

Creating a 3D Triangle ������������������������������������������������������������������������������������������������������������������������ 832

Creating a 3D Rectangle ��������������������������������������������������������������������������������������������������������������������� 835

Creating a Tetrahedron ����������������������������������������������������������������������������������������������������������������������� 836

Summary ���������������������������������������������������������������������������������������������������������������������� 839

Page 25: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

xxv

Chapter 20: Applying Effects ■ ����������������������������������������������������������������������������� 841

What Is an Effect? �������������������������������������������������������������������������������������������������������� 841

Chaining Effects ����������������������������������������������������������������������������������������������������������� 843

Shadowing Effects ������������������������������������������������������������������������������������������������������� 844

The DropShadow Effect ���������������������������������������������������������������������������������������������������������������������� 844

The InnerShadow Effect ��������������������������������������������������������������������������������������������������������������������� 849

The Shadow Effect ������������������������������������������������������������������������������������������������������������������������������ 852

Blurring Effects ������������������������������������������������������������������������������������������������������������ 854

The BoxBlur Effect ������������������������������������������������������������������������������������������������������������������������������ 854

The GaussianBlur Effect ��������������������������������������������������������������������������������������������������������������������� 856

The MotionBlur Effect ������������������������������������������������������������������������������������������������������������������������� 857

The Bloom Effect ���������������������������������������������������������������������������������������������������������� 859

The Glow Effect ������������������������������������������������������������������������������������������������������������ 860

The Reflection Effect ���������������������������������������������������������������������������������������������������� 861

The SepiaTone Effect ���������������������������������������������������������������������������������������������������� 862

The DisplacementMap Effect ��������������������������������������������������������������������������������������� 863

The ColorInput Effect ���������������������������������������������������������������������������������������������������� 869

The ColorAdjust Effect �������������������������������������������������������������������������������������������������� 870

The ImageInput Effect �������������������������������������������������������������������������������������������������� 872

The Blend Effect ����������������������������������������������������������������������������������������������������������� 874

The Lighting Effect ������������������������������������������������������������������������������������������������������� 877

Customizing the Surface Texture �������������������������������������������������������������������������������������������������������� 878

Understanding Reflection Types ��������������������������������������������������������������������������������������������������������� 881

Understanding the Light Source ��������������������������������������������������������������������������������������������������������� 884

The PerspectiveTransform Effect ��������������������������������������������������������������������������������� 895

Summary ���������������������������������������������������������������������������������������������������������������������� 897

Page 26: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

xxvi

Chapter 21: Understanding Transformations ■ ����������������������������������������������������� 899

What Is a Transformation? �������������������������������������������������������������������������������������������� 899

The Translation Transformation ������������������������������������������������������������������������������������ 901

The Rotation Transformation ���������������������������������������������������������������������������������������� 903

The Scale Transformation ��������������������������������������������������������������������������������������������� 907

The Shear Transformation �������������������������������������������������������������������������������������������� 911

Applying Multiple Transformations ������������������������������������������������������������������������������� 914

Summary ���������������������������������������������������������������������������������������������������������������������� 915

Chapter 22: Understanding Animation ■ �������������������������������������������������������������� 917

What Is Animation? ������������������������������������������������������������������������������������������������������ 917

Understating Animation Classes ����������������������������������������������������������������������������������� 918

Understanding Utility Classes ��������������������������������������������������������������������������������������� 919

Understanding the Duration Class ������������������������������������������������������������������������������������������������������ 919

Understating the KeyValue Class �������������������������������������������������������������������������������������������������������� 921

Understanding the KeyFrame Class ���������������������������������������������������������������������������������������������������� 922

Understating the Timeline Animation ��������������������������������������������������������������������������� 923

Controlling an Animation ��������������������������������������������������������������������������������������������� 926

Playing an Animation �������������������������������������������������������������������������������������������������������������������������� 926

Delaying the Start of an Animation ����������������������������������������������������������������������������������������������������� 927

Stopping an Animation ����������������������������������������������������������������������������������������������������������������������� 927

Pausing an Animation ������������������������������������������������������������������������������������������������������������������������� 927

Knowing the State of an Animation ���������������������������������������������������������������������������������������������������� 927

Looping an Animation ������������������������������������������������������������������������������������������������������������������������� 928

Auto Reversing an Animation ������������������������������������������������������������������������������������������������������������� 928

Attaching an onFinished Action ���������������������������������������������������������������������������������������������������������� 929

Knowing the Duration of an Animation ����������������������������������������������������������������������������������������������� 929

Adjusting the Speed of an Animation ������������������������������������������������������������������������������������������������� 929

Page 27: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

xxvii

Understanding Cue Points �������������������������������������������������������������������������������������������� 930

Understanding Transitions �������������������������������������������������������������������������������������������� 933

Understanding the Fade Transition ����������������������������������������������������������������������������������������������������� 933

Understanding the Fill Transition �������������������������������������������������������������������������������������������������������� 935

Understanding the Stroke Transition �������������������������������������������������������������������������������������������������� 936

Understanding the Translate Transition ���������������������������������������������������������������������������������������������� 937

Understanding the Rotate Transition �������������������������������������������������������������������������������������������������� 938

Understanding the Scale Transition ���������������������������������������������������������������������������������������������������� 940

Understanding the Path Transition ������������������������������������������������������������������������������������������������������ 941

Understanding the Pause Transition ��������������������������������������������������������������������������������������������������� 943

Understanding the Sequential Transition �������������������������������������������������������������������������������������������� 944

Understanding the Parallel Transition ������������������������������������������������������������������������������������������������� 946

Understanding Interpolators ����������������������������������������������������������������������������������������� 948

Understanding the Linear Interpolator ������������������������������������������������������������������������������������������������ 949

Understanding the Discrete Interpolator �������������������������������������������������������������������������������������������� 949

Understanding the Ease-In Interpolator ���������������������������������������������������������������������������������������������� 950

Understanding the Ease-Out Interpolator ������������������������������������������������������������������������������������������� 950

Understanding the Ease-Both Interpolator ����������������������������������������������������������������������������������������� 950

Understanding the Spline Interpolator ������������������������������������������������������������������������������������������������ 951

Understanding the Tangent Interpolator ��������������������������������������������������������������������������������������������� 951

Summary ���������������������������������������������������������������������������������������������������������������������� 951

Chapter 23: Understanding Charts ■ �������������������������������������������������������������������� 953

What Is a Chart? ����������������������������������������������������������������������������������������������������������� 953

Understating the Chart API ������������������������������������������������������������������������������������������� 953

Styling Charts with CSS������������������������������������������������������������������������������������������������ 954

Data Used in Chart Examples ��������������������������������������������������������������������������������������� 956

Understanding the PieChart ����������������������������������������������������������������������������������������� 956

Customizing Pie Slices ����������������������������������������������������������������������������������������������������������������������� 959

Styling the PieChart with CSS ������������������������������������������������������������������������������������������������������������� 961

Page 28: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

xxviii

Understating the XYChart ��������������������������������������������������������������������������������������������� 967

Representing Axes in an XYChart ������������������������������������������������������������������������������������������������������� 967

Adding Data to an XYChart ����������������������������������������������������������������������������������������������������������������� 968

Understating the BarChart �������������������������������������������������������������������������������������������� 970

Styling the BarChart with CSS ������������������������������������������������������������������������������������������������������������ 975

Understating the StackedBarChart ������������������������������������������������������������������������������� 976

Styling the StackedBarChart with CSS ����������������������������������������������������������������������������������������������� 978

Understanding the ScatterChart ����������������������������������������������������������������������������������� 978

Styling the ScatterChart with CSS ������������������������������������������������������������������������������������������������������ 980

Understanding the LineChart ���������������������������������������������������������������������������������������� 981

Styling the LineChart with CSS ����������������������������������������������������������������������������������������������������������� 983

Understating the BubbleChart �������������������������������������������������������������������������������������� 983

Styling the BubbleChart with CSS ������������������������������������������������������������������������������������������������������ 986

Understating the AreaChart ������������������������������������������������������������������������������������������ 986

Styling the AreaChart with CSS ���������������������������������������������������������������������������������������������������������� 988

Understanding the StackedAreaChart �������������������������������������������������������������������������� 990

Styling the StackedAreaChart with CSS ��������������������������������������������������������������������������������������������� 991

Customizing XYChart Appearance �������������������������������������������������������������������������������� 991

Setting Alternate Row/Column Fill ������������������������������������������������������������������������������������������������������ 992

Showing Zero Line Axes ��������������������������������������������������������������������������������������������������������������������� 992

Showing Grid Lines ����������������������������������������������������������������������������������������������������������������������������� 993

Formatting Numeric Tick Labels ��������������������������������������������������������������������������������������������������������� 993

Summary ���������������������������������������������������������������������������������������������������������������������� 995

Chapter 24: Understanding the Image API ■ �������������������������������������������������������� 997

What Is the Image API? ������������������������������������������������������������������������������������������������ 997

Loading an Image ��������������������������������������������������������������������������������������������������������� 998

Specifying the Image-Loading Properties ������������������������������������������������������������������������������������������ 999

Reading the Loaded-Image Properties ����������������������������������������������������������������������������������������������� 999

Page 29: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

xxix

Viewing an Image ������������������������������������������������������������������������������������������������������� 1000

Multiple Views of an Image �������������������������������������������������������������������������������������������������������������� 1002

Viewing an Image in a Viewport ������������������������������������������������������������������������������������������������������� 1004

Understanding Image Operations ������������������������������������������������������������������������������� 1008

Pixel Formats ������������������������������������������������������������������������������������������������������������������������������������ 1008

Reading Pixels from an Image ���������������������������������������������������������������������������������������������������������� 1009

Writing Pixels to an Image ���������������������������������������������������������������������������������������������������������������� 1015

Creating an Image from Scratch ������������������������������������������������������������������������������������������������������� 1019

Saving a New Image to a FileSystem ����������������������������������������������������������������������������������������������� 1023

Taking the Snapshot of a Node and a Scene �������������������������������������������������������������� 1026

Taking the Snapshot of a Node ��������������������������������������������������������������������������������������������������������� 1026

Taking the Snapshot of a Scene ������������������������������������������������������������������������������������������������������� 1029

Summary �������������������������������������������������������������������������������������������������������������������� 1031

Chapter 25: Drawing on a Canvas ■ ������������������������������������������������������������������� 1033

What Is the Canvas API? ��������������������������������������������������������������������������������������������� 1033

Creating a Canvas ������������������������������������������������������������������������������������������������������ 1033

Drawing on the Canvas ���������������������������������������������������������������������������������������������� 1034

Drawing Basic Shapes ���������������������������������������������������������������������������������������������������������������������� 1034

Drawing Text ������������������������������������������������������������������������������������������������������������������������������������� 1035

Drawing Paths ���������������������������������������������������������������������������������������������������������������������������������� 1036

Drawing Images �������������������������������������������������������������������������������������������������������������������������������� 1037

Writing Pixels ������������������������������������������������������������������������������������������������������������������������������������ 1037

Clearing the Canvas Area ������������������������������������������������������������������������������������������� 1037

Saving and Restoring the Drawing States ������������������������������������������������������������������ 1038

A Canvas Drawing Example ���������������������������������������������������������������������������������������� 1039

Summary �������������������������������������������������������������������������������������������������������������������� 1041

Page 30: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

xxx

Chapter 26: Understanding Drag and Drop ■ ������������������������������������������������������ 1043

What Is a Press-Drag-Release Gesture? �������������������������������������������������������������������� 1043

A Simple Press-Drag-Release Gesture ��������������������������������������������������������������������������������������������� 1044

A Full Press-Drag-Release Gesture �������������������������������������������������������������������������������������������������� 1046

A Drag-and-Drop Gesture ����������������������������������������������������������������������������������������������������������������� 1048

Understanding the Data Transfer Modes �������������������������������������������������������������������� 1049

Understanding the Dragboard ������������������������������������������������������������������������������������ 1050

The Example Application �������������������������������������������������������������������������������������������� 1053

Initiating the Drag-and-Drop Gesture ����������������������������������������������������������������������������������������������� 1053

Detecting a Drag Gesture ����������������������������������������������������������������������������������������������������������������� 1054

Dropping the Source onto the Target ������������������������������������������������������������������������������������������������ 1055

Completing the Drag-and-Drop Gesture ������������������������������������������������������������������������������������������� 1055

Providing Visual Clues ������������������������������������������������������������������������������������������������ 1056

A Complete Drag-and-Drop Example ������������������������������������������������������������������������� 1056

Transferring an Image ������������������������������������������������������������������������������������������������ 1059

Transferring Custom Data Types ��������������������������������������������������������������������������������� 1063

Summary �������������������������������������������������������������������������������������������������������������������� 1069

Chapter 27: Understanding Concurrency in JavaFX ■ ���������������������������������������� 1071

The Need for a Concurrency Framework �������������������������������������������������������������������� 1071

Understating the Concurrent Framework API ������������������������������������������������������������� 1078

Understanding the Worker<V> Interface ������������������������������������������������������������������� 1078

State Transitions for a Worker ���������������������������������������������������������������������������������������������������������� 1079

Properties of a Worker ���������������������������������������������������������������������������������������������������������������������� 1079

Utility Classes for Examples ������������������������������������������������������������������������������������������������������������� 1080

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

Creating a Task ��������������������������������������������������������������������������������������������������������������������������������� 1083

Updating Task Properties ������������������������������������������������������������������������������������������������������������������ 1083

Listening to Task Transition Events ��������������������������������������������������������������������������������������������������� 1084

Cancelling a Task ������������������������������������������������������������������������������������������������������������������������������ 1084

Page 31: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

xxxi

Running a Task ��������������������������������������������������������������������������������������������������������������������������������� 1084

A Prime Finder Task Example ����������������������������������������������������������������������������������������������������������� 1085

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

Creating the Service ������������������������������������������������������������������������������������������������������������������������� 1089

Updating Service Properties ������������������������������������������������������������������������������������������������������������� 1090

Listening to Service Transition Events ���������������������������������������������������������������������������������������������� 1090

Cancelling the Service ���������������������������������������������������������������������������������������������������������������������� 1090

Starting the Service �������������������������������������������������������������������������������������������������������������������������� 1090

Resetting the Service ����������������������������������������������������������������������������������������������������������������������� 1090

Restarting the Service ���������������������������������������������������������������������������������������������������������������������� 1090

The Prime Finder Service Example ��������������������������������������������������������������������������������������������������� 1091

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

Creating the ScheduledService �������������������������������������������������������������������������������������������������������� 1093

Updating ScheduledService Properties �������������������������������������������������������������������������������������������� 1094

Listening to ScheduledService Transition Events ����������������������������������������������������������������������������� 1095

The Prime Finder ScheduledService Example ���������������������������������������������������������������������������������� 1095

Summary �������������������������������������������������������������������������������������������������������������������� 1100

Chapter 28: Playing Audios and Videos ■ ����������������������������������������������������������� 1101

Understanding the Media API ������������������������������������������������������������������������������������� 1101

Playing Short Audio Clips ������������������������������������������������������������������������������������������� 1102

Playing Media ������������������������������������������������������������������������������������������������������������� 1105

Creating a Media Object ������������������������������������������������������������������������������������������������������������������� 1105

Creating a MediaPlayer Object ��������������������������������������������������������������������������������������������������������� 1106

Creating a MediaView Node ������������������������������������������������������������������������������������������������������������� 1106

Combining Media, MediaPlayer, and MediaView ������������������������������������������������������������������������������ 1107

A Media Player Example ��������������������������������������������������������������������������������������������� 1107

Handling Playback Errors ������������������������������������������������������������������������������������������� 1110

State Transitions of the MediaPlayer �������������������������������������������������������������������������� 1111

Repeating Media Playback ����������������������������������������������������������������������������������������� 1114

Tracking Media Time �������������������������������������������������������������������������������������������������� 1114

Page 32: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

xxxii

Controlling the Playback Rate ������������������������������������������������������������������������������������ 1115

Controlling the Playback Volume �������������������������������������������������������������������������������� 1115

Positioning the MediaPlayer ��������������������������������������������������������������������������������������� 1116

Marking Positions in the Media ���������������������������������������������������������������������������������� 1116

Showing Media Metadata ������������������������������������������������������������������������������������������� 1117

Customizing the MediaView ��������������������������������������������������������������������������������������� 1118

Developing a Media Player Application ���������������������������������������������������������������������� 1118

Summary �������������������������������������������������������������������������������������������������������������������� 1119

Chapter 29: Understanding FXML ■ �������������������������������������������������������������������� 1120

What Is FXML? ������������������������������������������������������������������������������������������������������������ 1120

Editing FXML Documents ������������������������������������������������������������������������������������������� 1121

FXML Basics ��������������������������������������������������������������������������������������������������������������� 1121

Creating the FXML File ��������������������������������������������������������������������������������������������������������������������� 1123

Adding UI Elements �������������������������������������������������������������������������������������������������������������������������� 1123

Importing Java Types in FXML ���������������������������������������������������������������������������������������������������������� 1124

Setting Properties in FXML ��������������������������������������������������������������������������������������������������������������� 1125

Specifying FXML Namespace ����������������������������������������������������������������������������������������������������������� 1127

Assigning an Identifier to an Object �������������������������������������������������������������������������������������������������� 1128

Adding Event Handlers ��������������������������������������������������������������������������������������������������������������������� 1128

Loading FXML Documents ���������������������������������������������������������������������������������������������������������������� 1130

Using a Controller in FXML ��������������������������������������������������������������������������������������������������������������� 1132

Creating Objects in FXML ������������������������������������������������������������������������������������������� 1136

Using the no-args Constructor���������������������������������������������������������������������������������������������������������� 1137

Using the static valueOf() Method����������������������������������������������������������������������������������������������������� 1137

Using a Factory Method �������������������������������������������������������������������������������������������������������������������� 1138

Using Builders ���������������������������������������������������������������������������������������������������������������������������������� 1139

Creating Reusable Objects in FXML ��������������������������������������������������������������������������� 1144

Specifying Locations in Attributes ������������������������������������������������������������������������������ 1145

Using Resource Bundles �������������������������������������������������������������������������������������������� 1145

Including FXML Files �������������������������������������������������������������������������������������������������� 1147

Page 33: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ Contents

xxxiii

Using Constants ��������������������������������������������������������������������������������������������������������� 1150

Referencing Another Element ������������������������������������������������������������������������������������� 1151

Copying Elements ������������������������������������������������������������������������������������������������������� 1151

Binding Properties in FXML ���������������������������������������������������������������������������������������� 1152

Creating Custom Controls������������������������������������������������������������������������������������������� 1153

Summary �������������������������������������������������������������������������������������������������������������������� 1155

Chapter 30: Understanding the Print API ■ �������������������������������������������������������� 1157

What is the Printing API? �������������������������������������������������������������������������������������������� 1157

Listing Available Printers �������������������������������������������������������������������������������������������� 1158

Getting the Default Printer ������������������������������������������������������������������������������������������ 1159

Printing Nodes ������������������������������������������������������������������������������������������������������������ 1159

Showing the Page Setup and Print Dialogs ���������������������������������������������������������������� 1163

Customizing PrinterJob Settings �������������������������������������������������������������������������������� 1166

Setting Page Layout ��������������������������������������������������������������������������������������������������� 1168

Printing a Webpage ���������������������������������������������������������������������������������������������������� 1169

Summary �������������������������������������������������������������������������������������������������������������������� 1171

Index ������������������������������������������������������������������������������������������������������������������� 1173

Page 34: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan
Page 35: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

xxxv

About the Author

Kishori Sharan works as a software architect at Up and Running, Inc. He has earned a master’s of science degree in computer information systems from Troy State University, Alabama. He is a Sun-certified Java 2 programmer. He has over 18 years of experience in developing enterprise applications and providing training to professional developers in Java platform.

Page 36: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan
Page 37: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

xxxvii

About the Technical Reviewers

Jeff Friesen is a freelance tutor and software developer with an emphasis on Java (and now Android). In addition to authoring Learn Java for Android Development and coauthoring Android Recipes, Jeff has written numerous articles on Java and other technologies for JavaWorld (http://www.JavaWorld.com), informIT (http://www.InformIT.com), Java.net (http://www.java.net), and DevSource (http://www.DevSource.com). Jeff can be contacted via his web site at http://www.tutortutor.ca.

David Coffin is an IT analyst working at the Savannah River Site, a large Department of Energy facility. For more than 30 years, his expertise has been in multiplatform network integration and systems programming. Before coming to the Savannah River Site, he worked for several defense contractors and served as the technical lead for office and network computing at the National Aerospace Plane Joint Program Office at Wright-Patterson Air Force Base in Ohio. As a perpetual student, he has one master’s degree and has begun several others. As a family man, he has raised eight children. He is a triathlete who competes in the middle of the pack. He is also a classical guitar player, but he’s not quitting his day job.

Page 38: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ about the teChniCal RevieweRs

xxxviii

Wallace Jackson has been writing for leading multimedia publications about his work in new media content development since the advent of Multimedia Producer Magazine nearly two decades ago. He has authored a half-dozen Android book titles for Apress, including four titles in the popular Pro Android series. Wallace received his undergraduate degree in business economics from the University of California at Los Angeles and a graduate degree in MIS design and implementation from the University of Southern California. He is currently the CEO of Mind Taffy Design, a new media content production and digital campaign design and development agency.

Massimo Nardone holds a master’s of science degree in computing science from the University of Salerno, Italy. He worked as a PCI QSA and senior lead IT security/cloud/SCADA architect for many years and currently works as security, cloud, and SCADA lead IT architect for Hewlett-Packard in Finland. He has more than 20 years of work experience in IT, including security, SCADA, cloud computing, IT infrastructure, mobile, and WWW technology areas for both national and international projects. He has worked as a project manager, cloud/SCADA lead IT architect, software engineer, research engineer, chief security architect, and software specialist. He worked as a visiting lecturer and supervisor for exercises at the networking laboratory of the Helsinki University of Technology (Aalto University). He has been programming and teaching how to program with Perl, PHP, Java, VB, Python, C/C++, and MySQL for more than 20 years. He is the author of Beginning PHP and MySQL (Apress, 2014) and Pro Android Games (Apress, 2015).

Tri Phan is the founder of the Programming Learning Channel on YouTube. He has over seven years of experience in the software industry. Specifically, he has worked in many outsourcing companies and has written many applications of many fields in different programming languages such as PHP, Java, and C#. In addition, he has over six years of experience in teaching at international and technological centers such as Aptech, NIIT, and Kent College.

Page 39: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

xxxix

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 web site at 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 of her support for 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 for 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 publication of this book.

Page 40: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan
Page 41: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

xli

Introduction

Java had the support for developing GUI applications since its version 1.0 using the AWT (Abstract Windows 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 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 8 is supported in the Java platform by including the JavaFX runtime along with the Java runtime in the JRE. Now JavaFX 8 is considered a real successor for Swing for building the GUI application using the Java platform.

Learn JavaFX 8 shows you how to start developing rich-client desktop applications in JavaFX 8 using your Java skills. It provides comprehensive coverage of the JavaFX 8 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. Some of the advanced controls such as TableView, TreeTableView, and WebView are covered in chapters of their own.

I faced few hurdles while writing 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 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 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 330 ready-to-run programs and 430 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.

Page 42: Learn JavaFX 8 - Home - Springer978-1-4842-1142-7/1.pdf · Learn JavaFX 8 Building User Experience and Interfaces with Java 8 Kishori Sharan

■ intRoduCtion

xlii

Structure of the BookThe book contains 30 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 LearnThis book will help you to learn:

What JavaFX 8 is and its history•

How to develop rich-client desktop applications using JavaFX 8•

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 TreeTableViev.•

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 8 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 8 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 8.

Source code for this book may be downloaded from www.apress.com/9781484211434; errata can be submitted and viewed via the same link.

Please direct all your questions and comments for the author to [email protected].