2

I am trying to call a Java Function as a callback from JavaScript using JavaFX. First, I added the Java class as a member of window during initialization:

JSObject jsobj = (JSObject) currentWebEngine.executeScript("window");
jsobj.setMember("java", this);

The Java request function is described as follows inside the Platform.runLater():

String script = "document.makeRequest('"+inputString+"')";
currentWebEngine.executeScript(script);

After the asynchronous request is done in the javascript side, it should call a java function to return the result. However, I am getting exceptions in that side. I even tried calling a function without argument and still doesn't work:

window.java.returnResult();

But I get the following exception:

Exception in runnable netscape.javascript.JSException: TypeError: 'undefined' is not an object

It has nothing to do with the asynchronous request since I even tried calling it directly in makeRequest() function and got the same error. Any idea what I did wrong in that area? The previous code is integrated in a Desktop JFrame application, not an applet.

1
  • Does the example demonstrating WebView to Java callbacks in the Oracle WebView tutorial work for you? Commented May 6, 2013 at 20:07

2 Answers 2

3

I managed to find the solution of the problem. The following code was called a bit early and therefore the class is not registered within javascript:

JSObject jsobj = (JSObject) currentWebEngine.executeScript("window");
jsobj.setMember("java", this);

I just called it at the listener of the web engine and it worked.

Sign up to request clarification or add additional context in comments.

Comments

0

Use DWR, You can use java methods through JavaScript.

1 Comment

As far as I can see, DWR requires a Java Server to communicate with a Web browser. That is not what I am aiming to do. I have a Swing desktop Application that embeds JavaScript using JavaFX

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.