Java Reference
In-Depth Information
Finally, I will show you the simplest JavaFX application, which can be written in just
one line of script. It will show a message in a window. The window will not have title text,
however. Listing 11-6 contains the one-liner script. It shows the beauty of Nashorn, which
shrinks 10 to 15 lines of Java code into 1 line of script! The following command runs the
script that displays a window, as shown in Figure 11-2 :
jjs -fx simplestfxapp.js
Listing 11-6. The Simplest JavaFX Application in Nashorn
// simplestfxapp.js
$STAGE.scene = new javafx.scene.Scene(new javafx.scene.control.Label
("Hello JavaFX Scripting"));
Figure 11-2. The simplest JavaFX application using Nashorn script
It was an understatement that you need to write one line of code for the simplest
JavaFX application in Nashorn. The true statement is that you do not need to write even
one line of code in Nashorn to display a window. Create a script file named empty.js
and do not write any code in it. You can name the file anything else. Use the following
command to run the empty.js file:
jjs -fx empty.js
The command will display a window as shown in Figure 11-3 . How did Nashorn
display a window without you writing even one line of code? Recall that Nashorn creates
the primary stage and a global object $STAGE to represent that primary stage. If it sees
that you have not shown the primary stage, it shows it for you. That is what happened
in this case. The script file is empty and Nashorn automatically displayed the empty
primary stage.
Search WWH ::




Custom Search