SwingWorker makes it easy to set up a background Thread that interacts with the Swing GUI (Graphic User Interface). If the background task does not use the GUI components, it is simpler to use an ordinary Thread instead. SwingWorker formally became part of Swing with Java version 1.6, but prior to that, you could find the source and incorporate it.
The idea of SwingWorker is to make it easier to follow the two primary rules of Swing Threads.
There are two fundamental things to understand about Swing and threads:
The magic under the hood is basically that SwingWorker calls your SwingWorker. process method periodically on the EDT thread, so SwingWorker. process does not have to use SwingUtilities. invokeAndWait or SwingUtilities. invokeLater. Your SwingWorker. doInBackground method runs on a non-EDT thread. It communicates with SwingWorker. process which runs on the EDT thread by calling SwingWorker. publish with packets of data for SwingWorker. process.
You might imagine coding with SwingWorker uses fewer threads than SwingUtilities. invokeLater. However invokeLater uses the Runnable interface, without ever generating any extra threads, just the generation of event, enqueing it, and eventually calling the run method. So unless you are using most of the bells and whistles of SwingWorker, you will probably find it simpler just to spin off a garden variety background Thread and use invokeLater.
SwingWorker runs all your code on the EDT thread. This means you have to be very quick. If you dawdle in the least, your entire GUI will freeze. If you need to do time-consuming tasks, they need to run on some other non-SwingWorker threads.
Like Threads, SwingWorkers objects are not reusable. You must create a new one before you call execute to kick it off.
available on the web at:
optional Replicator mirror
Please email your feedback for publication, letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : . If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, especially when sending an ad-hominem attack, a rant composed mainly of obscenities or a death threat, please quote the offending passage and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. I can’t very well fix erroneous or ambiguous text if I can’t find it.
Your face IP:[22.214.171.124]
|Feedback||You are visitor number 11.|