Javafx webengine使loadContent像同步一样

时间:2018-02-05 03:56:33

标签: javascript java multithreading javafx

我想让loadContent在我执行方法中的下一个语句之前完成加载网页。 IE我希望它表现得像是一个同步方法。这是因为否则我得到Javascript例外,例如通过执行Exception in thread "JavaFX Application Thread" netscape.javascript.JSException: ReferenceError: Can't find variable: ace

之类的内容webEngine.executeScript(ace ....)

从在线帖子中,我尝试了以下内容(但它们不起作用......

(Try1)

...some code...
this.webEngine.getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
        if (newState != Worker.State.SUCCEEDED) {
          return;
        }
    });
    this.webEngine.loadContent(...);
 ...more code...

(Try2)

...some code...
this.webEngine.getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
        if (newState == Worker.State.SUCCEEDED) {
          new CountDownLatch(1).countDown();
        }
    });
    this.webEngine.loadContent(...);
 ...more code...

(Try3)

...some code...
CountDownLatch latch=new CountDownLatch(1);
this.webEngine.getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
        if (newState != Worker.State.SUCCEEDED) {
          latch.countDown();
        }
    });
    this.webEngine.loadContent(...);
 ...more code...

请帮我解决这个问题......提前谢谢!

0 个答案:

没有答案