< prev index next >

modules/javafx.fxml/src/main/docs/javafx/fxml/doc-files/introduction_to_fxml.html

Print this page
*** 27,11 ***
   */
  -->
  
  <html lang="en">
  <head>
! <link href="fxml.css" type="text/css" rel="stylesheet"/>    
  <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
  <title>Introduction to FXML | JavaFX @FXVERSION@</title>
  <meta name="description" content="The document introduces FXML, an XML-based declarative markup language for defining user interfaces in JavaFX @FXVERSION@ applications."/>
  <meta name="keywords" content="JavaFX, FXML, JavaFX GUI development, web development, Java application development, GUI applications, rich internet applications, RIA, expressive content"/>
  </head>
--- 27,11 ---
   */
  -->
  
  <html lang="en">
  <head>
! <link href="fxml.css" type="text/css" rel="stylesheet"/>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
  <title>Introduction to FXML | JavaFX @FXVERSION@</title>
  <meta name="description" content="The document introduces FXML, an XML-based declarative markup language for defining user interfaces in JavaFX @FXVERSION@ applications."/>
  <meta name="keywords" content="JavaFX, FXML, JavaFX GUI development, web development, Java application development, GUI applications, rich internet applications, RIA, expressive content"/>
  </head>

*** 595,17 ***
   <tr><th scope="row">null</th><td>A constant representing the null value</td></tr>
   <tr><th scope="row">50.0<br />3e5<br />42</th><td>A numerical constant</td></tr>
   <tr><th scope="row">- <br/>(unary operator)</th><td>Unary minus operator, applied on a number</td>
   <tr><th scope="row">! <br/>(unary operator)</th><td>Unary negation of a boolean</td></tr>
   <tr><th scope="row">+ - <br />
!  			* /
!  			%</th> <td>Numerical binary operators</td></tr>
   <tr><th scope="row">&amp;&amp; ||</th><td>Boolean binary operators</td></tr>
   <tr><th scope="row">&gt; &gt;= <br />
!  		   &lt; &lt;= <br />
!  		   == !=</th>
!  		   <td>Binary operators of comparison.<br/> Both arguments must be of type Comparable</td></tr>
  </table>
  
  <h3><a id="static_property_attributes">Static Properties</a></h3>
  <p>Attributes representing static properties are handled similarly to static property elements and use a similar syntax. For example, the earlier <span class="code">GridPane</span> markup shown earlier to demonstrate static property elements could be rewritten as follows:</p>
  
--- 595,17 ---
   <tr><th scope="row">null</th><td>A constant representing the null value</td></tr>
   <tr><th scope="row">50.0<br />3e5<br />42</th><td>A numerical constant</td></tr>
   <tr><th scope="row">- <br/>(unary operator)</th><td>Unary minus operator, applied on a number</td>
   <tr><th scope="row">! <br/>(unary operator)</th><td>Unary negation of a boolean</td></tr>
   <tr><th scope="row">+ - <br />
!                         * /
!                         %</th> <td>Numerical binary operators</td></tr>
   <tr><th scope="row">&amp;&amp; ||</th><td>Boolean binary operators</td></tr>
   <tr><th scope="row">&gt; &gt;= <br />
!                    &lt; &lt;= <br />
!                    == !=</th>
!                    <td>Binary operators of comparison.<br/> Both arguments must be of type Comparable</td></tr>
  </table>
  
  <h3><a id="static_property_attributes">Static Properties</a></h3>
  <p>Attributes representing static properties are handled similarly to static property elements and use a similar syntax. For example, the earlier <span class="code">GridPane</span> markup shown earlier to demonstrate static property elements could be rewritten as follows:</p>
  

*** 639,10 ***
--- 639,12 ---
  &lt;/VBox&gt;
  </pre>
  
  <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>
  
+ <p>Note: to turn off automatic compilation of script code place the processing instruction <span class="code">&lt;?compile false?&gt;</span> before the element that contains the script. To turn on compilation of script code again use the processing instruction <span class="code">&lt;?compile true?&gt;</span> (or short: <span class="code">&lt;?compile?&gt;</span>). The compile processing instruction can be used repeatedly to turn compilation of script code off and on.</p>
+ 
  <h4><a id="controller_method_event_handlers">Controller Method Event Handlers</a></h4>
  <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>
  
  <p>A controller method event handler is specified by a leading hash symbol followed by the name of the handler method. For example:</p>
  

*** 698,19 ***
      &lt;/children&gt;
  &lt;/VBox&gt;
  </pre>
  
  <p> With the controller that contains a field like this </p>
!     
  <pre class="code">
  public class MyController {
!     
      &#64;FXML
      public EventHandler&lt;ActionEvent&gt; onActionHandler = new EventHandler&lt;&gt;() { ... }
  
      ...
! }  
  </pre>
  
  <p> Note that other kinds of expressions, like <a href="#expression_binding">binding expressions</a>
      are not supported in this context. </p>
  
--- 700,19 ---
      &lt;/children&gt;
  &lt;/VBox&gt;
  </pre>
  
  <p> With the controller that contains a field like this </p>
! 
  <pre class="code">
  public class MyController {
! 
      &#64;FXML
      public EventHandler&lt;ActionEvent&gt; onActionHandler = new EventHandler&lt;&gt;() { ... }
  
      ...
! }
  </pre>
  
  <p> Note that other kinds of expressions, like <a href="#expression_binding">binding expressions</a>
      are not supported in this context. </p>
  

*** 761,18 ***
  
  <pre class="code">
  &lt;VBox fx:controller="com.foo.MyController"
      xmlns:fx="http://javafx.com/fxml" onParentChange="#handleParentChange"/&gt;
  </pre>
!     
  <p>Note that collections and properties do not currently support scripting handlers.</p>
  
  <h2><a id="scripting">Scripting</a></h2>
  <p>
  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.</p>
  
! <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>
  
  <pre class="code">
  &lt;?language javascript?&gt;
  
  &lt;?import javafx.scene.control.*?&gt;
--- 763,22 ---
  
  <pre class="code">
  &lt;VBox fx:controller="com.foo.MyController"
      xmlns:fx="http://javafx.com/fxml" onParentChange="#handleParentChange"/&gt;
  </pre>
! 
  <p>Note that collections and properties do not currently support scripting handlers.</p>
  
  <h2><a id="scripting">Scripting</a></h2>
  <p>
  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.</p>
  
! <p>Scripts are compiled by default, when they are first loaded, if the <span class="code">ScriptEngine</span> implements the <span class="code">javax.script.Compilable</span> interface. If compilation fails, the <span class="code">FXMLLoader</span> will fall back to interpreted mode.</p>
+ 
+ <p>Note: to turn off automatic compilation of script code place the processing instruction <span class="code">&lt;?compile false?&gt;</span> before the script element. To turn on compilation of script code again use the processing instruction <span class="code">&lt;?compile true?&gt;</span> (or short: <span class="code">&lt;?compile?&gt;</span>). The compile processing instruction can be used repeatedly to turn compilation of script code off and on.</p>
+ 
+ <p>The following example 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>
  
  <pre class="code">
  &lt;?language javascript?&gt;
  
  &lt;?import javafx.scene.control.*?&gt;

*** 796,10 ***
--- 802,12 ---
  
  <p>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:</p>
  
  <div class="caption">example.fxml</div>
  <pre class="code">
+ &lt;?language javascript?&gt;
+ 
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  
  &lt;VBox xmlns:fx="http://javafx.com/fxml"&gt;
      &lt;fx:script source="example.js" charset="cp1252"/&gt;

*** 831,21 ***
  ...
  
  &lt;Label text="$myText"/&gt;
  </pre>
  
! 
- <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>
  
  <pre class="code">
  load("nashorn:mozilla_compat.js");
  importClass(java.lang.System);
  
  function handleButtonAction(event) {
     System.out.println('You clicked me!');
  }
! </pre> 
  
  <h2><a id="controllers">Controllers</a></h2>
  <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>
  
  <p>As shown earlier, controllers are often used to implement event handlers for user interface elements defined in markup:</p>
--- 839,20 ---
  ...
  
  &lt;Label text="$myText"/&gt;
  </pre>
  
! <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>
  
  <pre class="code">
  load("nashorn:mozilla_compat.js");
  importClass(java.lang.System);
  
  function handleButtonAction(event) {
     System.out.println('You clicked me!');
  }
! </pre>
  
  <h2><a id="controllers">Controllers</a></h2>
  <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>
  
  <p>As shown earlier, controllers are often used to implement event handlers for user interface elements defined in markup:</p>
< prev index next >