I am experimenting with using JavaFX 2 from JavaScript through the JavaScripting API (Rhino).
Note: This is a rich-client question, nothing to do with web-programming.
I would like to orchestrate some parts of my JavaFX rich-client application with dynamic scripts. I am evaluating if the JavaScripting API with the bundled JavaScript Runtime (Rhino) would be a good approach for my requirements.
So my first attempt was to create a simple JavaFX-GUI through JavaScript:
print('Starting...')
importPackage(Packages.javafx.scene);
importPackage(Packages.javafx.scene.layout);
importPackage(Packages.javafx.stage);
importClass(Packages.javafx.scene.layout.StackPane);
importClass(Packages.javafx.scene.Scene);
importClass(Packages.javafx.stage.Stage);
myroot = new Packages.javafx.scene.layout.StackPane();
myscene = new Packages.javafx.scene.Scene(myroot);
mystage = new Packages.javafx.stage.Stage();
mystage.setScene(myscene);
mystage.show();
I am then running this script with jsrunscript. The inspiration for that script came from the Oracle documentation.
However the script already fails at line 4:
> ☹ jrunscript -f script.js
> Starting...script error in file script.js :
> sun.org.mozilla.javascript.internal.EvaluatorException: Function importClass must be called with a class; had "[JavaPackage javafx.scene.layout.StackPane]" instead. (script.js#4) in script.js at line number 6
It looks like I can't import the JavaFx package/classes into the javascript runtime. Although the oracle documentation states to this with java.awt.
What am I doing wrong?