在多任务应用程序中管理GUI和EDT

编程入门 行业动态 更新时间:2024-10-24 08:32:31
本文介绍了在多任务应用程序中管理GUI和EDT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我开发了一个Java应用程序,用于创建和提取存档-如 WinRAR .您可以使用多线程同时创建多个档案.最近,我想在归档创建期间以每次创建的新JFrame中的JProgressBar的形式添加信息状态.

I developed a Java application for creating and extracting an archive - like WinRAR. You can create several archives at the same time with multithreading. And recently, I wanted to add an information status during the archive creation in the form of JProgressBar in a new JFrame at every creation.

但是我的问题是在新的状态框架和创建档案的线程中生成信息.这就是为什么我在存档线程中创建JFrame以便当前更新进度条.

But my problem is generating information in the new status frame and the thread which create the archive. That's why I create the JFrame in the archive thread for updating the progress bar currently.

但是,就像我可以在各种各样的信息源中以及您的答案/评论中阅读它一样,它与Java 摇摆和性能;我无法在 EDT 的其他位置创建swing对象.

But like I could read it in a diverse information source and on your answers/comments, it's against Java Swing and performance; I can't create swing object elsewhere that the EDT.

但是,我该如何解决我的问题呢?如何建立归档文件及其状态JFrame(通过JProgressBar)之间的通信?

But then, how should I solve my problem? How can I etablish communication between the writing of my archive and its status JFrame (with JProgressBar)?

我实现了SwingWorker来管理我的应用程序中的GUI.现在完成了,我还有另一个问题:

I implemented SwingWorker to manage the GUI in my application. Now it's done, I have an other question:

使用SwingWorker,如何通过状态框架按钮上的事件来执行后台任务? (例如:暂停压缩或停止压缩.)

With SwingWorker, how do I act on the background task with an event on status Frame's button? (Example: pause compression or stop it.)

推荐答案

其他人建议,最好的方法是使用SwingWorker.

As suggested by others, the best way is to use SwingWorker.

SwingWorker属性是可侦听的,并且始终在EDT中调用侦听器,因此,您可以执行以下操作:

SwingWorker properties are listenable and listeners are always called in the EDT, thus, you could do something like:

public class ArchivingWorker extends SwingWorker<Void, Void> { JProgressBar progressBar = null; // Other members here... ... public ArchivingWorker(...) { // Any specific initialization here (in EDT) addPropertyChangeListener(new PropertyChangeListener() { @Override void propertyChange(PropertyChangeEvent e) { if ( "state".equals(e.getPropertyName()) && e.getNewValue() == StateValue.STARTED) { // Background thread has just started, show a progress dialog here progressBar = new JProgressBar(); ... } else if ("progress".equals(e.getPropertyName())) { // Update progress bar here with e.getNewValue() ... } } }); } @Override protected Void doInBackground() { // Archiving process here and update progress from time to time setProgress(progress); return null; } @Override protected void done() { // Ensure that archiving process worked correctly (no exception) try { get(); } catch (Exception e) { // Handle exception (user feedback or whatever) } finally { // Close progress dialog ... } } }

然后您可以根据需要使用ArchivingWorker:

Then you can use ArchivingWorker as you need it:

ArchivngWorker worker = new ArchivingWorker(...); worker.execute();

更多推荐

在多任务应用程序中管理GUI和EDT

本文发布于:2023-11-27 03:09:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636319.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   GUI   EDT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!