Java Reference
In-Depth Information
Listing 9.14
JavaFX - deferAction()
// The applet uses this to set the current Image shown
public function setCurrentPic (ndx:Integer) : Void {
FX.deferAction (function(): Void {
try {
imageList.setIndex(ndx);
} catch(ex: java.lang.Exception) {
applet.eval("alert('{ex}')");
}
});
}
You can also set and get any public script variables defined in the JavaFX applet.
In the next example, we want to change the fill script variable in the NASAImage-
Browser applet to silver. The JavaScript code in Listing 9.15 shows how to do this.
Listing 9.15
HTML-JavaScript Access Public JavaFX Variables
<script>
document.app.script.fill =
document.app.Packages.javafx.scene.paint.Color.SILVER;
....
</script>
The Packages per-Applet keyword provides JavaScript access to the JavaFX and
Java packages visible to the JavaFX applet. In the preceding example, we accessed
the script variable, SILVER , from the javafx.scene.paint.Color class.
You can also use the Packages keyword to instantiate new Java objects in the
JavaScript code. Instantiating JavaFX objects within JavaScript is not currently
supported. Listing 9.16 shows how to do this.
Listing 9.16
HTML-JavaScript - Instantiate Java Objects
var point = new document.app.Packages.java.awt.Point();
point.x = 100;
point.y = 150;
The main restriction when accessing JavaFX applets from JavaScript is that you
can only directly access the public script variables and functions defined for the
JavaFX applet. If you need to access instance variables or functions, you need to
create a script level variable or function that lets you get to the instance. For
example, the JavaFX code could be structured as shown in Listing 9.17.
 
Search WWH ::




Custom Search