Java GUI 线程 - SwingWorker

发布于 2024-09-08 01:17:12 字数 566 浏览 5 评论 0原文

我有一个关于 SwingWorker 和 Java GUI 的问题。

我有几个处理信息的类,我们可以将它们称为 Foo1Foo2Foo3。此处理可能需要很长时间。

这些都是 Foo 的子类,但是 Foo 本身并不直接调用(Foo[x] 类使用从 Foo 继承的方法)为了让 EDT 能够自由地绘制进度条,在保留对象层次结构时使用 SwingWorker 的最佳方法是什么? >Foo1Worker 扩展了 SwingWorker 并让其 doInBackground() 调用 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 技术交流群。

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

发布评论

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

评论(2

痴者 2024-09-15 01:17:12

我认为答案关键取决于 Foo 子类管理的数据类型。如果结果是同质的,只需扩展 SwingWorker 并相应地实例化具体子类:

class Whatever {}

abstract class AbstractFoo extends SwingWorker<List<Whatever>, Whatever> {}

class Foo1 extends AbstractFoo {

    @Override
    protected List<Whatever> doInBackground() throws Exception {
        ...
    }
}

如果每个子类管理不同的类型,则使父类通用并使用所需类型实例化每个具体子类:

class Whatever {}
class Whichever {}

abstract class GenericAbstractFoo<T, V> extends SwingWorker<T, V> {}

class Foo2 extends GenericAbstractFoo<List<Whatever>, Whatever> {

    @Override
    protected List<Whatever> doInBackground() throws Exception {
        ...
    }
}

class Foo3 extends GenericAbstractFoo<List<Whichever>, Whichever> {

    @Override
    protected List<Whichever> doInBackground() throws Exception {
        ...
    }
}

I think the answer hinges critically on the type of data managed by Foo subclasses. If the results are homogeneous, just extend SwingWorker and instantiate concrete subclasses accordingly:

class Whatever {}

abstract class AbstractFoo extends SwingWorker<List<Whatever>, Whatever> {}

class Foo1 extends AbstractFoo {

    @Override
    protected List<Whatever> doInBackground() throws Exception {
        ...
    }
}

If each manages a different type, make the parent generic and instantiate each concrete subclass with the required type:

class Whatever {}
class Whichever {}

abstract class GenericAbstractFoo<T, V> extends SwingWorker<T, V> {}

class Foo2 extends GenericAbstractFoo<List<Whatever>, Whatever> {

    @Override
    protected List<Whatever> doInBackground() throws Exception {
        ...
    }
}

class Foo3 extends GenericAbstractFoo<List<Whichever>, Whichever> {

    @Override
    protected List<Whichever> doInBackground() throws Exception {
        ...
    }
}
生生不灭 2024-09-15 01:17:12

如果您需要更新 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-classed Thread or implemented Runnable. I recommend that you try this with your Foo classes and see if the problem takes care of itself.

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