【问题标题】:JavaFX webengine.executeScript() from another thread.JavaFX webengine.executeScript() 来自另一个线程。
【发布时间】:2014-02-01 22:47:17
【问题描述】:

我有一个应用程序,它使用WebView 及其支持WebEngine 来显示一个包含一些JavaScript 函数的简单网页。我还有另一个线程有时会从互联网接收消息。我希望这些触发网页上的脚本,使用webengine.executeScript()

我的问题是 JavaFX 线程模型不允许它在主 UI 线程之外的任何其他线程上运行,并引发异常。我查看了属性、任务、服务,但似乎没有一个能满足我的需要。脚本执行速度很快——让它在 UI 线程上运行不是问题,但我不确定如何将消息发送到 UI 线程。

【问题讨论】:

    标签: java multithreading javafx


    【解决方案1】:

    你可以使用Platform.runLater():

    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            webEngine.executeScript("foo()")
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多