javafx, update ui from another thread(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
- C++ 和 Java 进程之间的共享内存 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01