Java GUI Themes - SwingWorker

I have a question about SwingWorker and the Java GUI.

I have several classes that 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 not called directly (the classes Foo[x] use methods inherited from Foo ). To prevent the EDT from drawing a progress bar, what's the best way to use SwingWorker while maintaining a hierarchy of objects? Is it possible to have wrapper classes like Foo1Worker extends SwingWorker and have doInBackground() Foo1.myProcessMethod() ? Even if Foo1 does not extend SwingWorker , will it still work, as I expect?

edit: to clarify my question, how can I make Foo[x] SwingWorkers even if they are already subclasses?

+1
source share
2 answers

I think the answer is critically dependent on the type of data managed by the Foo subclasses. If the results are uniform, just SwingWorker and create specific 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 one controls a different type, create a parent family tree and create each specific 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 { ... } } 
+2
source

You will need SwingWorker if you need to update GUI elements, for example. calling routines such as SetText() . I never thought of using them for update tasks without a GUI; I always subclassed Thread or implemented Runnable . I recommend that you try this with your Foo classes and see if the problem is managing itself.

0
source

Source: https://habr.com/ru/post/1398222/


All Articles