Java Reference
In-Depth Information
In a Nashorn script, you can manage the lifecycle of a JavaFX application the same
way as you do in Java. You can have three functions named
init()
,
start()
, and
stop()
in
the script. Notice that all three functions are optional in a Nashorn script. These functions
correspond to the three methods in the Java class and they are called as follows, in order:
The
init()
function is called. You can initialize the
application in this function.
1.
The
start()
function is called. As is the case in a Java
application, the
start()
function is passed the reference to
the primary stage of the application. You need to populate the
scene, add the scene to the primary stage, and show the stage.
2.
The
stop()
function is called when the JavaFX application exits.
3.
■
if you do not have the
start()
function in a script for a JavaFX application, the
entire script in the global scope is considered the code for the
start()
function. You can
have functions other than these three functions. they will be treated as functions without
assigning any special meaning to them. those functions will not be called automatically; you
will need to call them in your script.
Tip
Listing 11-1 contains code for a simple JavaFX application in Java. It displays a window
with a
Text
node in a
StackPane
. The
HelloFX
class contains the
init()
,
start()
, and
stop()
methods. Figure
11-1
shows the window displayed by the
HelloFX
application. When
you exit the application the
stop()
method displays a message on the standard output.
Listing 11-1.
The HelloFX Application in Java
// HelloFX.java
package com.jdojo.script;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class HelloFX extends Application {
private Text msg;
private StackPane sp;
public static void main(String[] args) {
Application.launch(HelloFX.class);
}
Search WWH ::
Custom Search