Java Swingworker:不是封装类
我在传递信息、更新进度以及使用不是封装类的 SwingWorker 类指示“完成”时遇到问题。
我有一个简单的类来处理硬盘驱动器上的文件和目录。用户单击“开始”按钮即可启动 SwingWorker 的实例。
我想打印在 SwingWorker 的事件驱动线程中的 JTextArea 上处理的文件的名称作为更新进度条。网上所有的例子都是嵌套类,嵌套类访问外部类中的变量(比如done
方法)。我还想向事件驱动线程发出信号,表明 SwingWorker 已完成,以便 EDT 可以执行诸如启用“开始”按钮(和清除字段)之类的操作。
这是我的问题: 1. SwingWorker 类如何将文本放入事件驱动线程的 JTextArea 并更新进度条?
- 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?
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SwingWorker
仍然是一个类,您可以扩展并传递完成其工作所需的任何信息,无论它是否封装在另一个类中。因此,您可以在构造函数中将 JTextArea 作为“目标文本区域”传递,将其存储为成员变量,然后在 process(ListEDT 无法确定工作线程何时完成:工作线程本身知道它已完成其工作,因为
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 theJTextArea
as a "target text area" in the constructor, store it as a member variable, and then update the text area in theprocess(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 ofdoInBackground()
and then invokes thedone()
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.