1 #Usage: jjs -fx fxml_example.js
   2 #nashorn simple example using FXML with #javafx
   3 
   4 /*
   5  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *   - Redistributions of source code must retain the above copyright
  12  *     notice, this list of conditions and the following disclaimer.
  13  *
  14  *   - Redistributions in binary form must reproduce the above copyright
  15  *     notice, this list of conditions and the following disclaimer in the
  16  *     documentation and/or other materials provided with the distribution.
  17  *
  18  *   - Neither the name of Oracle nor the names of its
  19  *     contributors may be used to endorse or promote products derived
  20  *     from this software without specific prior written permission.
  21  *
  22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  23  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33  */
  34  
  35 // See also https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html
  36  
  37 if (! $OPTIONS._fx) {
  38     print("Usage: jjs -fx fxml_example.js");
  39     exit(1);
  40 }
  41  
  42 // inline FXML document here
  43 var fxml = <<EOF
  44  
  45 <?import javafx.scene.*?>
  46 <?import javafx.scene.control.*?>
  47 <?import javafx.scene.layout.*?>
  48  
  49 <VBox xmlns:fx="http://javafx.com/fxml">
  50     <children>
  51     <!-- ids will be used script later -->
  52     <HBox>
  53         <Label text="Your name please:"/>
  54         <TextField fx:id="nameText" text="Nashorn"/>
  55     </HBox>
  56     <Button fx:id="clickButton" text="Click!"/>
  57     </children>
  58 </VBox>
  59  
  60 EOF
  61  
  62 // Java and FX classes used
  63 var ByteArrayInputStream = Java.type("java.io.ByteArrayInputStream");
  64 var FXMLLoader = Java.type("javafx.fxml.FXMLLoader");
  65 var Scene = Java.type("javafx.scene.Scene");
  66  
  67 function start(stage) {
  68     var loader = new FXMLLoader();
  69     // load FXML from a string
  70     var root = loader.load(new ByteArrayInputStream(fxml.getBytes("UTF-8")));
  71  
  72     // get the button and the text field controls
  73     var button = root.lookup("#clickButton");
  74     var textField = root.lookup("#nameText");
  75  
  76     // event handler for button
  77     var clickCount = 0;
  78     button.onAction = function() {
  79         print(textField.text + ", you clicked me: " + ++clickCount + " time(s)");
  80     }
  81  
  82     var scene = new Scene(root, 300, 275);
  83     stage.title = "FXML Example";
  84     stage.scene = scene;
  85     stage.show();
  86 }