JavaFX Mastering FXML Release 2.2 E20478-09

January 2014

JavaFX/Mastering FXML, Release 2.2 E20478-09

Copyright © 2011, 2014 Oracle and/or its affiliates. All rights reserved.

Primary Author: Irina Fedortsova Contributing Author:

Contributor: Greg Brown

This and related documentation are provided under a license agreement containing restrictions on use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means. Reverse engineering, disassembly, or decompilation of this software, unless required by law for interoperability, is prohibited. The information contained herein is subject to change without notice and is not warranted to be error-free. If you find any errors, please report them to us in writing.

If this is software or related documentation that is delivered to the U.S. Government or anyone licensing it on behalf of the U.S. Government, the following notice is applicable:

U.S. GOVERNMENT RIGHTS Programs, software, databases, and related documentation and technical data delivered to U.S. Government customers are "commercial computer software" or "commercial technical data" pursuant to the applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As such, the use, duplication, disclosure, modification, and adaptation shall be subject to the restrictions and license terms set forth in the applicable Government contract, and, to the extent applicable by the terms of the Government contract, the additional rights set forth in FAR 52.227-19, Commercial Computer Software License (December 2007). Oracle America, Inc., 500 Oracle Parkway, Redwood City, CA 94065.

This software or hardware is developed for general use in a variety of information management applications. It is not developed or intended for use in any inherently dangerous applications, including applications that may create a risk of personal injury. If you use this software or hardware in dangerous applications, then you shall be responsible to take all appropriate fail-safe, backup, redundancy, and other measures to ensure its safe use. and its affiliates disclaim any liability for any damages caused by use of this software or hardware in dangerous applications. Oracle and are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.

Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are used under license and are trademarks or registered trademarks of SPARC International, Inc. AMD, Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced Micro Devices. is a registered trademark of The Open Group.

This software or hardware and documentation may provide access to or information on content, products, and services from third parties. Oracle Corporation and its affiliates are not responsible for and expressly disclaim all warranties of any kind with respect to third-party content, products, and services. Oracle Corporation and its affiliates will not be responsible for any loss, costs, or damages incurred due to your access to or use of third-party content, products, or services. Contents

1 Why Use FXML Introduction to FXML...... 1-1 Simple Example of FXML...... 1-2 Benefits of FXML...... 1-2 FXML and Scene Builder ...... 1-3

2 FXML—What’s New in JavaFX 2.1 FXML Enhancements for JavaFX 2.1...... 2-1 FXML Loader Incompatibilities with Previous JavaFX Releases ...... 2-2 Some JavaFX 2.0 FXML Escape Sequences Are Deprecated in JavaFX 2.1 ...... 2-2 Backslash Is Now an Escape Character ...... 2-2

3 FXML—What’s New in JavaFX 2.2

4 Creating an Address Book with FXML Set Up the Project ...... 4-1 Create the Basic ...... 4-2 Add Columns to the Table...... 4-4 Define the Data Model...... 4-4 Associate Data with the Table Columns...... 4-5 Set Sort Order on Startup ...... 4-7 Define Column Widths ...... 4-8 Set Alignment in Table Cells ...... 4-9 Add Rows to the Table ...... 4-11 Where to Go from Here...... 4-12

5 Creating a Custom Control with FXML Set Up a Project...... 5-1 Create the Basic User Interface ...... 5-2 Create a Controller ...... 5-2 Load the FXML Source File and Define Stage and Scene ...... 5-3

6 Deployment of FXML Applications

iii iv Part I

Part I About This Tutorial

This document consists of the following pages:

■ Why Use FXML A basic description of FXML and the benefits of using it to create user interfaces.

■ FXML—What’s New in JavaFX 2.1 A list of FXML enhancements in JavaFX 2.1 and incompatibilities with previous releases.

■ FXML—What’s New in JavaFX 2.2 A list of FXML enhancements in JavaFX 2.2.

■ Creating an Address Book with FXML A tutorial that shows how to populate a table with data, sort the data at application startup, align the data in the table cells, and add rows to the table.

■ Creating a Custom Control with FXML A tutorial that shows how to create a custom control using APIs introduced in JavaFX 2.2.

■ Deployment of FXML Applications A clarification about why some FXML applications need digital signatures. An alternative to signing the application is also presented. You can also get information on FXML from the following resources:

■ Creating a User Interface with FXML A beginning tutorial that shows how to create a login application using FXML.

■ Introduction to FXML A reference document that provides information on the elements that make up the FXML language. The document is included in the .fxml package in the API documentation.

■ JavaFX 2 Forum A place where you can post questions about FXML.

1

Why1 Use FXML

This tutorial provides a basic description of FXML and the benefits of using it to create user interfaces. FXML is an XML-based language that provides the structure for building a user interface separate from the application logic of your code. This separation of the presentation and application logic is attractive to web developers because they can assemble a user interface that leverages Java components without mastering the code for fetching and filling in the data. The following sections provide more information about FXML, and when you would choose FXML over other methods of creating a user interface:

■ Introduction to FXML

■ Simple Example of FXML

■ Benefits of FXML

■ FXML and Scene Builder

Introduction to FXML FXML does not have a schema, but it does have a basic predefined structure. What you can express in FXML, and how it applies to constructing a scene graph, depends on the API of the objects you are constructing. Because FXML maps directly to Java, you can use the API documentation to understand what elements and attributes are allowed. In general, most JavaFX classes can be used as elements, and most Bean properties can be used as attributes. From a Model View Controller (MVC) perspective, the FXML file that contains the description of the user interface is the view. The controller is a Java class, optionally implementing the Initializable class, which is declared as the controller for the FXML file. The model consists of domain objects, defined on the Java side, that you connect to the view through the controller. An example of this structure is in the tutorial Creating an Address Book with FXML. While you can use FXML to create any user interface, FXML is particularly useful for user interfaces that have large, complex scene graphs, forms, data entry, or complex animation. FXML is also well-suited for defining static layouts such as forms, controls, and tables. In addition, you can use FXML to construct dynamic layouts by including scripts.

Why Use FXML 1-1 Simple Example of FXML

Simple Example of FXML The easiest way to show the advantages of FXML is with an example. Take a look at Figure 1–1, which shows a user interface that includes a border pane layout that has a top and center region, each of which contains a label.

Figure 1–1 Border Pane Simple Example

First, look at how the user interface is constructed and built directly in the source code, as shown in Example 1–1.

Example 1–1 Java Code for a User Interface BorderPane border = new BorderPane(); Label toppanetext = new Label("Page Title"); border.setTop(toppanetext); Label centerpanetext = new Label ("Some data here"); border.setCenter(centerpanetext);

Next, look at Example 1–2, which shows the same user interface, but in FXML markup. You can see the hierarchical structure of the user interface, which in turn makes it easier to add components and build upon the user interface.

Example 1–2 FXML Markup for a User Interface

Benefits of FXML In addition to providing web developers a familiar approach to designing user interfaces, FXML offers these benefits:

■ Because the scene graph is more transparent in FXML, it is easy for a development team to create and maintain a testable user interface.

■ FXML is not a compiled language; you do not need to recompile the code to see the changes.

1-2 Mastering FXML FXML and Scene Builder

■ The content of an FXML file can be localized as the file is read. For example, if an FXML file is loaded using the en_US locale, then it produces the string "First Name" for a label based on the following resource string:

■ You can use FXML with any (JVM) language, such as Java, Scala, or .

■ FXML is not limited to the view portion of the MVC interface. You can construct services or tasks or domain objects, and you can use JavaScript or other scripting languages in FXML. For an example of using JavaScript, see Use a Scripting Language to Handle Events in the FXML tutorial of the Getting Started guide.

FXML and Scene Builder Just as some developers prefer to work directly in the XML code, other developers prefer to use a tool to author their XML. The same is true with FXML. If you prefer to use a tool, or if you want to create a quick prototype to get feedback, then you might prefer to use JavaFX Scene Builder. Scene Builder is a design tool that generates the FXML source code as you define the user interface for your application. Scene Builder can help you to quickly create a prototype for an interactive application that connects components to the application logic. For more information, see Getting Started with JavaFX Scene Builder. Because Scene Builder uses XML as a serialization format, the produced FXML code is very clear and you can further edit FXML files, generated by Scene Builder, in any text or XML editor. NetBeans IDE 7.2 enables you to open FXML files in JavaFX Scene Builder, provided that the latter is installed on your computer. This tighter integration of NetBeans and Scene Builder gives an additional advantage when developing FXML applications.

Why Use FXML 1-3 FXML and Scene Builder

1-4 Mastering FXML 2

FXML—What’s2 New in JavaFX 2.1

This page contains the following sections that describe the FXML enhancements in JavaFX 2.1 and incompatibilities with previous releases:

■ FXML Enhancements for JavaFX 2.1

■ FXML Loader Incompatibilities with Previous JavaFX Releases

FXML Enhancements for JavaFX 2.1 The following FXML enhancements have been added in JavaFX 2.1:

■ Support for using a leading backslash as an escape character (RT-18680) JavaFX 2.0 used consecutive operator characters such as $$ as escape sequences. JavaFX 2.1 adds support for escape sequences using the backslash character, such as \$. These escape sequences are more similar to Unified Expression Language (UEL), making them more familiar to developers. The JavaFX 2.0 escape sequences are deprecated as of JavaFX 2.1. See Some JavaFX 2.0 FXML Escape Sequences Are Deprecated in JavaFX 2.1 and Backslash Is Now an Escape Character.

■ An implicit variable for the controller to document the namespace This new feature facilitates bidirectional binding between the controller and the UI. Bidirectional binding was dropped from JavaFX 2.1, but this feature was retained.

■ Convenience constructors to the FXMLLoader class (RT-16815) Several new convenience constructors have been added to the FXMLLoader class. These constructors mirror the static load() methods defined in JavaFX 2.0, but make it easier to access the document's controller from the calling code.

■ Customizable controller instantiation (RT-16724, RT-17268) In JavaFX 2.0, the calling code did not have any control over controller creation. This prevented an application from using a system such as or the Spring Framework to manage controller initialization. JavaFX 2.1 adds a Callback interface to facilitate delegation of controller construction: public interface Callback { public Object getController(Class type); }

When a controller factory is provided to the FXMLLoader object, the loader will delegate controller construction to the factory. An implementation might return a null value to indicate that it does not or cannot create a controller of the given type; in this case, the default controller construction mechanism will be employed

FXML—What’s New in JavaFX 2.1 2-1 FXML Loader Incompatibilities with Previous JavaFX Releases

by the loader. Implementations might also "recycle" controllers such that controller instances can be shared by multiple FXML documents. However, developers must be aware of the implications of doing this: primarily, that controller field injection should not be used in this case because it will result in the controller fields containing values from only the most recently loaded document.

■ Easier style sheets to work with (RT-18299, RT-15524) In JavaFX 2.0, applying style sheets in FXML was not very convenient. In JavaFX 2.1, it is much simpler. Style sheets can be specified as an attribute of a root element as follows: Style classes on individual nodes can now be applied as follows:

■ Caller-specified no-arg controller method as an event handler (RT-18229) In JavaFX 2.0, controller-based event handlers must adhere to the method signature defined by an event handler. They must accept a single argument of a type that extends the Event class and return void. In JavaFX 2.1, the argument restriction has been lifted, and it is now possible to write a controller event handler that takes no arguments.

FXML Loader Incompatibilities with Previous JavaFX Releases The following sections contain compatibility issues that users might encounter if they load a JavaFX 2.0 FXML file with a JavaFX 2.1 FXML loader:

■ Some JavaFX 2.0 FXML Escape Sequences Are Deprecated in JavaFX 2.1

■ Backslash Is Now an Escape Character

Some JavaFX 2.0 FXML Escape Sequences Are Deprecated in JavaFX 2.1 Table 2–1 shows the double-character escape sequences that were used in FXML in JavaFX 2.0, but are deprecated in JavaFX 2.1. Instead, use a backslash as the escape character.

Table 2–1 Deprecated and Current Escape Sequences JavaFX 2.0 Escape Sequence JavaFX 2.1 Escape Sequence $$ \$ %% \% @@ \@

If Scene Builder encounters any of these deprecated escape sequences, then the console displays a warning, but loads the FXML anyway. The next time the file is saved, Scene Builder automatically replaces the deprecated escape characters with the new syntax.

Backslash Is Now an Escape Character In JavaFX 2.1, the backslash \ is an escape character in FXML. As a result, JavaFX 2.0 applications with FXML files that contain FXML string attributes starting with a backslash might prevent the FXML from loading, or it might cause the FXML loader to misinterpret the string.

2-2 Mastering FXML FXML Loader Incompatibilities with Previous JavaFX Releases

Solution: For any FXML backslash text in a JavaFX 2.0 application, add an additional backslash to escape the character. Example: Remove this line of code: