Java Reference
In-Depth Information
You can simplify the script for the HelloFX application a bit more. You can remove
the start() function from the script. The JavaFX runtime creates and passes the
reference of the primary stage to the start() function. How will you get the reference
to the primary stage if you do not have the start() function? Nashorn creates a global
object named $STAGE , which is the reference to the primary stage. You can use this global
object to work with the primary stage. You do not even need to show the primary stage;
Nashorn will automatically show it for you.
Listing 11-3 contains the script that is another version for the same HelloFX
application. It uses the global object $STAGE to reference the primary stage. I have
removed the init() function. This time, you are not even calling the show() method of
the primary stage. You are letting Nashorn show the primary stage automatically for you.
The script is saved in the hellofx3.js file. You can run the script:
jjs -fx hellofx3.js
Listing 11-4. Another Version of the HelloFX Application without init(), start(), and
stop() Functions
// hellofx3.js
var msg = new javafx.scene.control.Label("Hello JavaFX from Nashorn!");
msg.font = javafx.scene.text.Font.font("Helvetica", 18);
var sp = new javafx.scene.layout.StackPane(msg);
$STAGE.setTitle("Hello FX");
$STAGE.setScene(new javafx.scene.Scene(sp, 300, 100));
$STAGE.sizeToScene();
// $STAGE.show(); // No need to show the primary stage. Nashorn will
// automatically show it.
Let's try one more combination of functions. You will provide the init() function
but not the start() function. Listing 11-5 contains the code for the same HelloFX
application. It contains the init() method that creates the controls, but the start()
method is removed.
Listing 11-5. Incorrect Implementation of JavaFX Application in a Nashorn Script
// incorrectfxapp.js
var msg;
var sp;
function init() {
msg = new javafx.scene.control.Label("Hello JavaFX from Nashorn!");
msg.font = javafx.scene.text.Font.font("Helvetica", 18);
sp = new javafx.scene.layout.StackPane(msg);
}
 
Search WWH ::




Custom Search