1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   2 
   3 <!--
   4 /*
   5  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.  Oracle designates this
  11  * particular file as subject to the "Classpath" exception as provided
  12  * by Oracle in the LICENSE file that accompanied this code.
  13  *
  14  * This code is distributed in the hope that it will be useful, but WITHOUT
  15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  17  * version 2 for more details (a copy is included in the LICENSE file that
  18  * accompanied this code).
  19  *
  20  * You should have received a copy of the GNU General Public License version
  21  * 2 along with this work; if not, write to the Free Software Foundation,
  22  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  23  *
  24  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  25  * or visit www.oracle.com if you need additional information or have any
  26  * questions.
  27  */
  28 -->
  29 
  30 <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">    
  31 <head>
  32 <link href="fxml.css" rel="stylesheet"/>    
  33 <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
  34 <title>Introduction to FXML | JavaFX 9</title>
  35 <meta name="description" content="The document introduces FXML, an XML-based declarative markup language for defining user interfaces in JavaFX 9 applications."/>
  36 <meta name="keywords" content="JavaFX 9, FXML, JavaFX GUI development, web development, Java application development, GUI applications, rich internet applications, RIA, expressive content"/>
  37 </head>
  38 <body>
  39 
  40 <div class="fx-code-header">
  41 <div class="version"><br/>Release: JavaFX 9</div>
  42 </div>
  43 
  44 <h1>Introduction to FXML</h1>
  45 <p class="subtitle">Last updated: 01 May 2017</p>
  46 
  47 <h2>Contents</h2>
  48 <ul class="contents">
  49 <li><a href="#overview">Overview</a></li>
  50 <li>
  51     <a href="#elements">Elements</a>
  52     <ul>
  53     <li>
  54         <a href="#class_instance_elements">Class Instance Elements</a>
  55         <ul>
  56         <li><a href="#instance_declaration_elements">Instance Declarations</a></li>
  57         <li><a href="#include_elements">&lt;fx:include&gt;</a></li>
  58         <li><a href="#constant_elements">&lt;fx:constant&gt;</a></li>
  59         <li><a href="#reference_elements">&lt;fx:reference&gt;</a></li>
  60         <li><a href="#copy_elements">&lt;fx:copy&gt;</a></li>
  61         <li><a href="#root_elements">&lt;fx:root&gt;</a></li>
  62         </ul>
  63     </li>
  64 
  65     <li>
  66         <a href="#property_elements">Property Elements</a>
  67         <ul>
  68         <li><a href="#property_setter_elements">Property Setters</a></li>
  69         <li><a href="#read_only_list_property_elements">Read-Only List Properties</a></li>
  70         <li><a href="#read_only_map_property_elements">Read-Only Map Properties</a></li>
  71         <li><a href="#default_properties">Default Properties</a></li>
  72         </ul>
  73     </li>
  74 
  75     <li><a href="#static_property_elements">Static Properties</a></li>
  76     <li><a href="#define_elements">Define Blocks</a></li>
  77     </ul>
  78 </li>
  79 <li>
  80     <a href="#attributes">Attributes</a>
  81     <ul>
  82     <li>
  83         <a href="#instance_property_attributes">Instance Properties</a>
  84         <ul>
  85         <li><a href="#location_resolution">Location Resolution</a></li>
  86         <li><a href="#resource_resolution">Resource Resolution</a></li>
  87         <li><a href="#variable_resolution">Variable Resolution</a></li>
  88         <li><a href="#escape_sequences">Escape Sequences</a></li>
  89         <li><a href="#expression_binding">Expression Binding</a></li>
  90         </ul>
  91     </li>
  92 
  93     <li><a href="#static_property_attributes">Static Properties</a></li>
  94     <li>
  95         <a href="#event_handler_attributes">Event Handlers</a>
  96         <ul>
  97         <li><a href="#script_event_handlers">Script Event Handlers</a></li>
  98         <li><a href="#controller_method_event_handlers">Controller Method Event Handlers</a></li>
  99         <li><a href="#expression_handlers">Event handlers from expressions</a></li>
 100         <li><a href="#collections_and_property_handlers">Special handlers for collections and properties</a></li>
 101         </ul>
 102     </li>
 103     </ul>
 104 </li>
 105 <li><a href="#scripting">Scripting</a></li>
 106 <li>
 107     <a href="#controllers">Controllers</a>
 108     <ul>
 109     <li><a href="#fxml_annotation">@FXML</a></li>
 110     <li><a href="#nested_controllers">Nested Controllers</a></li>
 111     </ul>
 112 </li>
 113 <li>
 114     <a href="#fxmlloader">FXMLLoader</a>
 115     <ul>
 116     <li><a href="#custom_components">Custom Components</a></li>
 117     </ul>
 118 </li>
 119 <li><a href="#deploy_as_module">Deploying an Application as a Module</a></li>
 120 </ul>
 121 
 122 <h2><a name="overview">Overview</a></h2>
 123 <p>FXML is a scriptable, XML-based markup language for constructing Java object graphs. It provides a convenient alternative to constructing such graphs in procedural code, and is ideally suited to defining the user interface of a JavaFX application, since the hierarchical structure of an XML document closely parallels the structure of the JavaFX scene graph.</p>
 124 
 125 <p>This document introduces the FXML markup language and explains how it can be used to simplify development of JavaFX applications.</p>
 126 
 127 <h2><a name="elements">Elements</a></h2>
 128 <p>In FXML, an XML element represents one of the following:</p>
 129 <ul>
 130 <li>A class instance</li>
 131 <li>A property of a class instance</li>
 132 <li>A "static" property</li>
 133 <li>A "define" block</li>
 134 <li>A block of script code</li>
 135 </ul>
 136 
 137 <p>Class instances, instance properties, static properties, and define blocks are discussed in this section below. Scripting is discussed in a later section.</p>
 138 
 139 <h3><a name="class_instance_elements">Class Instance Elements</a></h3>
 140 <p>Class instances can be constructed in FXML in several ways. The most common is via instance declaration elements, which simply create a new instance of a class by name. Other ways of creating class instances include referencing existing values, copying existing values, and including external FXML files. Each is discussed in more detail below.</p>
 141 
 142 <h4><a name="instance_declaration_elements">Instance Declarations</a></h4>
 143 <p><assert id="instance_declaration" group="instance_declarations">If an element's tag is considered an instance declaration if the tag begins with uppercase letter (and the class is imported)</assert> or, as in Java, <assert id="fully_qualified_name" group="instance_declarations">it denotes a fully-qualified (including the package name) name of a class.</assert> When the FXML loader (also introduced later) encounters such an element, it creates an instance of that class.</p>
 144 
 145 <p><assert id="import" group="instance_declarations">Importing a class is done using the "import" processing instruction (PI). For example, the following PI imports the <span class="code">javafx.scene.control.Label</span> class into the current FXML document’s namespace:</p>
 146 
 147 <pre class="code">
 148 &lt;?import javafx.scene.control.Label?&gt;
 149 </pre>
 150 
 151 <p>This PI imports all classes from the javafx.scene.control package into the current namespace:</p>
 152 
 153 <pre class="code">
 154 &lt;?import javafx.scene.control.*?&gt;
 155 </pre>
 156 </assert>
 157 
 158 <p><assert id="instantiation_java_bean" group="instance_declarations">Any class that adheres to JavaBean constructor and property naming conventions can be readily instantiated and configured using FXML.</assert> The following is a simple but complete example that creates an instance of <span class="code">javafx.scene.control.Label</span> and sets its "text" property to "Hello, World!":</p>
 159 
 160 <pre class="code">
 161 &lt;?import javafx.scene.control.Label?&gt;
 162 &lt;Label text="Hello, World!"/&gt;
 163 </pre>
 164 
 165 <p>Note that the <span class="code">Label</span>’s "text" property in this example is set using an XML attribute. Properties can also be set using nested property elements. Property elements are discussed in more detail later in this section. Property attributes are discussed in a later section.</p>
 166 
 167 <p>Classes that don't conform to Bean conventions can also be constructed in FXML, using an object called a "builder". Builders are discussed in more detail later.</p>
 168 
 169 <h5>Maps</h5>
 170 <p>Internally, the FXML loader uses an instance of <span class="code">com.sun.javafx.fxml.BeanAdapter</span> to wrap an instantiated object and invoke its setter methods. This (currently) private class implements the <span class="code">java.util.Map</span> interface and allows a caller to get and set Bean property values as key/value pairs.</p>
 171 
 172 <p><assert id="map_instantiaton" group="instance_declarations">If an element represents a type that already implements <span class="code">Map</span> (such as <span class="code">java.util.HashMap</span>), it is not wrapped and its <span class="code">get()</span> and <span class="code">put()</span> methods are invoked directly.</assert> For example, the following FXML creates an instance of <span class="code">HashMap</span> and sets its "foo" and "bar" values to "123" and "456", respectively:
 173 
 174 <pre class="code">
 175 &lt;HashMap foo="123" bar="456"/&gt;
 176 </pre>
 177 
 178 <h5>fx:value</h5>
 179 <p><assert id="instantiation_no_default_constructor" group="instance_declarations">The <span class="code">fx:value</span> attribute can be used to initialize an instance of a type that does not have a default constructor but provides a static <span class="code">valueOf(String)</span> method.</assert> For example, <span class="code">java.lang.String</span> as well as each of the primitive wrapper types define a <span class="code">valueOf()</span> method and can be constructed in FXML as follows:</p>
 180 
 181 <pre class="code">
 182 &lt;String fx:value="Hello, World!"/&gt;
 183 &lt;Double fx:value="1.0"/&gt;
 184 &lt;Boolean fx:value="false"/&gt;
 185 </pre>
 186 
 187 <p>Custom classes that define a static <span class="code">valueOf(String)</span> method can also be constructed this way.</p>
 188 
 189 <h5>fx:factory</h5>
 190 <p><assert id="instantiation_factory" group="instance_declarations">The <span class="code">fx:factory</span> attribute is another means of creating objects whose classes do not have a default constructor. The value of the attribute is the name of a static, no-arg factory method for producing class instances.</assert> For example, the following markup creates an instance of an observable array list, populated with three string values:</p>
 191 
 192 <pre class="code">
 193 &lt;FXCollections fx:factory="observableArrayList"&gt;
 194     &lt;String fx:value="A"/&gt;
 195     &lt;String fx:value="B"/&gt;
 196     &lt;String fx:value="C"/&gt;
 197 &lt;/FXCollections&gt;
 198 </pre>
 199 
 200 <h5>Builders</h5>
 201 <p><assert id="instantiation_builder" group="instance_declarations">A third means of creating instances of classes that do not conform to Bean conventions (such as those representing immutable values) is a "builder".</assert> The builder design pattern delegates object construction to a mutable helper class (called a "builder") that is responsible for manufacturing instances of the immutable type.</p>
 202 
 203 <p>Builder support in FXML is provided by two interfaces. The <span class="code">javafx.util.Builder</span> interface defines a single method named <span class="code">build()</span> which is responsible for constructing the actual object:</p>
 204 
 205 <pre class="code">
 206 public interface Builder&lt;T&gt; {
 207     public T build();
 208 }
 209 </pre>
 210 
 211 <p>A <span class="code">javafx.util.BuilderFactory</span> is responsible for producing builders that are capable of instantiating a given type:</p>
 212 
 213 <pre class="code">
 214 public interface BuilderFactory {
 215     public Builder&lt;?&gt; getBuilder(Class&lt;?&gt; type);
 216 }
 217 </pre>
 218 
 219 <p>A default builder factory, <span class="code">JavaFXBuilderFactory</span>, is provided in the <span class="code">javafx.fxml</span> package. This factory is capable of creating and configuring most immutable JavaFX types. For example, the following markup uses the default builder to create an instance of the immutable <span class="code">javafx.scene.paint.Color</span> class:
 220 
 221 <pre class="code">
 222 &lt;Color red="1.0" green="0.0" blue="0.0"/&gt;
 223 </pre>
 224 
 225 <p>Note that, unlike Bean types, which are constructed when the element's start tag is processed, objects constructed by a builder are not instantiated until the element's closing tag is reached. This is because all of the required arguments may not be available until the element has been fully processed. For example, the Color object in the preceding example could also be written as:</p>
 226 
 227 <pre class="code">
 228 &lt;Color&gt;
 229     &lt;red&gt;1.0&lt;/red&gt;
 230     &lt;green&gt;0.0&lt;/green&gt;
 231     &lt;blue&gt;0.0&lt;/blue&gt;
 232 &lt;/Color&gt;
 233 </pre>
 234 
 235 <p>The <span class="code">Color</span> instance cannot be fully constructed until all three of the color components are known.</p>
 236 
 237 <p>When processing markup for an object that will be constructed by a builder, the <span class="code">Builder</span> instances are treated like value objects - if a <span class="code">Builder</span> implements the <span class="code">Map</span> interface, the <span class="code">put()</span> method is used to set the builder's attribute values. Otherwise, the builder is wrapped in a <span class="code">BeanAdapter</span> and its properties are assumed to be exposed via standard Bean setters.</p>
 238 
 239 <h4><a name="include_elements">&lt;fx:include&gt;</a></h4>
 240 <p>The <span class="code">&lt;fx:include&gt;</span> tag creates an object from FXML markup defined in another file. It is used as follows:</p>
 241 
 242 <pre class="code">
 243 &lt;fx:include source="<span class="variable">filename</span>"/&gt;
 244 </pre>
 245 
 246 <p>where <span class="variable">filename</span> is the name of the FXML file to include. <assert id="include_leading_slash_character" group="instance_declarations">Values that begin with a leading slash character are treated as relative to the classpath.</assert> <assert id="include_no_leading_slash_character" group="instance_declarations">Values with no leading slash are considered relative to the path of the current document.</assert></p>
 247 
 248 <p>For example, given the following markup:</p>
 249 
 250 <pre class="code">
 251 &lt;?import javafx.scene.control.*?&gt;
 252 &lt;?import javafx.scene.layout.*?&gt;
 253 &lt;VBox xmlns:fx="http://javafx.com/fxml"&gt;
 254     &lt;children&gt;
 255         &lt;fx:include source="my_button.fxml"/&gt;
 256     &lt;/children&gt;
 257 &lt;/VBox&gt;
 258 </pre>
 259 
 260 <p>If <span class="filename">my_button.fxml</span> contains the following:
 261 
 262 <pre class="code">
 263 &lt;?import javafx.scene.control.*?&gt;
 264 &lt;Button text="My Button"/&gt;
 265 </pre>
 266 
 267 <p>the resulting scene graph would contain a <span class="code">VBox</span> as a root object with a single <span class="code">Button</span> as a child node.</p>
 268 
 269 <p>Note the use of the "fx" namespace prefix. This is a reserved prefix that defines a number of elements and attributes that are used for internal processing of an FXML source file. It is generally declared on the root element of a FXML document. Other features provided by the "fx" namespace are described in the following sections.</p>
 270 
 271 <p><span class="code">&lt;fx:include&gt;</span> also supports attributes for specifying the name of the resource bundle that should be used to localize the included content, as well as the character set used to encode the source file. Resource resolution is discussed in a later section.</p>
 272 
 273 <pre class="code">
 274 &lt;fx:include source="<span class="variable">filename</span>" resources="<span class="variable">resource_file</span>" charset="utf-8"/&gt;
 275 </pre>
 276 
 277 <h4><a name="constant_elements">&lt;fx:constant&gt;</a></h4>
 278 <p><assert id="constant" group="instance_declarations">The <span class="code">&lt;fx:constant&gt;</span> element creates a reference to a class constant.</assert> For example, the following markup sets the value of the "minWidth" property of a<span class="code">Button</span> instance to the value of the <span class="code">NEGATIVE_INFINITY</span> constant defined by the <span class="code">java.lang.Double</span> class:</p>
 279 
 280 <pre class="code">
 281 &lt;Button&gt;
 282     &lt;minHeight&gt;&lt;Double fx:constant="NEGATIVE_INFINITY"/&gt;&lt;/minHeight&gt;
 283 &lt;/Button&gt;
 284 </pre>
 285 
 286 <h4><a name="reference_elements">&lt;fx:reference&gt;</a></h4>
 287 <p><assert id="reference" group="instance_declarations">The <span class="code">&lt;fx:reference&gt;</span> element creates a new reference to an existing element.</assert> Wherever this tag appears, it will effectively be replaced by the value of the named element. It is used in conjunction with either the <span class="code">fx:id</span> attribute or with a script variables, both of which are discussed in more detail in later sections. The "source" attribute of the <span class="code">&lt;fx:reference&gt;</span> element specifies the name of the object to which the new element will refer.</p>
 288 
 289 <p>For example, the following markup assigns a previously-defined <span class="code">Image</span> instance named "myImage" to the "image" property of an <span class="code">ImageView</span> control:</p>
 290 
 291 <pre class="code">
 292 &lt;ImageView&gt;
 293     &lt;image&gt;
 294         &lt;fx:reference source="myImage"/&gt;
 295     &lt;/image&gt;
 296 &lt;/ImageView&gt;
 297 </pre>
 298 
 299 <p>Note that, since it is also possible to dereference a variable using the attribute variable resolution operator (discussed later in the <a href="#attributes">Attributes</a> section), <span class="code">fx:reference</span> is generally only used when a reference value must be specified as an element, such as when adding the reference to a collection:</p>
 300 
 301 <pre class="code">
 302 &lt;ArrayList&gt;
 303     &lt;fx:reference source="element1"/&gt;
 304     &lt;fx:reference source="element2"/&gt;
 305     &lt;fx:reference source="element3"/&gt;
 306 &lt;/ArrayList&gt;
 307 </pre>
 308 
 309 <p>For most other cases, using an attribute is simpler and more concise.</p>
 310 
 311 <h4><a name="copy_elements">&lt;fx:copy&gt;</a></h4>
 312 <p><assert id="copy" group="instance_declarations">The <span class="code">&lt;fx:copy&gt;</span> element creates a copy of an existing element.</assert> Like <span class="code">&lt;fx:reference&gt;</span>, it is used with the fx:id attribute or a script variable. The element's "source" attribute specifies the name of the object that will be copied. The source type must define a copy constructor that will be used to construct the copy from the source value.</p>
 313 
 314 <p>At the moment, no JavaFX platform classes provide such a copy constructor, so this element is provided primarily for use by application developers. This may change in a future release.</p>
 315 
 316 <h4><a name="root_elements">&lt;fx:root&gt;</a></h4>
 317 <p><assert id="root" group="instance_declarations">The <span class="code">&lt;fx:root&gt;</span> element creates a reference to a previously defined root element. It is only valid as the root node of an FXML document.</assert> <span class="code">&lt;fx:root&gt;</span> is used primarily when creating custom controls that are backed by FXML markup. This is discussed in more detail in the <a href="#fxmlloader">FXMLLoader</a> section.</p>
 318 
 319 <h3><a name="property_elements">Property Elements</a></h3>
 320 <p>Elements whose tag names begin with a lowercase letter represent object properties. A property element may represent one of the following:</p>
 321 
 322 <ul>
 323 <li>A property setter</li>
 324 <li>A read-only list property</li>
 325 <li>A read-only map property</li>
 326 </ul>
 327 
 328 <h4><a name="property_setter_elements">Property Setters</a></h4>
 329 <p><assert id="property_setter" group="property_elements">If an element represents a property setter, the contents of the element (which must be either a text node or a nested class instance element) are passed as the value to the setter for the property.</assert></p>
 330 
 331 <p>For example, the following FXML creates an instance of the <span class="code">Label</span> class and sets the value of the label's "text" property to "Hello, World!":</p>
 332 
 333 <pre class="code">
 334 &lt;?import javafx.scene.control.Label?&gt;
 335 &lt;Label&gt;
 336     &lt;text&gt;Hello, World!&lt;/text&gt;
 337 &lt;/Label&gt;
 338 </pre>
 339 
 340 <p>This produces the same result as the earlier example which used an attribute to set the "text" property:</p>
 341 
 342 <pre class="code">
 343 &lt;?import javafx.scene.control.Label?&gt;
 344 &lt;Label text="Hello, World!"/&gt;
 345 </pre>
 346 
 347 <p>Property elements are generally used when the property value is a complex type that can't be represented using a simple string-based attribute value, or when the character length of the value is so long that specifying it as an attribute would have a negative impact on readability.</p>
 348 
 349 <h5>Type Coercion</h5>
 350 <p><assert id="coercion" group="property_elements">FXML uses "type coercion" to convert property values to the appropriate type as needed.</assert> Type coercion is required because the only data types supported by XML are elements, text, and attributes (whose values are also text). However, Java supports a number of different data types including built-in primitive value types as well as extensible reference types.</p>
 351 
 352 <p>The FXML loader uses the <span class="code">coerce()</span> method of <span class="code">BeanAdapter</span> to perform any required type conversions. This method is capable of performing basic primitive type conversions such as <span class="code">String</span> to <span class="code">boolean</span> or <span class="code">int</span> to <span class="code">double</span>, and will also convert <span class="code">String</span> to <span class="code">Class</span> or <span class="code">String</span> to <span class="code">Enum</span>. Additional conversions can be implemented by defining a static <span class="code">valueOf()</span> method on the target type.</p>
 353 
 354 <h4><a name="read_only_list_property_elements">Read-Only List Properties</a></h4>
 355 <p><assert id="read_only_list_property" group="property_elements">A read-only list property is a Bean property whose getter returns an instance of <span class="code">java.util.List</span> and has no corresponding setter method. The contents of a read-only list element are automatically added to the list as they are processed.</assert></p>
 356 
 357 <p>For example, the "children" property of <span class="code">javafx.scene.Group</span> is a read-only list property representing the group's child nodes:</p>
 358 
 359 <pre class="code">
 360 &lt;?import javafx.scene.*?&gt;
 361 &lt;?import javafx.scene.shape.*?&gt;
 362 &lt;Group xmlns:fx="http://javafx.com/fxml"&gt;
 363     &lt;children&gt;
 364         &lt;Rectangle fx:id="rectangle" x="10" y="10" width="320" height="240"
 365             fill="#ff0000"/&gt;
 366         ...
 367     &lt;/children&gt;
 368 &lt;/Group&gt;
 369 </pre>
 370 
 371 <p>As each sub-element of the <span class="code">&lt;children&gt;</span> element is read, it is added to the list returned by <span class="code">Group#getChildren()</span>.
 372 
 373 <h4><a name="read_only_map_property_elements">Read-Only Map Properties</a></h4>
 374 <p><assert id="read_only_map_property" group="property_elements">A read-only map property is a bean property whose getter returns an instance of <span class="code">java.util.Map</span> and has no corresponding setter method. The attributes of a read-only map element are applied to the map when the closing tag is processed.</assert></p>
 375 
 376 <p>The "properties" property of <span class="code">javafx.scene.Node</span> is an example of a read-only map property. The following markup sets the "foo" and "bar" properties of a <span class="code">Label</span> instance to "123" and "456", respectively:</p>
 377 
 378 <pre class="code">
 379 &lt;?import javafx.scene.control.*?&gt;
 380 &lt;Button&gt;
 381     &lt;properties foo="123" bar="456"/&gt;
 382 &lt;/Button&gt;
 383 </pre>
 384 
 385 <p><assert id="read_only_property_type_treating" group="property_elements">Note that a read-only property whose type is neither a <span class="code">List</span> nor a <span class="code">Map</span> will be treated as if it were a read-only map. The return value of the getter method will be wrapped in a <span class="code">BeanAdapter</span> and can be used in the same way as any other read-only map.</assert></p>
 386 
 387 <h4><a name="default_properties">Default Properties</a></h4>
 388 <p><assert id="default_property" group="property_elements">A class may define a "default property" using the <span class="code">@DefaultProperty</span> annotation defined in the <span class="code">javafx.beans</span> package. If present, the sub-element representing the default property can be omitted from the markup.</assert></p>
 389 
 390 <p>For example, since <span class="code">javafx.scene.layout.Pane</span> (the superclass of <span class="code">javafx.scene.layout.VBox</span>) defines a default property of "children", a <span class="code">&lt;children&gt;</span> element is not required; the loader will automatically add the sub-elements of the <span class="code">VBox</span> to the container's "children" collection:</p>
 391 
 392 <pre class="code">
 393 &lt;?import javafx.scene.*?&gt;
 394 &lt;?import javafx.scene.shape.*?&gt;
 395 &lt;VBox xmlns:fx="http://javafx.com/fxml"&gt;
 396     &lt;Button text="Click Me!"/&gt;
 397     ...
 398 &lt;/VBox&gt;
 399 </pre>
 400 
 401 <p>Note that default properties are not limited to collections. If an element's default property refers to a scalar value, any sub-element of that element will be set as the value of the property.</p>
 402 
 403 <p>For example, since <span class="code">javafx.scene.control.ScrollPane</span> defines a default property of "content", a scroll pane containing a <span class="code">TextArea</span> as its content can be specified as follows:
 404 
 405 <pre class="code">
 406 &lt;ScrollPane&gt;
 407     &lt;TextArea text="Once upon a time..."/&gt;
 408 &lt;/ScrollPane&gt;
 409 </pre>
 410 
 411 <p>Taking advantage of default properties can significantly reduce the verbosity of FXML markup.</p>
 412 
 413 <h3><a name="static_property_elements">Static Properties</a></h3>
 414 <p><assert id="static_property" group="elements">An element may also represent a "static" property (sometimes called an "attached property"). Static properties are properties that only make sense in a particular context. They are not intrinsic to the class to which they are applied, but are defined by another class (often, the parent container of a control).</assert></p>
 415 
 416 <p>Static properties are prefixed with the name of class that defines them. For example, The following FXML invokes the static setter for <span class="code">GridPane</span>'s "rowIndex" and "columnIndex" properties:</p>
 417 
 418 <pre class="code">
 419 &lt;GridPane&gt;
 420     &lt;children&gt;
 421         &lt;Label text="My Label"&gt;
 422             &lt;GridPane.rowIndex&gt;0&lt;/GridPane.rowIndex&gt;
 423        &lt;GridPane.columnIndex&gt;0&lt;/GridPane.columnIndex&gt;
 424         &lt;/Label&gt;
 425     &lt;/children&gt;
 426 &lt;/TabPane&gt;
 427 </pre>
 428 
 429 <p>This translates roughly to the following in Java:</p>
 430 
 431 <pre class="code">
 432 GridPane gridPane = new GridPane();
 433 
 434 Label label = new Label();
 435 label.setText("My Label");
 436 
 437 GridPane.setRowIndex(label, 0);
 438 GridPane.setColumnIndex(label, 0);
 439 
 440 gridPane.getChildren().add(label);
 441 </pre>
 442 
 443 The calls to <span class="code">GridPane#setRowIndex()</span> and <span class="code">GridPane#setColumnIndex()</span> "attach" the index data to the <span class="code">Label</span> instance. <span class="code">GridPane</span> then uses these during layout to arrange its children appropriately. Other containers, including <span class="code">AnchorPane</span>, <span class="code">BorderPane</span>, and <span class="code">StackPane</span>, define similar properties.</p>
 444 
 445 <p>As with instance properties, static property elements are generally used when the property value cannot be efficiently represented by an attribute value. Otherwise, static property attributes (discussed in a later section) will generally produce more concise and readable markup.</p>
 446 
 447 <h3><a name="define_elements">Define Blocks</a></h3>
 448 <p><assert id="define" group="elements">The <span class="code">&lt;fx:define&gt;</span> element is used to create objects that exist outside of the object hierarchy but may need to be referred to elsewhere.</assert></p>
 449 
 450 <p>For example, when working with radio buttons, it is common to define a <span class="code">ToggleGroup</span> that will manage the buttons' selection state. This group is not part of the scene graph itself, so should not be added to the buttons' parent. A define block can be used to create the button group without interfering with the overall structure of the document:</p>
 451 
 452 <pre class="code">
 453 &lt;VBox&gt;
 454     &lt;fx:define&gt;
 455         &lt;ToggleGroup fx:id="myToggleGroup"/&gt;
 456     &lt;/fx:define&gt;
 457     &lt;children&gt;
 458         &lt;RadioButton text="A" toggleGroup="$myToggleGroup"/&gt;
 459         &lt;RadioButton text="B" toggleGroup="$myToggleGroup"/&gt;
 460         &lt;RadioButton text="C" toggleGroup="$myToggleGroup"/&gt;
 461     &lt;/children&gt;
 462 &lt;/VBox&gt;
 463 </pre>
 464 
 465 <p>Elements in define blocks are usually assigned an ID that can be used to refer to the element's value later. IDs are discussed in more detail in later sections.</p>
 466 
 467 <h2><a name="attributes">Attributes</a></h2>
 468 <p>An attribute in FXML may represent one of the following:</p>
 469 <ul>
 470 <li>A property of a class instance</li>
 471 <li>A "static" property</li>
 472 <li>An event handler</li>
 473 </ul>
 474 
 475 <p>Each are discussed in more detail in the following sections.</p>
 476 
 477 <h3><a name="instance_property_attributes">Instance Properties</a></h3>
 478 <p><assert id="instance_properties" group="instance_properties">Like property elements, attributes can also be used to configure the properties of a class instance.</assert> For example, the following markup creates a <span class="code">Button</span> whose text reads "Click Me!":</p>
 479 
 480 <pre class="code">
 481 &lt;?import javafx.scene.control.*?&gt;
 482 &lt;Button text="Click Me!"/&gt;
 483 </pre>
 484 
 485 <p><assert id="attributes_type_coercion" group="instance_properties">As with property elements, property attributes support type coercion.</assert> When the following markup is processed, the "x", "y", "width", and "height" values will be converted to doubles, and the "fill" value will be converted to a <span class="code">Color</span>:</p>
 486 
 487 <pre class="code">
 488 &lt;Rectangle fx:id="rectangle" x="10" y="10" width="320" height="240"
 489     fill="#ff0000"/&gt;
 490 </pre>
 491 
 492 <p>Unlike property elements, which are applied as they are processed, property attributes are not applied until the closing tag of their respective element is reached. This is done primarily to facilitate the case where an attribute value depends on some information that won't be available until after the element's content has been completely processed (for example, the selected index of a <span class="code">TabPane</span> control, which can't be set until all of the tabs have been added).</p>
 493 
 494 <p>Another key difference between property attributes and property elements in FXML is that attributes support a number of "resolution operators" that extend their functionality. The following operators are supported and are discussed in more detail below:</p>
 495 
 496 <ul>
 497 <li>Location resolution</li>
 498 <li>Resource resolution</li>
 499 <li>Variable resolution</li>
 500 </ul>
 501 
 502 <h4><a name="location_resolution">Location Resolution</a></h4>
 503 <p><assert id="location_resolution" group="instance_properties">As strings, XML attributes cannot natively represent typed location information such as a URL. However, it is often necessary to specify such locations in markup; for example, the source of an image resource. The location resolution operator (represented by an "@" prefix to the attribute value) is used to specify that an attribute value should be treated as a location relative to the current file rather than a simple string.</assert></p>
 504 
 505 <p>For example, the following markup creates an ImageView and populates it with image data from <span class="filename">my_image.png</span>, which is assumed to be located at a path relative to the current FXML file:</p>
 506 
 507 <pre class="code">
 508 &lt;ImageView&gt;
 509     &lt;image&gt;
 510         &lt;Image url="@my_image.png"/&gt;
 511     &lt;/image&gt;
 512 &lt;/ImageView&gt;
 513 </pre>
 514 
 515 <p>Since <span class="code">Image</span> is an immutable object, a builder is required to construct it. Alternatively, if <span class="code">Image</span> were to define a <span class="code">valueOf(URL)</span> factory method, the image view could be populated as follows:</p>
 516 
 517 <pre class="code">
 518 &lt;ImageView image="@my_image.png"/&gt;
 519 </pre>
 520 
 521 <p>The value of the "image" attribute would be converted to a URL by the FXML loader, then coerced to an <span class="code">Image</span> using the <span class="code">valueOf()</span> method.</p>
 522 
 523 <p>Note that whitespace values in the URL must be encoded; for example, to refer to a file named "My Image.png", the FXML document should contain the following:</p>
 524 
 525 <pre class="code">
 526 &lt;Image url="@My%20Image.png"/&gt;
 527 </pre>
 528 
 529 <p>rather than:</p>
 530 
 531 <pre class="code">
 532 &lt;Image url="@My Image.png"/&gt;
 533 </pre>
 534 
 535 <h4><a name="resource_resolution">Resource Resolution</a></h4>
 536 <assert id="resource_resolution" group="instance_properties">
 537 <p>In FXML, resource substitution can be performed at load time for localization purposes. When provided with an instance of <span class="code">java.util.ResourceBundle</span>, the FXML loader will replace instances of resource names with their locale-specific values. Resource names are identified by a "%" prefix, as shown below:</p>
 538 
 539 <pre class="code">
 540 &lt;Label text="%myText"/&gt;
 541 </pre>
 542 
 543 <p>If the loader is given a resource bundle defined as follows:</p>
 544 
 545 <pre class="code">
 546 myText = This is the text!
 547 </pre>
 548 </assert>
 549 
 550 <p>the output of the FXML loader would be a <span class="code">Label</span> instance containing the text "This is the text!".</p>
 551 
 552 <h4><a name="variable_resolution">Variable Resolution</a></h4>
 553 <p><assert id="variable_resolution" group="instance_properties">An FXML document defines a variable namespace in which named elements and script variables may be uniquely identified. The variable resolution operator allows a caller to replace an attribute value with an instance of a named object before the corresponding setter method is invoked. Variable references are identified by a "$" prefix</assert>, as shown below:</p>
 554 
 555 <pre class="code">
 556 &lt;fx:define&gt;
 557     &lt;ToggleGroup fx:id="myToggleGroup"/&gt;
 558 &lt;/fx:define&gt;
 559 ...
 560 &lt;RadioButton text="A" toggleGroup="$myToggleGroup"/&gt;
 561 &lt;RadioButton text="B" toggleGroup="$myToggleGroup"/&gt;
 562 &lt;RadioButton text="C" toggleGroup="$myToggleGroup"/&gt;
 563 </pre>
 564 
 565 <p><assert id="assigning_id" group="instance_properties">Assigning an <span class="code">fx:id</span> value to an element creates a variable in the document's namespace that can later be referred to by variable dereference attributes, such as the "toggleGroup" attribute shown above, or in script code, discussed in a later section. Additionally, if the object's type defines an "id" property, this value will also be passed to the objects <span class="code">setId()</span> method.</assert></p>
 566 
 567 <h4><a name="escape_sequences">Escape Sequences</a></h4>
 568 
 569 <p><assert id="escape_sequences" group="instance_properties">If the value of an attribute begins with one of the resource resolution prefixes, the character can be escaped by prepending it with a leading backslash ("\") character.</assert> For example, the following markup creates a <span class="code">Label</span> instance whose text reads "$10.00":</p>
 570 
 571 <pre class="code">
 572 &lt;Label text="\$10.00"/&gt;
 573 </pre>
 574 
 575 <h4><a name="expression_binding">Expression Binding</a></h4>
 576 <p>Attribute variables as shown above are resolved once at load time. Later updates to the variables value are not automatically reflected in any properties to which the value was assigned. In many cases, this is sufficient; however, it is often convenient to "bind" a property value to a variable or expression such that changes to the variable are automatically propagated to the target property. Expression bindings can be used for this purpose.</p>
 577 
 578 <p><assert id="expression_binding" group="instance_properties">An expression binding also begins with the variable resolution operator, but is followed by a set of curly braces which wrap the expression value.</assert> For example, the following markup binds the value of a text input's "text" property to the "text" property of a <span class="code">Label</span> instance:</p>
 579 
 580 <pre class="code">
 581 &lt;TextField fx:id="textField"/&gt;
 582 &lt;Label text="${textField.text}"/&gt;
 583 </pre>
 584 
 585 <p>As the user types in the text input, the label's text content will be automatically updated.</p>
 586 
 587 <p>More complex expression are also supported. A list of supported constants and operators follows:</p>
 588 
 589 <table>
 590  <tr><td>"string"<br />'string'</td><td>A string constant</td></tr>
 591  <tr><td>true<br />false</td><td>A boolean constant</td></tr>
 592  <tr><td>null</td><td>A constant representing the null value</td></tr>
 593  <tr><td>50.0<br />3e5<br />42</td><td>A numerical constant</td></tr>
 594  <tr><td>- <br/>(unary operator)</td><td>Unary minus operator, applied on a number</td>
 595  <tr><td>! <br/>(unary operator)</td><td>Unary negation of a boolean</td></tr>
 596  <tr><td>+ - <br />
 597                         * /
 598                         %</td> <td>Numerical binary operators</td></tr>
 599  <tr><td>&& ||</td><td>Boolean binary operators</td></tr>
 600  <tr><td>&gt; &gt;= <br />
 601                    &lt; &lt;= <br />
 602                    == !=</td>
 603                    <td>Binary operators of comparison.<br/> Both arguments must be of type Comparable</td></tr>
 604 </table>
 605 
 606 <h3><a name="static_property_attributes">Static Properties</a></h3>
 607 <p><assert id="static_property_attributes" group="attributes">Attributes representing static properties are handled similarly to static property elements and use a similar syntax.</assert> For example, the earlier <span class="code">GridPane</span> markup shown earlier to demonstrate static property elements could be rewritten as follows:</p>
 608 
 609 <pre class="code">
 610 &lt;GridPane&gt;
 611     &lt;children&gt;
 612         &lt;Label text="My Label" GridPane.rowIndex="0" GridPane.columnIndex="0"/&gt;
 613     &lt;/children&gt;
 614 &lt;/TabPane&gt;
 615 </pre>
 616 
 617 <p><assert id="static_property_attributes_use" group="attributes">In addition to being more concise, static property attributes, like instance property attributes, support location, resource, and variable resolution operators, the only limitation being that it is not possible to create an expression binding to a static property.</assert></p>
 618 
 619 <h3><a name="event_handler_attributes">Event Handlers</a></h3>
 620 <p>Event handler attributes are a convenient means of attaching behaviors to document elements. Any class that defines a <span class="code">setOn<span class="variable">Event</span>()</span> method can be assigned an event handler in markup.</p>
 621 
 622 <p>FXML supports three types of event handler attributes: script event handlers, controller method event handlers and expressions. Each are discussed below.</p>
 623 
 624 <h4><a name="script_event_handlers">Script Event Handlers</a></h4>
 625 <p><assert id="script_event_handlers" group="event_handlers">A script event handler is an event handler that executes script code when the event is fired, similar to event handlers in HTML.</assert> For example, the following script-based handler for the button's "onAction" event uses JavaScript to write the text "You clicked me!" to the console when the user presses the button:</p>
 626 
 627 <pre class="code">
 628 &lt;?language javascript?&gt;
 629 ...
 630 
 631 &lt;VBox&gt;
 632     &lt;children&gt;
 633         &lt;Button text="Click Me!"
 634             onAction="java.lang.System.out.println('You clicked me!');"/&gt;
 635     &lt;/children&gt;
 636 &lt;/VBox&gt;
 637 </pre>
 638 
 639 <p><assert id="language_processing_instruction" group="event_handlers">Note the use of the language processing instruction at the beginning of the code snippet. This PI tells the FXML loader which scripting language should be used to execute the event handler. A page language must be specified whenever inline script is used in an FXML document, and can only be specified once per document. However, this does not apply to external scripts, which may be implemented using any number of supported scripting languages.</assert> Scripting is discussed in more detail in the next section.</p>
 640 
 641 <h4><a name="controller_method_event_handlers">Controller Method Event Handlers</a></h4>
 642 <p>A controller method event handler is a method defined by a document's "controller". A controller is an object that is associated with the deserialized contents of an FXML document and is responsible for coordinating the behaviors of the objects (often user interface elements) defined by the document.</p>
 643 
 644 <p><assert id="controller_method_event_handler_specification" group="event_handlers">A controller method event handler is specified by a leading hash symbol followed by the name of the handler method.</assert> For example:</p>
 645 
 646 <pre class="code">
 647 &lt;VBox fx:controller="com.foo.MyController"
 648     xmlns:fx="http://javafx.com/fxml"&gt;
 649     &lt;children&gt;
 650         &lt;Button text="Click Me!" onAction="#handleButtonAction"/&gt;
 651     &lt;/children&gt;
 652 &lt;/VBox&gt;
 653 </pre>
 654 
 655 <p><assert id="controller_attribute" group="event_handlers">Note the use of the <span class="code">fx:controller</span> attribute on the root element. This attribute is used to associate a controller class with the document.</assert> If <span class="code">MyController</span> is defined as follows:</p>
 656 
 657 <pre class="code">
 658 package com.foo;
 659 
 660 public class MyController {
 661     public void handleButtonAction(ActionEvent event) {
 662         System.out.println("You clicked me!");
 663     }
 664 }
 665 </pre>
 666 
 667 <p>the <span class="code">handleButtonAction()</span> will be called when the user presses the button, and the text "You clicked me!" will be written to the console.</p>
 668 
 669 <p><assert id="handler_method_signature" group="event_handlers">In general, a handler method should conform to the signature of a standard event handler; that is, it should take a single argument of a type that extends <span class="code">javafx.event.Event</span> and should return void (similar to an event delegate in C#). The event argument often carries important and useful information about the nature of the event;</assert> <assert id="handler_method_opt_arg" group="event_handlers">however, it is optional and may be omitted if desired</assert>.
 670 So this is also a valid handler:</p>
 671 
 672 <pre class="code">
 673 package com.foo;
 674 
 675 public class MyController {
 676     public void handleButtonAction() {
 677         System.out.println("You clicked me!");
 678     }
 679 }
 680 </pre>
 681 
 682 <p>Controllers are discussed in more detail in a later section.</p>
 683 
 684 <h4><a name="expression_handlers">Event handlers from expressions</a></h4>
 685 <p>Any expression that point to a <a href="#variable_resolution">variable</a> of javafx.event.EventHandler type
 686     can be used as an expression handler. </p>
 687 <p>
 688 Previous example using an expression handler:
 689 </p>
 690 <pre class="code">
 691 &lt;VBox fx:controller="com.foo.MyController"
 692     xmlns:fx="http://javafx.com/fxml"&gt;
 693     &lt;children&gt;
 694         &lt;Button text="Click Me!" onAction="$controller.onActionHandler"/&gt;
 695     &lt;/children&gt;
 696 &lt;/VBox&gt;
 697 </pre>
 698 
 699 <p> With the controller that contains a field like this </p>
 700     
 701 <pre class="code">
 702 public class MyController {
 703     
 704     @FXML
 705     public EventHandler<ActionEvent> onActionHandler = new EventHandler<>() { ... }
 706 
 707     ...
 708 }  
 709 </pre>
 710 
 711 <p> Note that other kinds of expressions, like <a href="#expression_binding">binding expressions</a>
 712     are not supported in this context. </p>
 713 
 714 <h4><a name="collections_and_property_handlers">Special handlers for collections and properties</a></h4>
 715 <p> Collections and object properties cannot be listen to using <span class="code">setOn<span class="variable">Event</span>()</span> methods.
 716     For these reason, special handler methods need to be used.
 717 <span class="code">ObservableList</span>, <span class="code">ObservableMap</span> or <span class="code">ObservableSet</span>
 718  uses a special <span class="code">onChange</span> attribute that points to a handler method with a <span class="code">ListChangeListner.Change</span>, <span class="code">MapChangeListener.Change</span> or <span class="code">SetChangeListener.Change</span> parameter respectively.
 719 </p>
 720 <pre class="code">
 721 &lt;VBox fx:controller="com.foo.MyController"
 722     xmlns:fx="http://javafx.com/fxml"&gt;
 723     &lt;children onChange="#handleChildrenChange"/&gt;
 724 &lt;/VBox&gt;
 725 </pre>
 726 
 727 where the handler method looks like this:
 728 
 729 <pre class="code">
 730 package com.foo;
 731 
 732 import javafx.collections.ListChangeListener.Change;
 733 
 734 public class MyController {
 735     public void handleChildrenChange(ListChangeListener.Change c) {
 736         System.out.println("Children changed!");
 737     }
 738 }
 739 </pre>
 740 
 741 <p>Similarly, the property handlers are methods that have the same parameters as changed method of ChangeListener :</p>
 742 <p><span class="code">changed(ObservableValue&lt;? extends T&gt; observable, T oldValue, T newValue)</span></p>
 743 
 744 <p>A handler for parent property would look like this</p>
 745 <pre class="code">
 746 public class MyController {
 747     public void handleParentChange(ObservableValue value, Parent oldValue, Parent newValue) {
 748         System.out.println("Parent changed!");
 749     }
 750 }
 751 </pre>
 752 
 753 <p>For convenience, the first parameter can be a subclass of <span class="code">ObservableValue</span>,
 754     e.g. <span class="code">Property</span></p>
 755 
 756 <p>For registering to a property, a special <span class="code">on&lt;propertyName&gt;Change</span>
 757 attribute must be used.</p>
 758 
 759 <pre class="code">
 760 &lt;VBox fx:controller="com.foo.MyController"
 761     xmlns:fx="http://javafx.com/fxml" onParentChange="#handleParentChange"/&gt;
 762 </pre>
 763     
 764 <p>Note that collections and properties do not currently support scripting handlers.</p>
 765 
 766 <h2><a name="scripting">Scripting</a></h2>
 767 <assert id="script_tag" group="scripting">The <span class="code">&lt;fx:script&gt;</span> tag allows a caller to import scripting code into or embed script within a FXML file. Any JVM scripting language can be used, including JavaScript, Groovy, and Clojure, among others. Script code is often used to define event handlers directly in markup or in an associated source file, since event handlers can often be written more concisely in more loosely-typed scripting languages than they can in a statically-typed language such as Java.</assert></p>
 768 
 769 <p>For example, the following markup defines a function called <span class="code">handleButtonAction()</span> that is called by the action handler attached to the <span class="code">Button</span> element:</p>
 770 
 771 <pre class="code">
 772 &lt;?language javascript?&gt;
 773 
 774 &lt;?import javafx.scene.control.*?&gt;
 775 &lt;?import javafx.scene.layout.*?&gt;
 776 
 777 &lt;VBox xmlns:fx="http://javafx.com/fxml"&gt;
 778     &lt;fx:script&gt;
 779 
 780     function handleButtonAction(event) {
 781        java.lang.System.out.println('You clicked me!');
 782     }
 783     &lt;/fx:script&gt;
 784 
 785     &lt;children&gt;
 786         &lt;Button text="Click Me!" onAction="handleButtonAction(event);"/&gt;
 787     &lt;/children&gt;
 788 &lt;/VBox&gt;
 789 </pre>
 790 
 791 <p>Clicking the button triggers the event handler, which invokes the function, producing output identical to the previous examples.</p>
 792 
 793 <p><assert id="external_script_code" group="scripting">Script code may also be defined in external files. The previous example could be split into an FXML file and a JavaScript source file with no difference in functionality</assert>:</p>
 794 
 795 <div class="caption">example.fxml</div>
 796 <pre class="code">
 797 &lt;?import javafx.scene.control.*?&gt;
 798 &lt;?import javafx.scene.layout.*?&gt;
 799 
 800 &lt;VBox xmlns:fx="http://javafx.com/fxml"&gt;
 801     &lt;fx:script source="example.js" charset="cp1252"/&gt;
 802 
 803     &lt;children&gt;
 804         &lt;Button text="Click Me!" onAction="handleButtonAction(event);"/&gt;
 805     &lt;/children&gt;
 806 &lt;/VBox&gt;
 807 </pre>
 808 
 809 <div class="caption">example.js</div>
 810 <pre class="code">
 811 
 812 function handleButtonAction(event) {
 813    java.lang.System.out.println('You clicked me!');
 814 }
 815 </pre>
 816 
 817 <p>It is often preferable to separate code from markup in this way, since many text editors support syntax highlighting for the various scripting languages supported by the JVM. It can also help improve readability of the source code and markup.</p>
 818 
 819 <assert id="script_blocks_not_limited" group="scripting">
 820 <p>Note that script blocks are not limited to defining event handler functions. Script code is executed as it is processed, so it can also be used to dynamically configure the structure of the resulting output. As a simple example, the following FXML includes a script block that defines a variable named "labelText". The value of this variable is used to populate the text property of a <span class="code">Label</span> instance:</p>
 821 
 822 <pre class="code">
 823 &lt;fx:script&gt;
 824 var myText = "This is the text of my label.";
 825 &lt;/fx:script&gt;
 826 
 827 ...
 828 
 829 &lt;Label text="$myText"/&gt;
 830 </pre>
 831 </assert>
 832 
 833 <p><strong>Warning:</strong>As of JavaFX 8, <span class="code">importClass()</span> javascript function is no longer supported. You have to use fully qualified names as in the example above or load a nashorn compatibility script.</p>
 834 
 835 <pre class="code">
 836 load("nashorn:mozilla_compat.js");
 837 importClass(java.lang.System);
 838 
 839 function handleButtonAction(event) {
 840    System.out.println('You clicked me!');
 841 }
 842 </pre> 
 843 
 844 <h2><a name="controllers">Controllers</a></h2>
 845 <p>While it can be convenient to write simple event handlers in script, either inline or defined in external files, it is often preferable to define more complex application logic in a compiled, strongly-typed language such as Java. As discussed earlier, the <span class="code">fx:controller</span> attribute allows a caller to associate a "controller" class with an FXML document. A controller is a compiled class that implements the "code behind" the object hierarchy defined by the document.</p>
 846 
 847 <p>As shown earlier, controllers are often used to implement event handlers for user interface elements defined in markup:</p>
 848 
 849 <pre class="code">
 850 &lt;VBox fx:controller="com.foo.MyController"
 851     xmlns:fx="http://javafx.com/fxml"&gt;
 852     &lt;children&gt;
 853         &lt;Button text="Click Me!" onAction="#handleButtonAction"/&gt;
 854     &lt;/children&gt;
 855 &lt;/VBox&gt;
 856 </pre>
 857 
 858 <pre class="code">
 859 package com.foo;
 860 
 861 public class MyController {
 862     public void handleButtonAction(ActionEvent event) {
 863         System.out.println("You clicked me!");
 864     }
 865 }
 866 </pre>
 867 
 868 <p><assert id="handlers_initialize" group="controllers">In many cases, it is sufficient to simply declare event handlers in this manner. However, when more control over the behavior of the controller and the elements it manages is required, the controller can define an <span class="code">initialize()</span> method, which will be called once on an implementing controller when the contents of its associated document have been completely loaded</assert>:</p>
 869 
 870 <pre class="code">
 871 public void initialize();
 872 </pre>
 873 
 874 <p><assert id="post-processing" group="controllers">This allows the implementing class to perform any necessary post-processing on the content.</assert> It also provides the controller with access to the resources that were used to load the document and the location that was used to resolve relative paths within the document (commonly equivalent to the location of the document itself).</p>
 875 
 876 <p>For example, the following code defines an <span class="code">initialize()</span> method that attaches an action handler to a button in code rather than via an event handler attribute, as was done in the previous example. The button instance variable is injected by the loader as the document is read. The resulting application behavior is identical:</p>
 877 
 878 <pre class="code">
 879 &lt;VBox fx:controller="com.foo.MyController"
 880     xmlns:fx="http://javafx.com/fxml"&gt;
 881     &lt;children&gt;
 882         &lt;Button fx:id="button" text="Click Me!"/&gt;
 883     &lt;/children&gt;
 884 &lt;/VBox&gt;
 885 </pre>
 886 
 887 <pre class="code">
 888 package com.foo;
 889 
 890 public class MyController implements Initializable {
 891     public Button button;
 892 
 893     @Override
 894     public void initialize(URL location, Resources resources)
 895         button.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
 896             @Override
 897             public void handle(ActionEvent event) {
 898                 System.out.println("You clicked me!");
 899             }
 900         });
 901     }
 902 }
 903 </pre>
 904 
 905 <h3><a name="fxml_annotation">@FXML</a></h3>
 906 <p><assert id="public_controller_access" group="controllers">Note
 907 that, in the previous examples, the controller member fields and
 908 event handler methods were declared as public so they can be set
 909 or invoked by the loader.</assert> In practice, this is not often
 910 an issue, since a controller is generally only visible to the FXML
 911 loader that creates it. <assert id="FXML_controller_access"
 912 group="controllers">However, for developers who prefer more restricted
 913 visibility for controller fields or handler methods, the <span
 914 class="code">javafx.fxml.FXML</span> annotation can be used. This
 915 annotation marks a protected or private class member as accessible
 916 to FXML.
 917 If the class being annotated is in a named module, the
 918 module containing that class must <span class="code">open</span>
 919 the containing package to at least
 920 the <span class="code">javafx.fxml</span> module.</assert></span>
 921 
 922 <p>For example, the controllers from the previous examples could be rewritten as follows:</p>
 923 
 924 <pre class="code">
 925 package com.foo;
 926 
 927 public class MyController {
 928     @FXML
 929     private void handleButtonAction(ActionEvent event) {
 930         System.out.println("You clicked me!");
 931     }
 932 }
 933 </pre>
 934 
 935 <pre class="code">
 936 package com.foo;
 937 
 938 public class MyController implements Initializable {
 939     @FXML private Button button;
 940 
 941     @FXML
 942     protected void initialize()
 943         button.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
 944             @Override
 945             public void handle(ActionEvent event) {
 946                 System.out.println("You clicked me!");
 947             }
 948         });
 949     }
 950 }
 951 </pre>
 952 
 953 <p>In the first version, the <span class="code">handleButtonAction()</span> is tagged with <span class="code">@FXML</span> to allow markup defined in the controller's document to invoke it. In the second example, the button field is annotated to allow the loader to set its value. The <span class="code">initialize()</span> method is similarly annotated.</p>
 954 
 955 <h3><a name="nested_controllers">Nested Controllers</a></h3>
 956 <p><assert id="nested_fxml_mapping" group="controllers">Controller instances for nested FXML documents loaded via the <span class="code">&lt;fx:include&gt;</span> element are mapped directly to member fields of the including controller.</assert> This allows a developer to easily access functionality defined by an include (such as a dialog window presented by an application's main window controller). For example, given the following code:
 957 
 958 <div class="caption">main_window_content.fxml</div>
 959 <pre class="code">
 960 &lt;VBox fx:controller="com.foo.MainController"&gt;
 961    &lt;fx:define&gt;
 962       &lt;fx:include fx:id="dialog" source="dialog.fxml"/&gt;
 963    &lt;/fx:define&gt;
 964    ...
 965 &lt;/VBox&gt;
 966 </pre>
 967 
 968 <div class="caption">MainController.java</div>
 969 <pre class="code">
 970 public class MainController extends Controller {
 971     @FXML private Window dialog;
 972     @FXML private DialogController dialogController;
 973 
 974     ...
 975 }
 976 </pre>
 977 
 978 <p>when the controller's <span class="code">initialize()</span> method is called, the <span class="code">dialog</span> field will contain the root element loaded from the "dialog.fxml" include, and the <span class="code">dialogController</span> field will contain the include's controller. The main controller can then invoke methods on the included controller, to populate and show the dialog, for example. Note that as the content of the file referenced by fx:include otherwise would become part of the scene graph spanned from main_window_content.fxml, it is necessary to wrap fx:include by fx:define to separate the scene graphs of both windows.</p>
 979 
 980 <h2><a name="fxmlloader">FXMLLoader</a></h2>
 981 <p>The <span class="code">FXMLLoader</span> class is responsible for actually loading an FXML source file and returning the resulting object graph. For example, the following code loads an FXML file from a location on the classpath relative to the loading class and localizes it with a resource bundle named "com.foo.example". The type of the root element is assumed to be a subclass of <span class="code">javafx.scene.layout.Pane</span>, and the document is assumed to define a controller of type <span class="code">MyController</span>:</p>
 982 
 983 <pre class="code">
 984 URL location = getClass().getResource("example.fxml");
 985 ResourceBundle resources = ResourceBundle.getBundle("com.foo.example");
 986 FXMLLoader fxmlLoader = new FXMLLoader(location, resources);
 987 
 988 Pane root = (Pane)fxmlLoader.load();
 989 MyController controller = (MyController)fxmlLoader.getController();
 990 </pre>
 991 
 992 <p><assert id="loader_instance_hierarchy" group="fxml_loader">Note that the output of an <span class="code">FXMLLoader#load()</span> operation is an instance hierarchy that reflects the actual named classes in the document, not <span class="code">org.w3c.dom</span> nodes representing those classes.</assert> Internally, <span class="code">FXMLLoader</span> uses the <span class="code">javax.xml.stream</span> API (also known as the <i>Streaming API for XML</i>, or <i>StAX</i>) to load an FXML document. StAX is an extremely efficient event-based XML parsing API that is conceptually similar to its W3C predecessor, SAX. It allows an FXML document to be processed in a single pass, rather than loaded into an intermediate DOM structure and then post-processed.</p>
 993 
 994 <h3><a name="custom_components">Custom Components</a></h3>
 995 <p>The <assert id="loader_setRoot" group="fxml_loader"><span class="code">setRoot()</span></assert> and <assert id="loader_setController" group="fxml_loader"><span class="code">setController()</span></assert> methods of <span class="code">FXMLLoader</span> allow a caller to inject document root and controller values, respectively, into the document namespace, rather than delegating creation of these values to <span class="code">FXMLLoader</span> itself. This allows a developer to easily create reusable controls that are internally implemented using markup, but (from an API perspective) appear identically to controls implemented programmatically.</p>
 996 
 997 <p>For example, the following markup defines the structure of a simple custom control containing a <span class="code">TextField</span> and a <span class="code">Button</span> instance. The root container is defined as an instance of <span class="code">javafx.scene.layout.VBox</span>:</p>
 998 
 999 <pre class="code">
1000 &lt;?import javafx.scene.*?&gt;
1001 &lt;?import javafx.scene.control.*?&gt;
1002 &lt;?import javafx.scene.layout.*?&gt;
1003 
1004 &lt;fx:root type="javafx.scene.layout.VBox" xmlns:fx="http://javafx.com/fxml"&gt;
1005     &lt;TextField fx:id="textField"/&gt;
1006     &lt;Button text="Click Me" onAction="#doSomething"/&gt;
1007 &lt;/fx:root&gt;
1008 </pre>
1009 
1010 <p>As mentioned earlier, the <span class="code">&lt;fx:root&gt;</span> tag creates a reference to a previously defined root element. The value of this element is obtained by calling the <span class="code">getRoot()</span> method of <span class="code">FXMLLoader</span>. <assert id="loader_setRoot_sequence" group="fxml_loader">Prior to calling <span class="code">load()</span>, the caller must specify this value via a call to <span class="code">setRoot()</span></assert>. <assert id="loader_setController_sequence" group="fxml_loader">The caller may similarly provide a value for the document's controller by calling <span class="code">setController()</span>, which sets the value that will be used as the document's controller when the document is read</assert>. These two methods are commonly used together when creating custom FXML-based components.</p>
1011 
1012 <p>In the following example, the <span class="code">CustomControl</span> class extends <span class="code">VBox</span> (the type declared by the <span class="code">&lt;fx:root&gt;</span> element), and sets itself as both the root and controller of the FXML document in its constructor. When the document is loaded, the contents of <span class="code">CustomControl</span> will be populated with the contents of the previous FXML document:</p>
1013 
1014 <pre class="code">
1015 package fxml;
1016 
1017 import java.io.IOException;
1018 
1019 import javafx.beans.property.StringProperty;
1020 import javafx.fxml.FXML;
1021 import javafx.fxml.FXMLLoader;
1022 import javafx.scene.control.TextField;
1023 import javafx.scene.layout.VBox;
1024 
1025 public class CustomControl extends VBox {
1026     @FXML private TextField textField;
1027 
1028     public CustomControl() {
1029         FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("custom_control.fxml"));
1030         fxmlLoader.setRoot(this);
1031         fxmlLoader.setController(this);
1032 
1033         try {
1034             fxmlLoader.load();
1035         } catch (IOException exception) {
1036             throw new RuntimeException(exception);
1037         }
1038     }
1039 
1040     public String getText() {
1041         return textProperty().get();
1042     }
1043 
1044     public void setText(String value) {
1045         textProperty().set(value);
1046     }
1047 
1048     public StringProperty textProperty() {
1049         return textField.textProperty();
1050     }
1051 
1052     @FXML
1053     protected void doSomething() {
1054         System.out.println("The button was clicked!");
1055     }
1056 }
1057 </pre>
1058 
1059 <p>Now, callers can use instances of this control in code or in markup, just like any other control; e.g.:</p>
1060 
1061 <div class="caption">Java</div>
1062 <pre class="code">
1063 HBox hbox = new HBox();
1064 CustomControl customControl = new CustomControl();
1065 customControl.setText("Hello World!");
1066 hbox.getChildren().add(customControl);
1067 </pre>
1068 
1069 <div class="caption">FXML</div>
1070 <pre class="code">
1071 &lt;HBox&gt;
1072     &lt;CustomControl text="Hello World!"/&gt;
1073 &lt;/HBox&gt;
1074 </pre>
1075 
1076 <h2><a name="deploy_as_module">Deploying an Application as a Module</a></h2>
1077 <p>If <span class="code">FXMLLoader</span> is used to load types in a named
1078 module, the application must ensure that all types that are referenced in the
1079 FXML files, including the controller class and any custom <span class="code">Node</span>
1080 classes, are reflectively accessible to the <span class="code">javafx.fxml</span>
1081 module. A type is reflectively accessible if the module
1082 <span class="code">opens</span> the containing package to at least the
1083 <span class="code">javafx.fxml</span> module.
1084 </p>
1085 
1086 <p>For example, if <span class="code">com.foo.MyController</span> is in the
1087 <span class="code">foo.app</span> module, the
1088 <span class="code">module-info.java</span> might look like this:
1089 </p>
1090 <pre><span class="code">module foo.app {
1091     opens com.foo to javafx.fxml;
1092 }</span></pre>
1093 
1094 <p>Alternatively, a type is reflectively accessible if the module
1095 <span class="code">exports</span> the containing package unconditionally.
1096 </p>
1097 <hr>
1098 <p>
1099 <font size="-1"><a href="http://bugreport.java.com/bugreport/" target="_blank">Submit a bug or feature</a><br>For further API reference and developer documentation, see <a href="http://download.java.net/java/jdk9/docs/index.html" target="_blank">Java SE Documentation</a>. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.<br> <a href="http://download.java.net/java/jdk9/docs/legal/cpyr.html" target="_blank">Copyright</a> &copy; 2008, 2017, Oracle and/or its affiliates. All rights reserved.<br><b>DRAFT 9-ea</b></font>
1100 </p>
1101 </body>
1102 </html>