Java Reference
In-Depth Information
$STAGE.setTitle("Hello FX");
$STAGE.setScene(new javafx.scene.Scene(sp, 300, 100));
$STAGE.sizeToScene();
When you run the script in Listing 11-5, it throws an exception, as shown:
jjs -fx incorrectfxapp.js
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in
Application start method
at com.sun.javafx.application.LauncherImpl.
launchApplication1(LauncherImpl.java:875)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication
$149(LauncherImpl.java:157)
at com.sun.javafx.application.LauncherImpl$$Lambda$1/23211803.
run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassCastException: Cannot cast
jdk.nashorn.internal.runtime.Undefined to javafx.scene.Parent
at java.lang.invoke.MethodHandleImpl.newClassCastException
(MethodHandleImpl.java:364)
...
The exception is thrown when you try to create the scene using the global variable
sp , which is a reference to a StackPane . Contrary to expectations, the init() method
is not called before running the code in the global scope. The code in the global scope
is called before the init() function is automatically called. In the script, the init()
method creates the controls to be added to the scene. When the scene is being created,
the variable sp is still undefined , which causes the exception. If you show the primary
stage in the script, the init() function is called after the primary stage is already shown.
If you let Nashorn show the primary stage for you, the init() function is called before the
primary stage.
if you do not provide a start() function in a JavaFX script, providing the init()
function is almost useless because such an init() function will be called after the
primary stage has been constructed. if you want to use the init() function to initialize your
JavaFX application, you should provide both init() and start() methods so that they are
called in order.
Tip
 
Search WWH ::




Custom Search