I have a Java class that loads content into a WebView
. This WebView contains a JavaScript program that I need to query to, eg, populate lists of GUI components, call methods that return primitives, etc.
The problem for me is that WebViews can only be interacted with on the JavaFX application thread, and loading happens on a background thread. This means that the JS program loads ~500ms after my class is initialised, so for this period, my class is invalid and any method calls that query the JS program will return some sort of exception (eg, because the DOM object does not yet exist). However, I can’t wait on it, because that locks the JavaFX application thread and loading never completes.
I want to be able to query the JS program in such a way that calling classes can get a handle on a future of an answer, but any attempt I’ve made at this ends up deadlocking the JavaFX application thread.
In case it helps to demonstrate an answer, this is a minimal example of a similar class, that loads a CDN’ed JS text editor, and queries a variable defined once the page is loaded.
In this example, I want a way for classes interacting with WithWebView
objects to be able to call getX
in a “safe” way such that they (eventually) get the answer. I gather I should use CompletableFuture<?>
here to do this, but I’m somehow not getting my head around how I can wait for the page to finish loading and still return a value in the future. The only “safe” way is the one shown below, where I add a callback to the stateProperty
of the WebEngine
‘s loadWorker
.
package com.example.demowebview;
import javafx.concurrent.Worker;
import javafx.scene.Parent;
import javafx.scene.web.WebView;
public class WithWebView {
private WebView webView;
WithWebView() {
webView = new WebView();
String content = """
<html>
<script src=" https://cdn.jsdelivr.net/npm/[email protected]/src-min-noconflict/theme-chrome.min.js "></script>
<script>
x = "Answer"
</script>
</html>
""";
webView.getEngine().loadContent(content);
// System.out.println(getX()); // this will throw an exception, because loading takes some time
webView.getEngine().getLoadWorker().stateProperty().addListener((v, o, n) -> {
if (n == Worker.State.SUCCEEDED) {
System.out.println(webView.getEngine().executeScript("x")); // this will work because I'm waiting on the loadworker to finish
}
});
}
Parent getParent() {
return webView;
}
public String getX() {
return (String)webView.getEngine().executeScript("x");
}
}
with a simple application wrapper:
package com.example.demowebview;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class HelloApplication extends Application {
@Override
public void start(Stage stage) {
WithWebView wwv = new WithWebView();
Scene scene = new Scene(wwv.getParent(), 320, 240);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}