【问题标题】:Javafx calling javascript to excute java function in webview not workingJavafx调用javascript在webview中执行java函数不起作用
【发布时间】:2018-02-23 19:45:05
【问题描述】:

根据 JavaFX 文档,您可以使用 JavaScript 函数执行 Java 代码。以下是我的代码:

engine = webview.getEngine();
engine.load("http://localhost:8080/testing.php");
openExcel callback = new openExcel();
JSObject window = (JSObject) engine.executeScript("window");
window.setMember("app", callback);

上面是在初始化方法中,然后对于另一个类(openExcel)我有这样的东西:

public class openExcel {

    public void open() {
        if (Desktop.isDesktopSupported()) {
            try {
                File myFile = new File("C:\\Users\\HP\\Desktop\\v3.01.xlsm");
                Desktop.getDesktop().open(myFile);
            } catch(IOException ex) {
                System.out.println("Your application is not support");
            }
        }
    }

}

HTML 文件:

<html>
    <head>
        <script>
            function openExcel() {
                app.open();
                alert('hello world');
            }
        </script>
    </head>
    <body>
        <button onclick="openExcel()">Open excel</button>
    </body>

我面临的问题是,当我单击“openExcel”按钮时,它什么也不做?我需要帮助!

【问题讨论】:

    标签: javascript java javafx javafx-webengine


    【解决方案1】:

    我已尝试重现您的问题,但您的网桥似乎正在抛出错误/消息,并且您看不到输出。我使用您的代码创建了一个带有 JavaScript 消息侦听器的简单测试:

    public class WebEngineTest extends Application {
    
        @Override
        public void start(Stage primaryStage) {
            WebConsoleListener.setDefaultListener((webView, message, lineNumber, sourceId) -> {
                System.out.println(message + "[at " + lineNumber + "]");
            });
    
            WebView webView = new WebView();
            WebEngine engine = webView.getEngine();
            engine.loadContent("<html><head><script>function openExcel() { app.open(); alert('hello world'); } </script></head><body><button onclick=\"openExcel()\">Open excel</button></body></html>");
    
            JSObject window = (JSObject) engine.executeScript("window");
            window.setMember("app", new OpenExcel());
    
            Scene scene = new Scene(webView, 300, 150);
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        public class OpenExcel {
    
            public void open() {
                if (!Desktop.isDesktopSupported()) {
                    throw new RuntimeException("Desktop is not supported");
                }
    
                try {
                    File myFile = new File("C:\\Users\\HP\\Desktop\\v3.01.xlsm");
                    // File myFile = new File("C:\\Users\\dzikoysk\\Desktop\\panda.txt");
                    Desktop.getDesktop().open(myFile);
                } catch(IOException ex) {
                    System.out.println("Your application is not support");
                }
            }
    
        }
    
    }
    

    如果我删除 System.out.println(message + "[at " + lineNumber + "]"); 并且文件不存在或桌面不受支持,那么 什么都不会发生,但消息侦听器:

    所以我已将我的 PC 上存在的 myFile 更新为 new File("C:\\Users\\dzikoysk\\Desktop\\panda.txt");,现在一切正常:

    无论如何,您的代码中缺少&lt;/html&gt; 标签。在某些旧版本的 WebEngine 中,它也可能导致问题,请注意此类细节 - 引擎有很多错误并且未实现的功能。

    【讨论】:

    • 我已经添加了缺少的 html 标签,现在它可以正常工作了!!谢谢!
    猜你喜欢
    • 2013-10-13
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多