如何等待在JavaFX应用程序WebEngine /浏览器初始化? [英] How to wait for WebEngine/Browser initialization in JavaFx application?

查看:1051
本文介绍了如何等待在JavaFX应用程序WebEngine /浏览器初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个基于JavaFX的WebEngine定制FunctionPlotter组件。我的地块将在浏览器中显示。在我执行我的命令,情节我不得不等待,直到在浏览器已被初始化(加载d3.js)。目前,我通过把我的阴谋的前pressions在一个Runnable这样做,并传递到可运行的FunctionPlotter。 (该FunctionPlotter传递可运行到装载完成浏览器的钩):

I would like to create a custom FunctionPlotter component that is based on the JavaFx WebEngine. My plots will be shown in a browser. Before I execute my plot commands I have to wait until the browser has been initialized (it loads d3.js). Currently I do so by putting my plot expressions in a Runnable and pass that runnable to the FunctionPlotter. (The FunctionPlotter passes the runnable to the loading finished hook of the browser):

private FunctionPlotter plotter;
...

Runnable plotRunnable = ()->{
    plotter.plot("x^2");
}

plotter = new FunctionPlotter(plotRunnable);

不过,我想preFER以下为我FunctionPlotter组件的使用(阻塞)工作流程:

However I would prefer following (blocking) work flow for the usage of my FunctionPlotter component:

Functionplotter plotter = new FunctionPlotter();
plotter.plot("x^2")

=>的FunctionPlotter应自动等到包裹浏览器已被初始化。

=> The FunctionPlotter should automatically wait until the wrapped browser has been initialized.

我应该如何做到这一点在JavaFX应用程序?

How should I do this in an JavaFx Application?

在FunctionPlotter我可以做类似

Inside the FunctionPlotter I could do something like

private Boolean isInitialized = false
...

ReadOnlyObjectProperty<State> state =  webEngine.getLoadWorker().stateProperty();
state.addListener((obs, oldState, newState) -> {
    boolean isSucceeded = (newState == Worker.State.SUCCEEDED);
    if (isSucceeded) {
        isInitialized = true;
    }
});
webEngine.loadContent(initialBrowserContent);

waitUntilInitialLoadingIsFinished();

我实际的问题是如何在最后一行的方法可以实现。如果我使用以下code,应用程序将永远等下去:

My actual question is how the method on the last line could be implemented. If I use following code, the application will wait for ever:

private void waitUntilBrowserIsInitialized() {
    while(!isInitialized){
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        }
    }
}

我知道,有一个像JavaFx的任务的东西,Platform.runLater(),服务,CountdownLatch(的 JavaFx的一个线程和GUI 工作),但这些并没有帮助我(=我没有得到它的工作)。我怎么能在主线程等待,直到一个Runnable完成?

I know that there is stuff like JavaFx Tasks, Platform.runLater(), Service, CountdownLatch (JavaFx Working with threads and GUI) but those did not help me (= I did not get it working). How can I wait in the main Thread until a Runnable is finished?

下面有人说,JavaFX应用程序的线程不应该被阻止:

Here someone says that the JavaFx Application thread should never be blocked:

<一个href=\"http://stackoverflow.com/questions/29800410/make-javafx-application-thread-wait-for-another-thread-to-finish\">Make JavaFX应用程序线程等待另一个线程来完成

任何其他建议?

修改

相关问题:<一href=\"http://stackoverflow.com/questions/19247872/javafx-swt-webview-synchronous-loadcontent\">JavaFX/SWT的WebView同步loadcontent()

推荐答案

我决定在情节指令的内部队列包的情节功能。命令

I decided to wrap the plot functionality in an internal queue of plot instructions. The command

plotter.plot("x^2");

不会实际执行的情节,但一个绘图指令添加到队列。浏览器已经被初始化之后,该队列将通过工作,将与延迟来执行绘图命令。而浏览器被初始化,我会表现出一定的进度条。

will not actually execute the plot but add a plot instruction to the queue. After the browser has been initialized, that queue will be worked through and the plot commands will be executed with a delay. While the browser is initializing I will show some kind of progress bar.

如果您知道不需要解决此延迟执行的工作,请让我知道一个解决方案。

If you know a solution that does not need this delayed execution work around please let me know.

这篇关于如何等待在JavaFX应用程序WebEngine /浏览器初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆