Geertjan's Blog

  • July 1, 2012

Invoking JavaScript from Java

Geertjan Wielenga
Product Manager

Here's an Action class defined in Java. The Action class executes a script via the JavaFX WebEngine:

@NbBundle.Messages("CTL_AddBananasAction=Add Banana")
private class AddBananasAction extends AbstractAction {
public AddBananasAction() {
public void actionPerformed(ActionEvent e) {
Platform.runLater(new Runnable() {
public void run() {
webengine.executeScript("addBanana(' " + newBanana + " ') ");
How does the 'executescript' call know where to find the JavaScript file? Well, earlier in the code, the WebEngine loaded an HTML file, where the JavaScript file was registered:
WebView view = new WebView();
view.setMinSize(widthDouble, heightDouble);
view.setPrefSize(widthDouble, heightDouble);
webengine = view.getEngine();
URL url = getClass().getResource("home.html");

Finally, here's a skeleton 'addBanana' method, which is invoked via the Action class shown above:

function addBanana(user){

By the way, if you have your JavaScript and CSS embedded within your
HTML file, the code navigator combines all three into the same window,
which is kind of cool:

Join the discussion

Comments ( 3 )
  • Andreas Monday, July 2, 2012

    Interesting. It appears that JavaFX is somewhat akin to Wicket. Unlike Wicket, I assume that JavaFX has had no problem integrating jQuery; is this true? Which leads me to another question: is there a direct way to call JavaScript other than by referencing an HTML file?

  • guest Monday, July 2, 2012


    Wicket is a server side web application framework which was built, in many ways, to mimic Swing's coding style. JavaFX is a replacement for Swing. So it's not surprising that from a developers point of view they appear to mimic each other, especially in a context like the one in this blog post where JavaFX is wrapping an html page. However the execution models of the two frameworks (client side vs server side) are quite different.

    I integrated a jQueryUI DatePicker component with JavaFX and had no problems whatsoever with it - jQuery embedded in a WebView basically just works.

    You don't call JavaScript from a WebView by referencing an HTML file, you invoke a webView.getEngine().executeScript(<some javascript here>) command. This will execute your javascript in the context of the environment of the current document and javascript functions loaded into WebView.

  • Raj Friday, July 6, 2012

    Thanks for the article Geertjan. May be a slight deviation from the message in the article - WebView is still not a platform that can be relied on for rendering web content. It is ages old when compared to performance, rendering quality etc, as it just holds the bare-bones webkit. In contrast, everyone other alternative where they have packaged webkit into an application, it is just amazing - be it Adobe Air, Titanium desktop, Berkelinium etc or in cases where they have embedded 'chromium embedded framework' in to the apps.

    Will be great if you can influence getting javafx to support webkit in the nicer way if possible.

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.