1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   2 
   3 <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">    
   4 <head>
   5 <link href="fxml.css" rel="stylesheet"/>    
   6 <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
   7 <title>Introduction to FXML | JavaFX 9</title>
   8 <meta name="description" content="The document introduces FXML, an XML-based declarative markup language for defining user interfaces in JavaFX 9 applications."/>
   9 <meta name="keywords" content="JavaFX 9, FXML, JavaFX GUI development, web development, Java application development, GUI applications, rich internet applications, RIA, expressive content"/>
  10 </head>
  11 <body>
  12 
  13 <div class="fx-code-header">
  14 <div class="version"><br/>Release: JavaFX 9</div>
  15 </div>
  16 
  17 <h1>Introduction to FXML</h1>
  18 <p class="subtitle">Last updated: 01 May 2017</p>
  19 
  20 <h2>Contents</h2>
  21 <ul class="contents">
  22 <li><a href="#overview">Overview</a></li>
  23 <li>
  24     <a href="#elements">Elements</a>
  25     <ul>
  26     <li>
  27         <a href="#class_instance_elements">Class Instance Elements</a>
  28         <ul>
  29         <li><a href="#instance_declaration_elements">Instance Declarations</a></li>
  30         <li><a href="#include_elements">&lt;fx:include&gt;</a></li>
  31         <li><a href="#constant_elements">&lt;fx:constant&gt;</a></li>
  32         <li><a href="#reference_elements">&lt;fx:reference&gt;</a></li>
  33         <li><a href="#copy_elements">&lt;fx:copy&gt;</a></li>
  34         <li><a href="#root_elements">&lt;fx:root&gt;</a></li>
  35         </ul>
  36     </li>
  37 
  38     <li>
  39         <a href="#property_elements">Property Elements</a>
  40         <ul>
  41         <li><a href="#property_setter_elements">Property Setters</a></li>
  42         <li><a href="#read_only_list_property_elements">Read-Only List Properties</a></li>
  43         <li><a href="#read_only_map_property_elements">Read-Only Map Properties</a></li>
  44         <li><a href="#default_properties">Default Properties</a></li>
  45         </ul>
  46     </li>
  47 
  48     <li><a href="#static_property_elements">Static Properties</a></li>
  49     <li><a href="#define_elements">Define Blocks</a></li>
  50     </ul>
  51 </li>
  52 <li>
  53     <a href="#attributes">Attributes</a>
  54     <ul>
  55     <li>
  56         <a href="#instance_property_attributes">Instance Properties</a>
  57         <ul>
  58         <li><a href="#location_resolution">Location Resolution</a></li>
  59         <li><a href="#resource_resolution">Resource Resolution</a></li>
  60         <li><a href="#variable_resolution">Variable Resolution</a></li>
  61         <li><a href="#escape_sequences">Escape Sequences</a></li>
  62         <li><a href="#expression_binding">Expression Binding</a></li>
  63         </ul>
  64     </li>
  65 
  66     <li><a href="#static_property_attributes">Static Properties</a></li>
  67     <li>
  68         <a href="#event_handler_attributes">Event Handlers</a>
  69         <ul>
  70         <li><a href="#script_event_handlers">Script Event Handlers</a></li>
  71         <li><a href="#controller_method_event_handlers">Controller Method Event Handlers</a></li>
  72         <li><a href="#expression_handlers">Event handlers from expressions</a></li>
  73         <li><a href="#collections_and_property_handlers">Special handlers for collections and properties</a></li>
  74         </ul>
  75     </li>
  76     </ul>
  77 </li>
  78 <li><a href="#scripting">Scripting</a></li>
  79 <li>
  80     <a href="#controllers">Controllers</a>
  81     <ul>
  82     <li><a href="#fxml_annotation">@FXML</a></li>
  83     <li><a href="#nested_controllers">Nested Controllers</a></li>
  84     </ul>
  85 </li>
  86 <li>
  87     <a href="#fxmlloader">FXMLLoader</a>
  88     <ul>
  89     <li><a href="#custom_components">Custom Components</a></li>
  90     </ul>
  91 </li>
  92 <li><a href="#deploy_as_module">Deploying an Application as a Module</a></li>
  93 </ul>
  94 
  95 <h2><a name="overview">Overview</a></h2>
  96 <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>
  97 
  98 <p>This document introduces the FXML markup language and explains how it can be used to simplify development of JavaFX applications.</p>
  99 
 100 <h2><a name="elements">Elements</a></h2>
 101 <p>In FXML, an XML element represents one of the following:</p>
 102 <ul>
 103 <li>A class instance</li>
 104 <li>A property of a class instance</li>
 105 <li>A "static" property</li>
 106 <li>A "define" block</li>
 107 <li>A block of script code</li>
 108 </ul>
 109 
 110 <p>Class instances, instance properties, static properties, and define blocks are discussed in this section below. Scripting is discussed in a later section.</p>
 111 
 112 <h3><a name="class_instance_elements">Class Instance Elements</a></h3>
 113 <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>
 114 
 115 <h4><a name="instance_declaration_elements">Instance Declarations</a></h4>
 116 <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>
 117 
 118 <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>
 119 
 120 <pre class="code">
 121 &lt;?import javafx.scene.control.Label?&gt;
 122 </pre>
 123 
 124 <p>This PI imports all classes from the javafx.scene.control package into the current namespace:</p>
 125 
 126 <pre class="code">
 127 &lt;?import javafx.scene.control.*?&gt;
 128 </pre>
 129 </assert>
 130 
 131 <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>
 132 
 133 <pre class="code">
 134 &lt;?import javafx.scene.control.Label?&gt;
 135 &lt;Label text="Hello, World!"/&gt;
 136 </pre>
 137 
 138 <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>
 139 
 140 <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>
 141 
 142 <h5>Maps</h5>
 143 <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>
 144 
 145 <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:
 146 
 147 <pre class="code">
 148 &lt;HashMap foo="123" bar="456"/&gt;
 149 </pre>
 150 
 151 <h5>fx:value</h5>
 152 <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>
 153 
 154 <pre class="code">
 155 &lt;String fx:value="Hello, World!"/&gt;
 156 &lt;Double fx:value="1.0"/&gt;
 157 &lt;Boolean fx:value="false"/&gt;
 158 </pre>
 159 
 160 <p>Custom classes that define a static <span class="code">valueOf(String)</span> method can also be constructed this way.</p>
 161 
 162 <h5>fx:factory</h5>
 163 <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>
 164 
 165 <pre class="code">
 166 &lt;FXCollections fx:factory="observableArrayList"&gt;
 167     &lt;String fx:value="A"/&gt;
 168     &lt;String fx:value="B"/&gt;
 169     &lt;String fx:value="C"/&gt;
 170 &lt;/FXCollections&gt;
 171 </pre>
 172 
 173 <h5>Builders</h5>
 174 <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>
 175 
 176 <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>
 177 
 178 <pre class="code">
 179 public interface Builder&lt;T&gt; {
 180     public T build();
 181 }
 182 </pre>
 183 
 184 <p>A <span class="code">javafx.util.BuilderFactory</span> is responsible for producing builders that are capable of instantiating a given type:</p>
 185 
 186 <pre class="code">
 187 public interface BuilderFactory {
 188     public Builder&lt;?&gt; getBuilder(Class&lt;?&gt; type);
 189 }
 190 </pre>
 191 
 192 <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:
 193 
 194 <pre class="code">
 195 &lt;Color red="1.0" green="0.0" blue="0.0"/&gt;
 196 </pre>
 197 
 198 <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>
 199 
 200 <pre class="code">
 201 &lt;Color&gt;
 202     &lt;red&gt;1.0&lt;/red&gt;
 203     &lt;green&gt;0.0&lt;/green&gt;
 204     &lt;blue&gt;0.0&lt;/blue&gt;
 205 &lt;/Color&gt;
 206 </pre>
 207 
 208 <p>The <span class="code">Color</span> instance cannot be fully constructed until all three of the color components are known.</p>
 209 
 210 <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>
 211 
 212 <h4><a name="include_elements">&lt;fx:include&gt;</a></h4>
 213 <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>
 214 
 215 <pre class="code">
 216 &lt;fx:include source="<span class="variable">filename</span>"/&gt;
 217 </pre>
 218 
 219 <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>
 220 
 221 <p>For example, given the following markup:</p>
 222 
 223 <pre class="code">
 224 &lt;?import javafx.scene.control.*?&gt;
 225 &lt;?import javafx.scene.layout.*?&gt;
 226 &lt;VBox xmlns:fx="http://javafx.com/fxml"&gt;
 227     &lt;children&gt;
 228         &lt;fx:include source="my_button.fxml"/&gt;
 229     &lt;/children&gt;
 230 &lt;/VBox&gt;
 231 </pre>
 232 
 233 <p>If <span class="filename">my_button.fxml</span> contains the following:
 234 
 235 <pre class="code">
 236 &lt;?import javafx.scene.control.*?&gt;
 237 &lt;Button text="My Button"/&gt;
 238 </pre>
 239 
 240 <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>
 241 
 242 <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>
 243 
 244 <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>
 245 
 246 <pre class="code">
 247 &lt;fx:include source="<span class="variable">filename</span>" resources="<span class="variable">resource_file</span>" charset="utf-8"/&gt;
 248 </pre>
 249 
 250 <h4><a name="constant_elements">&lt;fx:constant&gt;</a></h4>
 251 <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>
 252 
 253 <pre class="code">
 254 &lt;Button&gt;
 255     &lt;minHeight&gt;&lt;Double fx:constant="NEGATIVE_INFINITY"/&gt;&lt;/minHeight&gt;
 256 &lt;/Button&gt;
 257 </pre>
 258 
 259 <h4><a name="reference_elements">&lt;fx:reference&gt;</a></h4>
 260 <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>
 261 
 262 <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>
 263 
 264 <pre class="code">
 265 &lt;ImageView&gt;
 266     &lt;image&gt;
 267         &lt;fx:reference source="myImage"/&gt;
 268     &lt;/image&gt;
 269 &lt;/ImageView&gt;
 270 </pre>
 271 
 272 <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>
 273 
 274 <pre class="code">
 275 &lt;ArrayList&gt;
 276     &lt;fx:reference source="element1"/&gt;
 277     &lt;fx:reference source="element2"/&gt;
 278     &lt;fx:reference source="element3"/&gt;
 279 &lt;/ArrayList&gt;
 280 </pre>
 281 
 282 <p>For most other cases, using an attribute is simpler and more concise.</p>
 283 
 284 <h4><a name="copy_elements">&lt;fx:copy&gt;</a></h4>
 285 <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>
 286 
 287 <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>
 288 
 289 <h4><a name="root_elements">&lt;fx:root&gt;</a></h4>
 290 <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>
 291 
 292 <h3><a name="property_elements">Property Elements</a></h3>
 293 <p>Elements whose tag names begin with a lowercase letter represent object properties. A property element may represent one of the following:</p>
 294 
 295 <ul>
 296 <li>A property setter</li>
 297 <li>A read-only list property</li>
 298 <li>A read-only map property</li>
 299 </ul>
 300 
 301 <h4><a name="property_setter_elements">Property Setters</a></h4>
 302 <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>
 303 
 304 <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>
 305 
 306 <pre class="code">
 307 &lt;?import javafx.scene.control.Label?&gt;
 308 &lt;Label&gt;
 309     &lt;text&gt;Hello, World!&lt;/text&gt;
 310 &lt;/Label&gt;
 311 </pre>
 312 
 313 <p>This produces the same result as the earlier example which used an attribute to set the "text" property:</p>
 314 
 315 <pre class="code">
 316 &lt;?import javafx.scene.control.Label?&gt;
 317 &lt;Label text="Hello, World!"/&gt;
 318 </pre>
 319 
 320 <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>
 321 
 322 <h5>Type Coercion</h5>
 323 <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>
 324 
 325 <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>
 326 
 327 <h4><a name="read_only_list_property_elements">Read-Only List Properties</a></h4>
 328 <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>
 329 
 330 <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>
 331 
 332 <pre class="code">
 333 &lt;?import javafx.scene.*?&gt;
 334 &lt;?import javafx.scene.shape.*?&gt;
 335 &lt;Group xmlns:fx="http://javafx.com/fxml"&gt;
 336     &lt;children&gt;
 337         &lt;Rectangle fx:id="rectangle" x="10" y="10" width="320" height="240"
 338             fill="#ff0000"/&gt;
 339         ...
 340     &lt;/children&gt;
 341 &lt;/Group&gt;
 342 </pre>
 343 
 344 <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>.
 345 
 346 <h4><a name="read_only_map_property_elements">Read-Only Map Properties</a></h4>
 347 <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>
 348 
 349 <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>
 350 
 351 <pre class="code">
 352 &lt;?import javafx.scene.control.*?&gt;
 353 &lt;Button&gt;
 354     &lt;properties foo="123" bar="456"/&gt;
 355 &lt;/Button&gt;
 356 </pre>
 357 
 358 <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>
 359 
 360 <h4><a name="default_properties">Default Properties</a></h4>
 361 <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>
 362 
 363 <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>
 364 
 365 <pre class="code">
 366 &lt;?import javafx.scene.*?&gt;
 367 &lt;?import javafx.scene.shape.*?&gt;
 368 &lt;VBox xmlns:fx="http://javafx.com/fxml"&gt;
 369     &lt;Button text="Click Me!"/&gt;
 370     ...
 371 &lt;/VBox&gt;
 372 </pre>
 373 
 374 <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>
 375 
 376 <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:
 377 
 378 <pre class="code">
 379 &lt;ScrollPane&gt;
 380     &lt;TextArea text="Once upon a time..."/&gt;
 381 &lt;/ScrollPane&gt;
 382 </pre>
 383 
 384 <p>Taking advantage of default properties can significantly reduce the verbosity of FXML markup.</p>
 385 
 386 <h3><a name="static_property_elements">Static Properties</a></h3>
 387 <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>
 388 
 389 <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>
 390 
 391 <pre class="code">
 392 &lt;GridPane&gt;
 393     &lt;children&gt;
 394         &lt;Label text="My Label"&gt;
 395             &lt;GridPane.rowIndex&gt;0&lt;/GridPane.rowIndex&gt;
 396        &lt;GridPane.columnIndex&gt;0&lt;/GridPane.columnIndex&gt;
 397         &lt;/Label&gt;
 398     &lt;/children&gt;
 399 &lt;/TabPane&gt;
 400 </pre>
 401 
 402 <p>This translates roughly to the following in Java:</p>
 403 
 404 <pre class="code">
 405 GridPane gridPane = new GridPane();
 406 
 407 Label label = new Label();
 408 label.setText("My Label");
 409 
 410 GridPane.setRowIndex(label, 0);
 411 GridPane.setColumnIndex(label, 0);
 412 
 413 gridPane.getChildren().add(label);
 414 </pre>
 415 
 416 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>
 417 
 418 <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>
 419 
 420 <h3><a name="define_elements">Define Blocks</a></h3>
 421 <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>
 422 
 423 <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>
 424 
 425 <pre class="code">
 426 &lt;VBox&gt;
 427     &lt;fx:define&gt;
 428         &lt;ToggleGroup fx:id="myToggleGroup"/&gt;
 429     &lt;/fx:define&gt;
 430     &lt;children&gt;
 431         &lt;RadioButton text="A" toggleGroup="$myToggleGroup"/&gt;
 432         &lt;RadioButton text="B" toggleGroup="$myToggleGroup"/&gt;
 433         &lt;RadioButton text="C" toggleGroup="$myToggleGroup"/&gt;
 434     &lt;/children&gt;
 435 &lt;/VBox&gt;
 436 </pre>
 437 
 438 <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>
 439 
 440 <h2><a name="attributes">Attributes</a></h2>
 441 <p>An attribute in FXML may represent one of the following:</p>
 442 <ul>
 443 <li>A property of a class instance</li>
 444 <li>A "static" property</li>
 445 <li>An event handler</li>
 446 </ul>
 447 
 448 <p>Each are discussed in more detail in the following sections.</p>
 449 
 450 <h3><a name="instance_property_attributes">Instance Properties</a></h3>
 451 <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>
 452 
 453 <pre class="code">
 454 &lt;?import javafx.scene.control.*?&gt;
 455 &lt;Button text="Click Me!"/&gt;
 456 </pre>
 457 
 458 <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>
 459 
 460 <pre class="code">
 461 &lt;Rectangle fx:id="rectangle" x="10" y="10" width="320" height="240"
 462     fill="#ff0000"/&gt;
 463 </pre>
 464 
 465 <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>
 466 
 467 <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>
 468 
 469 <ul>
 470 <li>Location resolution</li>
 471 <li>Resource resolution</li>
 472 <li>Variable resolution</li>
 473 </ul>
 474 
 475 <h4><a name="location_resolution">Location Resolution</a></h4>
 476 <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>
 477 
 478 <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>
 479 
 480 <pre class="code">
 481 &lt;ImageView&gt;
 482     &lt;image&gt;
 483         &lt;Image url="@my_image.png"/&gt;
 484     &lt;/image&gt;
 485 &lt;/ImageView&gt;
 486 </pre>
 487 
 488 <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>
 489 
 490 <pre class="code">
 491 &lt;ImageView image="@my_image.png"/&gt;
 492 </pre>
 493 
 494 <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>
 495 
 496 <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>
 497 
 498 <pre class="code">
 499 &lt;Image url="@My%20Image.png"/&gt;
 500 </pre>
 501 
 502 <p>rather than:</p>
 503 
 504 <pre class="code">
 505 &lt;Image url="@My Image.png"/&gt;
 506 </pre>
 507 
 508 <h4><a name="resource_resolution">Resource Resolution</a></h4>
 509 <assert id="resource_resolution" group="instance_properties">
 510 <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>
 511 
 512 <pre class="code">
 513 &lt;Label text="%myText"/&gt;
 514 </pre>
 515 
 516 <p>If the loader is given a resource bundle defined as follows:</p>
 517 
 518 <pre class="code">
 519 myText = This is the text!
 520 </pre>
 521 </assert>
 522 
 523 <p>the output of the FXML loader would be a <span class="code">Label</span> instance containing the text "This is the text!".</p>
 524 
 525 <h4><a name="variable_resolution">Variable Resolution</a></h4>
 526 <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>
 527 
 528 <pre class="code">
 529 &lt;fx:define&gt;
 530     &lt;ToggleGroup fx:id="myToggleGroup"/&gt;
 531 &lt;/fx:define&gt;
 532 ...
 533 &lt;RadioButton text="A" toggleGroup="$myToggleGroup"/&gt;
 534 &lt;RadioButton text="B" toggleGroup="$myToggleGroup"/&gt;
 535 &lt;RadioButton text="C" toggleGroup="$myToggleGroup"/&gt;
 536 </pre>
 537 
 538 <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>
 539 
 540 <h4><a name="escape_sequences">Escape Sequences</a></h4>
 541 
 542 <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>
 543 
 544 <pre class="code">
 545 &lt;Label text="\$10.00"/&gt;
 546 </pre>
 547 
 548 <h4><a name="expression_binding">Expression Binding</a></h4>
 549 <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>
 550 
 551 <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>
 552 
 553 <pre class="code">
 554 &lt;TextField fx:id="textField"/&gt;
 555 &lt;Label text="${textField.text}"/&gt;
 556 </pre>
 557 
 558 <p>As the user types in the text input, the label's text content will be automatically updated.</p>
 559 
 560 <p>More complex expression are also supported. A list of supported constants and operators follows:</p>
 561 
 562 <table>
 563  <tr><td>"string"<br />'string'</td><td>A string constant</td></tr>
 564  <tr><td>true<br />false</td><td>A boolean constant</td></tr>
 565  <tr><td>null</td><td>A constant representing the null value</td></tr>
 566  <tr><td>50.0<br />3e5<br />42</td><td>A numerical constant</td></tr>
 567  <tr><td>- <br/>(unary operator)</td><td>Unary minus operator, applied on a number</td>
 568  <tr><td>! <br/>(unary operator)</td><td>Unary negation of a boolean</td></tr>
 569  <tr><td>+ - <br />
 570                         * /
 571                         %</td> <td>Numerical binary operators</td></tr>
 572  <tr><td>&& ||</td><td>Boolean binary operators</td></tr>
 573  <tr><td>&gt; &gt;= <br />
 574                    &lt; &lt;= <br />
 575                    == !=</td>
 576                    <td>Binary operators of comparison.<br/> Both arguments must be of type Comparable</td></tr>
 577 </table>
 578 
 579 <h3><a name="static_property_attributes">Static Properties</a></h3>
 580 <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>
 581 
 582 <pre class="code">
 583 &lt;GridPane&gt;
 584     &lt;children&gt;
 585         &lt;Label text="My Label" GridPane.rowIndex="0" GridPane.columnIndex="0"/&gt;
 586     &lt;/children&gt;
 587 &lt;/TabPane&gt;
 588 </pre>
 589 
 590 <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>
 591 
 592 <h3><a name="event_handler_attributes">Event Handlers</a></h3>
 593 <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>
 594 
 595 <p>FXML supports three types of event handler attributes: script event handlers, controller method event handlers and expressions. Each are discussed below.</p>
 596 
 597 <h4><a name="script_event_handlers">Script Event Handlers</a></h4>
 598 <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>
 599 
 600 <pre class="code">
 601 &lt;?language javascript?&gt;
 602 ...
 603 
 604 &lt;VBox&gt;
 605     &lt;children&gt;
 606         &lt;Button text="Click Me!"
 607             onAction="java.lang.System.out.println('You clicked me!');"/&gt;
 608     &lt;/children&gt;
 609 &lt;/VBox&gt;
 610 </pre>
 611 
 612 <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>
 613 
 614 <h4><a name="controller_method_event_handlers">Controller Method Event Handlers</a></h4>
 615 <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>
 616 
 617 <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>
 618 
 619 <pre class="code">
 620 &lt;VBox fx:controller="com.foo.MyController"
 621     xmlns:fx="http://javafx.com/fxml"&gt;
 622     &lt;children&gt;
 623         &lt;Button text="Click Me!" onAction="#handleButtonAction"/&gt;
 624     &lt;/children&gt;
 625 &lt;/VBox&gt;
 626 </pre>
 627 
 628 <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>
 629 
 630 <pre class="code">
 631 package com.foo;
 632 
 633 public class MyController {
 634     public void handleButtonAction(ActionEvent event) {
 635         System.out.println("You clicked me!");
 636     }
 637 }
 638 </pre>
 639 
 640 <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>
 641 
 642 <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>.
 643 So this is also a valid handler:</p>
 644 
 645 <pre class="code">
 646 package com.foo;
 647 
 648 public class MyController {
 649     public void handleButtonAction() {
 650         System.out.println("You clicked me!");
 651     }
 652 }
 653 </pre>
 654 
 655 <p>Controllers are discussed in more detail in a later section.</p>
 656 
 657 <h4><a name="expression_handlers">Event handlers from expressions</a></h4>
 658 <p>Any expression that point to a <a href="#variable_resolution">variable</a> of javafx.event.EventHandler type
 659     can be used as an expression handler. </p>
 660 <p>
 661 Previous example using an expression handler:
 662 </p>
 663 <pre class="code">
 664 &lt;VBox fx:controller="com.foo.MyController"
 665     xmlns:fx="http://javafx.com/fxml"&gt;
 666     &lt;children&gt;
 667         &lt;Button text="Click Me!" onAction="$controller.onActionHandler"/&gt;
 668     &lt;/children&gt;
 669 &lt;/VBox&gt;
 670 </pre>
 671 
 672 <p> With the controller that contains a field like this </p>
 673     
 674 <pre class="code">
 675 public class MyController {
 676     
 677     @FXML
 678     public EventHandler<ActionEvent> onActionHandler = new EventHandler<>() { ... }
 679 
 680     ...
 681 }  
 682 </pre>
 683 
 684 <p> Note that other kinds of expressions, like <a href="#expression_binding">binding expressions</a>
 685     are not supported in this context. </p>
 686 
 687 <h4><a name="collections_and_property_handlers">Special handlers for collections and properties</a></h4>
 688 <p> Collections and object properties cannot be listen to using <span class="code">setOn<span class="variable">Event</span>()</span> methods.
 689     For these reason, special handler methods need to be used.
 690 <span class="code">ObservableList</span>, <span class="code">ObservableMap</span> or <span class="code">ObservableSet</span>
 691  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.
 692 </p>
 693 <pre class="code">
 694 &lt;VBox fx:controller="com.foo.MyController"
 695     xmlns:fx="http://javafx.com/fxml"&gt;
 696     &lt;children onChange="#handleChildrenChange"/&gt;
 697 &lt;/VBox&gt;
 698 </pre>
 699 
 700 where the handler method looks like this:
 701 
 702 <pre class="code">
 703 package com.foo;
 704 
 705 import javafx.collections.ListChangeListener.Change;
 706 
 707 public class MyController {
 708     public void handleChildrenChange(ListChangeListener.Change c) {
 709         System.out.println("Children changed!");
 710     }
 711 }
 712 </pre>
 713 
 714 <p>Similarly, the property handlers are methods that have the same parameters as changed method of ChangeListener :</p>
 715 <p><span class="code">changed(ObservableValue&lt;? extends T&gt; observable, T oldValue, T newValue)</span></p>
 716 
 717 <p>A handler for parent property would look like this</p>
 718 <pre class="code">
 719 public class MyController {
 720     public void handleParentChange(ObservableValue value, Parent oldValue, Parent newValue) {
 721         System.out.println("Parent changed!");
 722     }
 723 }
 724 </pre>
 725 
 726 <p>For convenience, the first parameter can be a subclass of <span class="code">ObservableValue</span>,
 727     e.g. <span class="code">Property</span></p>
 728 
 729 <p>For registering to a property, a special <span class="code">on&lt;propertyName&gt;Change</span>
 730 attribute must be used.</p>
 731 
 732 <pre class="code">
 733 &lt;VBox fx:controller="com.foo.MyController"
 734     xmlns:fx="http://javafx.com/fxml" onParentChange="#handleParentChange"/&gt;
 735 </pre>
 736     
 737 <p>Note that collections and properties do not currently support scripting handlers.</p>
 738 
 739 <h2><a name="scripting">Scripting</a></h2>
 740 <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>
 741 
 742 <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>
 743 
 744 <pre class="code">
 745 &lt;?language javascript?&gt;
 746 
 747 &lt;?import javafx.scene.control.*?&gt;
 748 &lt;?import javafx.scene.layout.*?&gt;
 749 
 750 &lt;VBox xmlns:fx="http://javafx.com/fxml"&gt;
 751     &lt;fx:script&gt;
 752 
 753     function handleButtonAction(event) {
 754        java.lang.System.out.println('You clicked me!');
 755     }
 756     &lt;/fx:script&gt;
 757 
 758     &lt;children&gt;
 759         &lt;Button text="Click Me!" onAction="handleButtonAction(event);"/&gt;
 760     &lt;/children&gt;
 761 &lt;/VBox&gt;
 762 </pre>
 763 
 764 <p>Clicking the button triggers the event handler, which invokes the function, producing output identical to the previous examples.</p>
 765 
 766 <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>
 767 
 768 <div class="caption">example.fxml</div>
 769 <pre class="code">
 770 &lt;?import javafx.scene.control.*?&gt;
 771 &lt;?import javafx.scene.layout.*?&gt;
 772 
 773 &lt;VBox xmlns:fx="http://javafx.com/fxml"&gt;
 774     &lt;fx:script source="example.js" charset="cp1252"/&gt;
 775 
 776     &lt;children&gt;
 777         &lt;Button text="Click Me!" onAction="handleButtonAction(event);"/&gt;
 778     &lt;/children&gt;
 779 &lt;/VBox&gt;
 780 </pre>
 781 
 782 <div class="caption">example.js</div>
 783 <pre class="code">
 784 
 785 function handleButtonAction(event) {
 786    java.lang.System.out.println('You clicked me!');
 787 }
 788 </pre>
 789 
 790 <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>
 791 
 792 <assert id="script_blocks_not_limited" group="scripting">
 793 <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>
 794 
 795 <pre class="code">
 796 &lt;fx:script&gt;
 797 var myText = "This is the text of my label.";
 798 &lt;/fx:script&gt;
 799 
 800 ...
 801 
 802 &lt;Label text="$myText"/&gt;
 803 </pre>
 804 </assert>
 805 
 806 <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>
 807 
 808 <pre class="code">
 809 load("nashorn:mozilla_compat.js");
 810 importClass(java.lang.System);
 811 
 812 function handleButtonAction(event) {
 813    System.out.println('You clicked me!');
 814 }
 815 </pre> 
 816 
 817 <h2><a name="controllers">Controllers</a></h2>
 818 <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>
 819 
 820 <p>As shown earlier, controllers are often used to implement event handlers for user interface elements defined in markup:</p>
 821 
 822 <pre class="code">
 823 &lt;VBox fx:controller="com.foo.MyController"
 824     xmlns:fx="http://javafx.com/fxml"&gt;
 825     &lt;children&gt;
 826         &lt;Button text="Click Me!" onAction="#handleButtonAction"/&gt;
 827     &lt;/children&gt;
 828 &lt;/VBox&gt;
 829 </pre>
 830 
 831 <pre class="code">
 832 package com.foo;
 833 
 834 public class MyController {
 835     public void handleButtonAction(ActionEvent event) {
 836         System.out.println("You clicked me!");
 837     }
 838 }
 839 </pre>
 840 
 841 <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>
 842 
 843 <pre class="code">
 844 public void initialize();
 845 </pre>
 846 
 847 <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>
 848 
 849 <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>
 850 
 851 <pre class="code">
 852 &lt;VBox fx:controller="com.foo.MyController"
 853     xmlns:fx="http://javafx.com/fxml"&gt;
 854     &lt;children&gt;
 855         &lt;Button fx:id="button" text="Click Me!"/&gt;
 856     &lt;/children&gt;
 857 &lt;/VBox&gt;
 858 </pre>
 859 
 860 <pre class="code">
 861 package com.foo;
 862 
 863 public class MyController implements Initializable {
 864     public Button button;
 865 
 866     @Override
 867     public void initialize(URL location, Resources resources)
 868         button.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
 869             @Override
 870             public void handle(ActionEvent event) {
 871                 System.out.println("You clicked me!");
 872             }
 873         });
 874     }
 875 }
 876 </pre>
 877 
 878 <h3><a name="fxml_annotation">@FXML</a></h3>
 879 <p><assert id="public_controller_access" group="controllers">Note
 880 that, in the previous examples, the controller member fields and
 881 event handler methods were declared as public so they can be set
 882 or invoked by the loader.</assert> In practice, this is not often
 883 an issue, since a controller is generally only visible to the FXML
 884 loader that creates it. <assert id="FXML_controller_access"
 885 group="controllers">However, for developers who prefer more restricted
 886 visibility for controller fields or handler methods, the <span
 887 class="code">javafx.fxml.FXML</span> annotation can be used. This
 888 annotation marks a protected or private class member as accessible
 889 to FXML.
 890 If the class being annotated is in a named module, the
 891 module containing that class must <span class="code">open</span>
 892 the containing package to at least
 893 the <span class="code">javafx.fxml</span> module.</assert></span>
 894 
 895 <p>For example, the controllers from the previous examples could be rewritten as follows:</p>
 896 
 897 <pre class="code">
 898 package com.foo;
 899 
 900 public class MyController {
 901     @FXML
 902     private void handleButtonAction(ActionEvent event) {
 903         System.out.println("You clicked me!");
 904     }
 905 }
 906 </pre>
 907 
 908 <pre class="code">
 909 package com.foo;
 910 
 911 public class MyController implements Initializable {
 912     @FXML private Button button;
 913 
 914     @FXML
 915     protected void initialize()
 916         button.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
 917             @Override
 918             public void handle(ActionEvent event) {
 919                 System.out.println("You clicked me!");
 920             }
 921         });
 922     }
 923 }
 924 </pre>
 925 
 926 <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>
 927 
 928 <h3><a name="nested_controllers">Nested Controllers</a></h3>
 929 <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:
 930 
 931 <div class="caption">main_window_content.fxml</div>
 932 <pre class="code">
 933 &lt;VBox fx:controller="com.foo.MainController"&gt;
 934    &lt;fx:define&gt;
 935       &lt;fx:include fx:id="dialog" source="dialog.fxml"/&gt;
 936    &lt;/fx:define&gt;
 937    ...
 938 &lt;/VBox&gt;
 939 </pre>
 940 
 941 <div class="caption">MainController.java</div>
 942 <pre class="code">
 943 public class MainController extends Controller {
 944     @FXML private Window dialog;
 945     @FXML private DialogController dialogController;
 946 
 947     ...
 948 }
 949 </pre>
 950 
 951 <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>
 952 
 953 <h2><a name="fxmlloader">FXMLLoader</a></h2>
 954 <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>
 955 
 956 <pre class="code">
 957 URL location = getClass().getResource("example.fxml");
 958 ResourceBundle resources = ResourceBundle.getBundle("com.foo.example");
 959 FXMLLoader fxmlLoader = new FXMLLoader(location, resources);
 960 
 961 Pane root = (Pane)fxmlLoader.load();
 962 MyController controller = (MyController)fxmlLoader.getController();
 963 </pre>
 964 
 965 <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>
 966 
 967 <h3><a name="custom_components">Custom Components</a></h3>
 968 <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>
 969 
 970 <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>
 971 
 972 <pre class="code">
 973 &lt;?import javafx.scene.*?&gt;
 974 &lt;?import javafx.scene.control.*?&gt;
 975 &lt;?import javafx.scene.layout.*?&gt;
 976 
 977 &lt;fx:root type="javafx.scene.layout.VBox" xmlns:fx="http://javafx.com/fxml"&gt;
 978     &lt;TextField fx:id="textField"/&gt;
 979     &lt;Button text="Click Me" onAction="#doSomething"/&gt;
 980 &lt;/fx:root&gt;
 981 </pre>
 982 
 983 <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>
 984 
 985 <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>
 986 
 987 <pre class="code">
 988 package fxml;
 989 
 990 import java.io.IOException;
 991 
 992 import javafx.beans.property.StringProperty;
 993 import javafx.fxml.FXML;
 994 import javafx.fxml.FXMLLoader;
 995 import javafx.scene.control.TextField;
 996 import javafx.scene.layout.VBox;
 997 
 998 public class CustomControl extends VBox {
 999     @FXML private TextField textField;
1000 
1001     public CustomControl() {
1002         FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("custom_control.fxml"));
1003         fxmlLoader.setRoot(this);
1004         fxmlLoader.setController(this);
1005 
1006         try {
1007             fxmlLoader.load();
1008         } catch (IOException exception) {
1009             throw new RuntimeException(exception);
1010         }
1011     }
1012 
1013     public String getText() {
1014         return textProperty().get();
1015     }
1016 
1017     public void setText(String value) {
1018         textProperty().set(value);
1019     }
1020 
1021     public StringProperty textProperty() {
1022         return textField.textProperty();
1023     }
1024 
1025     @FXML
1026     protected void doSomething() {
1027         System.out.println("The button was clicked!");
1028     }
1029 }
1030 </pre>
1031 
1032 <p>Now, callers can use instances of this control in code or in markup, just like any other control; e.g.:</p>
1033 
1034 <div class="caption">Java</div>
1035 <pre class="code">
1036 HBox hbox = new HBox();
1037 CustomControl customControl = new CustomControl();
1038 customControl.setText("Hello World!");
1039 hbox.getChildren().add(customControl);
1040 </pre>
1041 
1042 <div class="caption">FXML</div>
1043 <pre class="code">
1044 &lt;HBox&gt;
1045     &lt;CustomControl text="Hello World!"/&gt;
1046 &lt;/HBox&gt;
1047 </pre>
1048 
1049 <h2><a name="deploy_as_module">Deploying an Application as a Module</a></h2>
1050 <p>If <span class="code">FXMLLoader</span> is used to load types in a named
1051 module, the application must ensure that all types that are referenced in the
1052 FXML files, including the controller class and any custom <span class="code">Node</span>
1053 classes, are reflectively accessible to the <span class="code">javafx.fxml</span>
1054 module. A type is reflectively accessible if the module
1055 <span class="code">opens</span> the containing package to at least the
1056 <span class="code">javafx.fxml</span> module.
1057 </p>
1058 
1059 <p>For example, if <span class="code">com.foo.MyController</span> is in the
1060 <span class="code">foo.app</span> module, the
1061 <span class="code">module-info.java</span> might look like this:
1062 </p>
1063 <pre><span class="code">module foo.app {
1064     opens com.foo to javafx.fxml;
1065 }</span></pre>
1066 
1067 <p>Alternatively, a type is reflectively accessible if the module
1068 <span class="code">exports</span> the containing package unconditionally.
1069 </p>
1070 <hr>
1071 <p>
1072 <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>
1073 </p>
1074 </body>
1075 </html>