permaculture playing cards
The moose likes Swing / AWT / SWT and the fly likes When would I use a SwingWorker and why are all the videos in Chinese? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "When would I use a SwingWorker and why are all the videos in Chinese?" Watch "When would I use a SwingWorker and why are all the videos in Chinese?" New topic

When would I use a SwingWorker and why are all the videos in Chinese?

Adam Outler

Joined: Jun 24, 2011
Posts: 18
Hello, I've read the entire lesson on concurrency. I understand most of this.. I read SwingWorker, I didn't really understand it. So, I went looking for videos. They're all in Asian languages.

I have questions about things....

1. When would I use a swing worker instead of a Thread, or a Timer object?
2. How does one implement a swing worker?
3. What is the advantage of a swing worker? It seems alot more complicated then just starting a new thread.
4. Can someone break this example down? It's designed to retrieve images from a website.

what is with this? Why the <..............>?

Why would you want to use a "Publish" method? It seems that just a threaded object would work great in all circumstances... Please help me to understand.
Arun Chidam
Ranch Hand

Joined: May 08, 2006
Posts: 79
Check the SwingWorker link, it will clear most of your doubts....unfortunately it is a asian link in english

SCJP 1.4, SCWCD 1.4
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4112

Hi Adam,

The advantage of a SwingWorker above using your own thread, is that a SwingWorker already handles all the necessary synchronization behind the scenes. You don't need to toy around with the wait() and notifyAll() methods in order to make it work, which greatly simplifies your code.

The difference between a SwingWorker and a timer is mostly a conceptual one. SwingWorkers are meant for long background tasks that might otherwise hold up the event dispatch thread. Timers are mostly used to trigger short recurring events.

SwingWorkers roughly work like this:
1) The event dispatch thread will react to some event, like a button being pushed, and will call the appropriate listener method.
2) The listener will start your background task by calling the execute() method on your SwingWorker. This method returns immediately, so the event dispatch thread is free to keep your user interface responsive.
3) The execute() methods causes your doInBackground() method to be executed on a different thread.
4) When the doInBackground() method is done, it will in turn cause the done() method to be executed on the event dispatch thread, so your user interface can get updated.

The point of the publish() and process() methods is to let the user interface update while the entire background task isn't done yet, but has intermediate results that are useful to the user interface.

Let's say your doInBackground() method is responsible for loading a set of images. When it's done, the user interface should show these images in a list. When we don't use the publish() and process() methods, the list will remain empty until all images are loaded, and then suddenly all images will appear once they are all loaded. Using the two aforementioned methods, we can add individual images to the list as they are done loading, before the doInBackground() method is finished.

After the doInBackground() method finishes loading an image, it can simply call the publish() method, and the event dispatch thread gets a chance to react in the process() method.

The <..............> part signifies generic type parameters. The first parameter will be the return type of the get() and doInBackground() methods; in your case an array of ImageIcons. The second parameter will be the parameter types of the publish() and process() methods. In your case this is Void, meaning these methods don't take arguments.

For more information, see the Oracle tutorials on generics.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Adam Outler

Joined: Jun 24, 2011
Posts: 18
Thank you. I still don't understand its implementations fully. I am trying to launch a shell command which hogs up resources. I'd like it to update a jTextArea in my main View, HeimdallOneClickView . I was told to look at SwingWorker by someone on Stack Overflow. I'm not sure if that's the best option because of how complex the ShellWorker is and how frequently I need update.

From what I understand I may have been given wrong advice and searched for about a month with a misunderstanding. I now understand ShellWorker to be a "fire and forget" threaded operation, which allows for intervention and modification during its process. At the end of its cycle it will perform an operation. Am I correct in this?

From what you have told me, I believe the proper way is to create a process which implements runnable and have that process update my jTextBox.

It sure would be great if someone would do a ShellWorker video in English.,or.r_gc.r_pw.&fp=6bda322bc0201a5e&biw=1600&bih=801
Rob Spoor

Joined: Oct 27, 2005
Posts: 20137

SwingWorker can be used just perfectly. You call publish for any String you want to have in the JTextArea. You override process to take all elements of the List and add those to the JTextArea:
If you don't need any return value from the get method, just replace X with Object or Void, and return null at the end of doInBackground.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: When would I use a SwingWorker and why are all the videos in Chinese?
It's not a secret anymore!