WebView – JavaFX 8 webengine: how to from Java to console Log() from Java to system out?

I use JavaFX and JavaScript engine to develop applications in JavaFX web engine I want to get feedback from JavaScript for debugging What happens to console output in web engine? Is there any way to access it or redirect to system. Net in Java out?

Solution

The following code will console Log() redirects to javabridge log():

import netscape.javascript.JSObject;

public class JavaBridge
{
    public void log(String text)
    {
        System.out.println(text);
    }
}

[...]

webEngine.getLoadWorker().stateproperty().addListener((observable,oldValue,newValue) ->
{
    JSObject window = (JSObject) webEngine.executeScript("window");
    JavaBridge bridge = new JavaBridge();
    window.setMember("java",bridge);
    webEngine.executeScript("console.log = function(message)\n" +
        "{\n" +
        "    java.log(message);\n" +
        "};");
});
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>