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