来自
WebEngine文档:
Loading always happens on a background thread. Methods that initiate
loading return immediately after scheduling a background job. To track
progress and/or cancel a job, use the Worker instance available from
the getLoadWorker() method.
我有一个HTML string,我通过WebEngine.loadContent(String)加载到WebView上.这个字符串大约有500万个字符.在Platform.runLater()中运行它(我必须在JavaFX线程中运行它,否则我得到一个错误)我的UI挂起大约一分钟.
如果我不在Platform.runLater()中运行它,我得到:
java.lang.IllegalStateException: Not on FX application thread; currentThread = populator
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:236)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:423)
at javafx.scene.web.WebEngine.checkThread(WebEngine.java:1216)
at javafx.scene.web.WebEngine.loadContent(WebEngine.java:931)
at javafx.scene.web.WebEngine.loadContent(WebEngine.java:919)
...
我不能通过webEngine.getLoadWorker().getProgress()查询webEngine,也不能通过webEngine.getLoadWorker()取消取消.因为我必须再次在挂起的JavaFX线程上运行它…
所以我必须等到页面加载,然后先前提交的任何Platform.runLater(() – > webEngine.getLoadWorker().getProgress())(在网页加载过程中)将运行,每次给我1.0. ..
我用来查询Worker的代码:
// WebView
wvIn.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
class ProgressThread extends Thread {
private Worker.State loadWorkerState;
synchronized Worker.State getLoadWorkerState() {
return loadWorkerState;
}
synchronized void setLoadWorkerState(Worker.State loadWorkerState) {
this.loadWorkerState = loadWorkerState;
}
{
setDaemon(true);
setName("LoadingWebpageProgressThread");
}
public void run() {
while (true) {
try {
if (getLoadWorkerState() == Worker.State.RUNNING)
// piWv ProgressIndicator (WebView loading)
Platform.runLater(() -> piWv.setVisible(true));
while (getLoadWorkerState() == Worker.State.RUNNING) {
Platform.runLater(() -> {
piWv.setProgress(wvIn.getEngine().getLoadWorker().getProgress());
// TODO delete
System.out.println(wvIn.getEngine().getLoadWorker().getProgress());
});
Thread.sleep(100);
}
if (getLoadWorkerState() == Worker.State.SUCCEEDED) {
Platform.runLater(() -> piWv.setProgress(1d));
Thread.sleep(100);
Platform.runLater(() -> {
piWv.setVisible(false);
piWv.setProgress(0d);
});
}
synchronized (this) {
wait();
}
} catch (InterruptedException e) {
}
}
}
};
final ProgressThread progressThread = new ProgressThread();
{
progressThread.start();
}
// executed on JavaFX Thread
@Override
public void changed(ObservableValue<? extends State> observable, State oldValue, State newValue) {
if (newValue == State.SUCCEEDED) {
JSObject window = (JSObject) wvIn.getEngine().executeScript("window");
window.setMember("controller", mainController);
progressThread.setLoadWorkerState(newValue);
progressThread.interrupt();
} else if (newValue == State.RUNNING) {
progressThread.setLoadWorkerState(newValue);
progressThread.interrupt();
}
// TODO delete
System.out.println(oldValue + "->" + newValue);
}
});
反正是否强制加载后台线程?
JavaFX线程到底发生了什么?它是填充WebView的过程吗?
最佳答案 您在问题中显示的HTML文件几乎立即加载到我的计算机上.
在我看来,问题在于你的代码.嗯,这很麻烦.请允许我指出它看起来不太好,因为它容易出现deadlock,并使用例外来实现guarded-blocks的形式.
看起来你并不完全理解events的概念.
除非为了简洁起见以某种方式切断了问题中的代码片段,否则ProgressThread实例完全没用.
您已经根据需要调度了ChangeListener
个事件,并在JavaFX Application Thread上执行回调;只是使用它.
indicator.setVisible(false); // Start hidden
engine.getLoadWorker().progressProperty().addListener((observable, oldValue, newValue) -> {
indicator.setProgress(newValue.doubleValue());
indicator.setVisible(indicator.getProgress() != 1D);
});
或者,您可以使用binding的强大功能:
Worker<Void> worker = engine.getLoadWorker();
indicator.visibleProperty().bind(worker.stateProperty().isEqualTo(Worker.State.RUNNING));
indicator.progressProperty().bind(worker.progressProperty());
对于您的JavaScript代码,我建议您收听文档事件而不是工作状态事件:它们在您的情况下更有意义,并且在页面实际完成渲染时触发它们,而不仅仅是在页面数据完成下载时.
engine.documentProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == null) {
return;
}
JSObject window = (JSObject)engine.executeScript("window");
window.setMember("controller", mainController);
});
请注意,在我的机器上,加载过程中进度始终为零.我认为问题在于你的HTML代码:如果你试图加载一些其他资源,比如你现在正在阅读的网页,进度指示器将提供更愉快的体验,在页面内容完成之前多次更新加载.