You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<title>Introduction to FXML | JavaFX @FXVERSION@</title>
35
35
<metaname="description" content="The document introduces FXML, an XML-based declarative markup language for defining user interfaces in JavaFX @FXVERSION@ applications."/>
<p>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. Scripting is discussed in more detail in the next section.</p>
643
643
644
+
<p>Note: to turn off automatic compilation of script code place the processing instruction <spanclass="code"><?compile false?></span> before the element that contains the script. To turn on compilation of script code again use the processing instruction <spanclass="code"><?compile true?></span> (or short: <spanclass="code"><?compile?></span>). The compile processing instruction can be used repeatedly to turn compilation of script code off and on.</p>
<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>
646
648
@@ -700,15 +702,15 @@ <h4><a id="expression_handlers">Event handlers from expressions</a></h4>
700
702
</pre>
701
703
702
704
<p> With the controller that contains a field like this </p>
703
-
705
+
704
706
<preclass="code">
705
707
public class MyController {
706
-
708
+
707
709
@FXML
708
710
public EventHandler<ActionEvent> onActionHandler = new EventHandler<>() { ... }
709
711
710
712
...
711
-
}
713
+
}
712
714
</pre>
713
715
714
716
<p> Note that other kinds of expressions, like <ahref="#expression_binding">binding expressions</a>
@@ -763,14 +765,18 @@ <h4><a id="collections_and_property_handlers">Special handlers for collections a
<p>Note that collections and properties do not currently support scripting handlers.</p>
768
770
769
771
<h2><aid="scripting">Scripting</a></h2>
770
772
<p>
771
773
The <spanclass="code"><fx:script></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.</p>
772
774
773
-
<p>For example, the following markup defines a function called <spanclass="code">handleButtonAction()</span> that is called by the action handler attached to the <spanclass="code">Button</span> element:</p>
775
+
<p>Scripts are compiled by default, when they are first loaded, if the <spanclass="code">ScriptEngine</span> implements the <spanclass="code">javax.script.Compilable</span> interface. If compilation fails, the <spanclass="code">FXMLLoader</span> will fall back to interpreted mode.</p>
776
+
777
+
<p>Note: to turn off automatic compilation of script code place the processing instruction <spanclass="code"><?compile false?></span> before the script element. To turn on compilation of script code again use the processing instruction <spanclass="code"><?compile true?></span> (or short: <spanclass="code"><?compile?></span>). The compile processing instruction can be used repeatedly to turn compilation of script code off and on.</p>
778
+
779
+
<p>The following example markup defines a function called <spanclass="code">handleButtonAction()</span> that is called by the action handler attached to the <spanclass="code">Button</span> element:</p>
<p><strong>Warning:</strong>As of JavaFX 8, <spanclass="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>
844
+
<p><strong>Warning:</strong> As of JavaFX 8, <spanclass="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>
<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 <spanclass="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>
0 commit comments