Java Swingworker:不是封装类

发布于 2024-08-30 02:11:18 字数 522 浏览 4 评论 0原文

我在传递信息、更新进度以及使用不是封装类的 SwingWorker 类指示“完成”时遇到问题。

我有一个简单的类来处理硬盘驱动器上的文件和目录。用户单击“开始”按钮即可启动 SwingWorker 的实例。

我想打印在 SwingWorker 的事件驱动线程中的 JTextArea 上处理的文件的名称作为更新进度条。网上所有的例子都是嵌套类,嵌套类访问外部类中的变量(比如done方法)。我还想向事件驱动线程发出信号,表明 SwingWorker 已完成,以便 EDT 可以执行诸如启用“开始”按钮(和清除字段)之类的操作。

这是我的问题: 1. SwingWorker 类如何将文本放入事件驱动线程的 JTextArea 并更新进度条?

  1. EDT 如何确定外部 SwingWorker 线程何时完成?

{我不希望 SwingWorker 作为嵌套类,因为需要完成大量代码(和处理)。}

I'm having problems passing information, updating progress and indicating "done" with a SwingWorker class that is not an encapsulated class.

I have a simple class that processes files and directories on a hard drive. The user clicks on the Start button and that launches an instance of the SwingWorker.

I would like to print the names of the files that are processed on the JTextArea in the Event Driven Thread from the SwingWorker as update a progress bar. All the examples on the web are for an nested class, and the nested class accesses variables in the outer class (such as the done method). I would also like to signal the Event Driven Thread that the SwingWorker is finished so the EDT can perform actions such as enabling the Start button (and clearing fields).

Here are my questions:
1. How does the SwingWorker class put text into the JTextArea of the Event Driven Thread and update a progress bar?

  1. How does the EDT determine when the {external} SwingWorker thread is finished?

{I don't want the SwingWorker as a nested class because there is a lot of code (and processing) done.}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开半夏魅人心 2024-09-06 02:11:18

SwingWorker 仍然是一个类,您可以扩展并传递完成其工作所需的任何信息,无论它是否封装在另一个类中。因此,您可以在构造函数中将 JTextArea 作为“目标文本区域”传递,将其存储为成员变量,然后在 process(List) 中更新文本区域方法。

EDT 无法确定工作线程何时完成:工作线程本身知道它已完成其工作,因为 doInBackground() 方法已完成。包装工作线程的代码检测 doInBackground() 的完成情况,然后调用 EDT 上的 done() 方法。 (Swing Worker 能够自动为您处理线程中的所有更改,这是它如此出色的原因之一。)

在您的 done() 实现中,您可以通过以下方式通知 GUI 完成情况:通过观察者对象回调 - 再次,您可以将其传递给工作人员的构造函数。

有关如何操作的说明,请参阅SwingWorker 教程实施不同的方法。 进度条 上的线索包括有关使用Swing Worker 中的进度条。

A SwingWorker is still a class you can extend and pass in any information it requires to do its job, whether or not it's encapsulated in another class. So you could pass in the JTextArea as a "target text area" in the constructor, store it as a member variable, and then update the text area in the process(List<V>) method.

The EDT doesn't determine when the worker is finished: The worker itself knows it has completed its job because the doInBackground() method finishes. The code that wraps up the worker detects the completion of doInBackground() and then invokes the done() method on the EDT. (The ability of the swing worker to handle all the changes in threads for you automatically is one of the things that makes it so good.)

In your done() implementation you could inform your GUI of completion by calling back via an observer object - again, something you could pass in to the worker's constructor.

See this tutorial on SwingWorker for a description of how to impement the different methods. The trail on progress bars includes a section on using a progress bar in a swing worker.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文