javafx,从另一个线程更新 ui

javafx, update ui from another thread(javafx,从另一个线程更新 ui)

本文介绍了javafx,从另一个线程更新 ui的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 javafx 应用程序和一个工作线程,通过 javafx.concurrent.Task 实现,它执行一个漫长的过程,即压缩和上传一组文件.
我已通过 progressProperty 将任务进度连接到进度条.
除此之外,我还希望将正在处理的项目的详细状态报告到 ui 中.也就是说,正在处理的文件的名称及其大小以及单个文件进程可能出现的任何错误.
无法从工作线程中使用这些信息更新 UI,最多我可以将其添加到同步集合中.
但随后我需要一些事件来通知 UI 新数据可用.
javafx 对这个问题有特定的支持吗?

I have a javafx application, and a worker thread, implemented via javafx.concurrent.Task, that performs a long process, that is zipping and uploading a set of files.
I've connected the task progress to a progress bar via progressProperty.
In addition to this i want a detailed state about the item being processed to be reported into the ui. That is, the name of the file being processed along with its size and any error that may arise from the single file process.
Updating the UI with these information cannot be done from the worker thread, at the most i can add it to a synchronized collection.
But then i need some event to inform the UI that new data is available.
Does javafx have some specific support for this issue?

我没有像 Platform.runLater 那样设计一个特别的跨线程机制,而是试图让每个属性都可以从其他线程中被监听.就像Task提供的runningProperty和stateProperty一样

Instead of designing an ad hoc cross-thread mechanism as Platform.runLater, i'm trying to allow each property to be listened from other threads. Just like runningProperty and stateProperty provided by Task

推荐答案

我遇到了类似的问题,据我所知,您必须自己处理错误处理.我的解决方案是通过方法调用更新 UI:

I'm running into a similar issue, as far as I can tell you have to deal with the error handling yourself. My solution is to update the UI via a method call:

类似于:

  try
  {
    //blah...
  }
  catch (Exception e)
  {
    reportAndLogException(e);
  }
  ...
  public void reportAndLogException(final Throwable t)
  {
    Platform.runLater(new Runnable() {
      @Override public void run() {
        //Update UI here     
      }
    });
  }

基本上我只是手动将其移回 UI 线程进行更新(就像我在几乎任何其他框架中所做的那样).

Essentially I am just manually moving it back to the UI Thread for an update (as I would do in pretty much any other framework).

这篇关于javafx,从另一个线程更新 ui的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:javafx,从另一个线程更新 ui

基础教程推荐