Javafx 调用 java 脚本在 webview 中执行 java 函数不起作用
Javafx calling javascript to excute java function in webview not working
根据 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);
上面是在初始化方法中,然后对于其他class(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" 时,它什么也没做?我需要帮助!
我已尝试重现您的问题,您的桥似乎正在抛出 error/message 并且您看不到输出。我用你的代码用 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 + "]");
并且文件不存在或桌面不受支持,那么 什么都不会发生 但消息侦听器:
所以我已经将 myFile
更新为 new File("C:\Users\dzikoysk\Desktop\panda.txt");
,它存在于我的电脑上,现在一切正常:
无论如何,您的代码中缺少 </html>
标记。在某些较旧版本的 WebEngine 中,它也可能导致问题,请注意此类细节 - 引擎有很多错误且未实现功能。
根据 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);
上面是在初始化方法中,然后对于其他class(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" 时,它什么也没做?我需要帮助!
我已尝试重现您的问题,您的桥似乎正在抛出 error/message 并且您看不到输出。我用你的代码用 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 + "]");
并且文件不存在或桌面不受支持,那么 什么都不会发生 但消息侦听器:
所以我已经将 myFile
更新为 new File("C:\Users\dzikoysk\Desktop\panda.txt");
,它存在于我的电脑上,现在一切正常:
无论如何,您的代码中缺少 </html>
标记。在某些较旧版本的 WebEngine 中,它也可能导致问题,请注意此类细节 - 引擎有很多错误且未实现功能。