Calling JavaFX code from JavaScript

The user interface callbacks we have talked about were our first example of calling JavaFX code from JavaScript. But you can use such calls not only for preset API methods but also for any logic, for example, as a reaction to a button press.

Let's see how it works.

First of all, you need to get access to the JavaScript window object as we already did in the previous section:

JSObject window = (JSObject) webEngine.executeScript("window");
Technically, it can be any JavaScript object, not only window. We used it for simplicity.

Now you can inject a random object into the window:

window.setMember("app", this);

Given that we call this code in the start() method, we are providing a link to the JavaFX application. ...

Get Mastering JavaFX 10 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.