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.
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.
Joined: Jun 24, 2011
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.
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.