JFXMG-Mastering FXML
JavaFX
Mastering FXML
Release 8
E50650-01
March 2014
In this tutorial, you learn the reasons you should use FXML,
get information about the FXML enhancements made in
JavaFX releases 2.1 and 2.2; create the sample Address Book
application using FXML...
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.
FXML and Scene Builder
Why Use FXML 1-3
■ 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:
If the locale is changed to fr_FR and the FXML file is reloaded, then the label
shows "Prénom."
The same is not true for Java code, because you must manually update the content
of every element of your user interface by obtaining a reference to it and calling
the appropriate setter (such as setText()).
■ You can use FXML with any Java Virtual Machine (JVM) language, such as Java,
Scala, or Clojure.
■ 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 the
Getting Started with JavaFX Scene Builder document.
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.4 or later 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.
FXML and Scene Builder
1-4 JavaFX Mastering FXML
2
FXML Enhancements in JavaFX Releases 2.1 and 2.2 2-1
2FXML Enhancements in JavaFX Releases 2.1
and 2.2
This chapter contains the following sections that describe the FXML enhancements
made in JavaFX releases 2.1 and 2.2. Also covered are the incompatibilities with
previous releases:
■ FXML Enhancements Made in JavaFX 2.1 and 2.2
■ FXML Loader Incompatibilities with Previous JavaFX Releases
FXML Enhancements Made in JavaFX 2.1 and 2.2
The following FXML enhancements have been added in JavaFX 2.1 and JavaFX 2.2:
■ 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 dependency injection system such as
Google Guice 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);
}
FXML Enhancements Made in JavaFX 2.1 and 2.2
2-2 JavaFX Mastering FXML
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
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 fi