Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
Does the example demonstrating WebView to Java callbacks in the Oracle WebView tutorial work for you? – jewelsea yesterday

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer

Use DWR, You can use java methods through JavaScript.

share|improve this answer
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 – Sami 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.