嗨我'm on a particular problem. Let me explain: I' m使用JavaFX并控制其他类的一些节点(让我们称之为: class A 和javaFX控制器类: class B ) . 在节点组内部'm using a listView, so from class B I'm添加到listModel(listView的listModel) . The problem consist when I add to that listModel ,JavaFX应用程序 gets freeze . Java抛出这样的预期:

线程“pool-2-thread-1”中的异常java.lang.IllegalStateException:不在FX应用程序线程上; currentThread = pool-2-thread-1

我在 Task 上运行该代码(我向listModel添加元素的代码)但在此之前我在ExecutorService上执行"Task",如下所示:

ExecutorService executorService = Executors.newFixedThreadPool(1); // pool of one thread
executorService.execute(task); // start the task
executorService.shutdown();

class B (JavaFX控制器)中的方法调用executorService . 在listModel添加了元素之后,应用程序解冻了几秒钟 .

我当然用过:

Platform.runLater(() -> action())

执行JavaFX更新 . 当我在Platform.runLater()内的listModel中添加元素时,应用程序不会被冻结,但listModel不会添加任何内容,因为listModel.add(element)接收的变量在某种程度上不存在JavaFX线程,至少我想是这样 .

这是一个示例:

for (RemoteXBeeDevice remote : network.getDevices()) {

    if (remote != null) {
        if (!remote.getNodeID().endsWith("SENSOR")) {
            Platform.runLater(() -> listModel.add(remote)));
        }
    }
}

提前致谢 .

编辑:经过一些更改后,listModel已经添加了元素,但应用程序仍然被冻结 .