Java GUI 线程 - SwingWorker
我有一个关于 SwingWorker 和 Java GUI 的问题。
我有几个处理信息的类,我们可以将它们称为 Foo1
、Foo2
和 Foo3
。此处理可能需要很长时间。
这些都是 Foo
的子类,但是 Foo
本身并不直接调用(Foo[x]
类使用从 Foo 继承的方法)为了让 EDT 能够自由地绘制进度条,在保留对象层次结构时使用
并让其 SwingWorker
的最佳方法是什么? >Foo1Worker 扩展了 SwingWorkerdoInBackground()
调用 Foo1.myProcessMethod()
?即使 Foo1
没有扩展 >SwingWorker
,这仍然会像我期望的那样工作吗?
编辑:为了澄清我的问题,即使它们已经是子类,我怎样才能制作 Foo[x]
SwingWorkers ?
I have a question regarding SwingWorker and Java GUI's.
I have several classes which process information, we can call them Foo1
, Foo2
, and Foo3
. This processing can take a very long time.
These are all subclasses of Foo
, however Foo
is not called directly itself (the Foo[x]
classes use methods inherited from Foo
. In order to keep the EDT free to paint a progress bar, what is the best way to use SwingWorker
when keeping my object hierarchy? Is it possible to have wrapper classes such as Foo1Worker extends SwingWorker
and have its doInBackground()
call Foo1.myProcessMethod()
? Even though Foo1
does not extend SwingWorker
, will this still work as I expect it to?
edit: to clarify my question, how can I make Foo[x]
SwingWorkers even though they are already subclasses?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为答案关键取决于 Foo 子类管理的数据类型。如果结果是同质的,只需扩展
SwingWorker
并相应地实例化具体子类:如果每个子类管理不同的类型,则使父类通用并使用所需类型实例化每个具体子类:
I think the answer hinges critically on the type of data managed by
Foo
subclasses. If the results are homogeneous, just extendSwingWorker
and instantiate concrete subclasses accordingly:If each manages a different type, make the parent generic and instantiate each concrete subclass with the required type:
如果您需要更新 GUI 元素,例如调用
SetText()
等例程,则需要 SwingWorker。我从来没有想过将它们用于非 GUI 更新任务;我总是对Thread
进行子类化或实现Runnable
。我建议您在 Foo 类中尝试此操作,看看问题是否会自行解决。You'd need a SwingWorker if you needed to update GUI elements, e.g. calling routines such as
SetText()
. I've never thought about using them for non-GUI update tasks; I've always sub-classedThread
or implementedRunnable
. I recommend that you try this with your Foo classes and see if the problem takes care of itself.