|
|
SwingWorker is one of the most handiest utility classes provided in Swing. Most of the interviews on Swing have a question or two on this class. Let’s make sure you know all about it’s need and usage. Step by Step. A (Quick) look at the problem with the “Event Dispatch” thread:
The “Event Dispatch” Thread(EDT) is a special thread in Swing where all graphical events are processed. One such event is painting the screen. Also, all the listeners attached to swing components are executed in the EDT(BTW, make it an habit to call it the “EDT”, it looks cool on interviews). Ok, how do I un-hang my Swing Application
Simple. Don’t let the code in your listener execute for a long time. But what if you have to write code that takes long time to execute? For eg., what if you need to fetch large data from a db table and display it to the user when the user presses a button? Again, Simple. Execute the time consuming task in a separate thread. The paint method executing in the EDT can execute parallely when your new thread is happily waiting for the data to get fetched from the DB(yes, the laws of nature allow this). Simple, right? So here are the steps,
From the background thread you can use methods like SwingUtilties.invokeLater() to run code in the EDT.. But…. Wouldn’t it be dreamy if …(yes, this title was picked up those head first books)
Wouldn’t it be dreamy you could write just two methods, one method (m1) where you put the code to load data and the other method (m2) where you write the code to update the UI? Wouldn’t it be great, if automatically m1(loading the data) is called first and then when that completes,m2 is automatically called? Wouldn’t it be magical, if somehow m1 could be automatically get called in a background thread and m2 in the EDT? So SwingWorker is this..A class having two main methods,
An ExampleSwingWorker worker = new SwingWorker<List, Void>() { //This method automatically gets executed in a background thread public List doInBackground() { //fetch the big list from the List list = getBigListAfterExecutingForLongTime(); return intDoc; } //This methods automatically gets executed in the EDT public void done() { //Get method retuns the exect, same thing //that the doInBackground() method returned List list = get(); //Now do all UI Operations here.. updateTheUIWithThisBigList(list); } }; //Code that executes when the button is pressed public void actionPerformed(ActionEvent ae){ worker.execute(); } O, No!, I don’t get to use Java 6 in my projectDon’t worry. There’s a backport of the exact SwingWorker present in Java 6 for Java 5. You can download it from here . “https://swingworker.dev.java.net/” 6 Responses to 'Using SwingWorker - A Step by Step tutorial'Leave a Reply |
Popular Articles
Blog Categories
Monthly Archives
Resources
|
Excellent text, nice prose.
Great intro to SwingWorker. SwingWorker is really cool stuff. I used it a lot in my last project and don’t want to miss it for any future Swing work.
Good article. Thanks.
You might want to look at Detangler. It shows you the exact lines in your code where you need to use SwingWorker.
http://denova.com/Detangler/
Detangler is the easiest way to find the hardest Swing bugs. And it’s free.
Very good post. Also, there is one related technique that I’ve described recently in my blog. It’s actually functionality that allows to perform scheduled execution of events - more details are there:
http://www.soft-amis.com/serendipity/index.php?/archives/12-Class-that-any-Swing-application-cant-live-without.html
[…] new article on JavaSwing.net has a short overview on Swing threading and SwingWorker […]
Excellent Article,
I was really having a hard time with SwingWorker, finding information in Internet that wasn´t explicit and valid. After reading your post, I quickly understood this class’s usage. THANKS!!!