Skip to content Skip to sidebar Skip to footer

Invoke Java Using Javascript Inside Javafx. Is It Possible?

I am using WebEngine & WebView from JavaFX. Now I want to execute Java using javascript running inside WebEngine. My question is if it is possible to do so and if yes any hints

Solution 1:

I didn't manage to create Java instances but the thing I managed to do is to push object instances created in Java into JavaScript and call back to them.

So my Java-Code looks like this:

JSObjectwin= (JSObject) engine.executeScript("window");
win.setMember("jHelper", newJavaHelper());

JavaHelper example (must be public):

publicstaticclassJavaHelper {
    publicintnewInteger(int input) {
        // ...
    }
    public Random newRandom() {
        // ...
    }
}

And then then in JavaScript:

function bla() {
  var number = jHelper.newInteger(1234);
  var random = jHelper.newRandom();
  // ...
}

You can see my work where I communicate between Java and JavaScript back and forth at https://github.com/tomsontom/fx-ide/tree/master/at.bestsolution.javafx.ide.editor and in action at http://tomsondev.bestsolution.at/2012/10/29/eclipsecon-javafx-demo-app-videos/

Post a Comment for "Invoke Java Using Javascript Inside Javafx. Is It Possible?"