【问题标题】:Updating javafx UI using task without using Platform.RunLater在不使用 Platform.RunLater 的情况下使用任务更新 javafx UI
【发布时间】:2017-06-26 18:32:02
【问题描述】:

我想更新我的 javafx UI 但我不想使用 Platform.RunLater 但是当我想更新我的窗格时:

Pane.getChildren().add(listview);

我得到 java.lang.illegalStateException : not on java fx application thread;current thread

【问题讨论】:

  • 所有 UI 活动都应该在一个特殊的线程上运行,runLater 是实现这一点的最简单方法。为什么不想使用Platform.RunLater
  • 因为我已经读过它不推荐用于大而长的任务
  • @navidmahdian 将 listView 添加到窗格中并不是一项艰巨的任务。
  • @navidmahdian 你只应该把 UI 更新放在Platform.runLater(...) 中。长时间运行的代码只进入call 方法,而不是Platform.runLater(..)。或者,可能更好的是,在Task 上使用onSucceeded(...) 处理程序。除非您发布一些示例代码来显示您正在尝试做什么,否则真的不可能回答您的问题。
  • 任务本身使用Platform.runLater 进行更新。如果您害怕使用 Platform.runLater 发布太多可运行文件,只需使用可以一次执行多个更新并同步的可运行文件(例如:stackoverflow.com/a/40678129/2991525

标签: java multithreading javafx pane


【解决方案1】:

您必须使用 Platform.runLater() 更新 UI 线程上的 UI 元素。

如果您有一个长时间运行的任务,请先执行该任务,然后使用该任务的结果更新 UI。

Object taskResult = GetLongRunningTaskResult();
Platform.runLater(() -> Pane.getChildren().add(taskResult));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-16
    • 2018-05-24
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    相关资源
    最近更新 更多