rhi's blog

Taming Swing Threads Part 3: The Swing Worker

The problem.

We problem we encountered at the end of part 2 is that although it is really easy to offload long run tasks to a background thread, it is rather difficult displaying the results in a thread safe manner.


Java™ 5 has recently provided us with a class called the SwingWorker, which solves this problem and at the same time shields us from the complexity of passing information between threads. The SwingWorker is the perfect solution to our problem. The SwingWorker offers a great deal of functionality; however for this blog entry I will only be showing you how to use it to solve the problem discussed above. If you want further information on the SwingWorker, you can check out its API on the sun site: http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html.


Taming Swing Threads Part 2: Common Event Queue Pitfalls

Key points from part 1.

Hopefully after reading part 1 you should be feeling pretty comfortable about what the EDT is and how it processes, but just as a reminder, here are the key points from part 1.

  • Only the EDT may safely manipulate swing components (it is the uber master swing thread).
  • If any other threads are manipulating swing components, they are doing so in an unsafe manner (and bad things will happen).
  • The EDT processes events using a simple sequential queue called the Event Queue.


The idea behind part 2 is to explain some of the tricky situations and pitfalls that swing developers often get caught in, as well as hopefully answering some of the questions you have regarding the Event Queue.

Taming Swing Threads Part 1: The Event Dispatch Thread

Why do I need to know what the Event Dispatch Thread (EDT).

As far as a topic goes for my first blog, discussing the Event Dispatch Thread is probably not the most exciting topic for some, but it is definitely one of the most important as far as swing is concerned.


If you have ever wondered (or shouted while pulling your hair out in frustration)

  • Why does my application go gray or become unresponsive while I’m processing?
  • Why is my swing application slow?
  • Or why do I seem to get random paint errors from swing?

The answer is the EDT and the painful truth is that you are probably not using it optimally.


However don’t get intimidated by the name, the Event Dispatch Thread (EDT) sounds a bit complex, but understanding it is in fact really easy. Once you have this piece of knowledge tucked safely under your belt, you will truly be able to make swing fly, and built the killer applications you have always wanted.

The LATEST chatter

  • Halted all development on the twitter client. The application in its incomplete state is available at: http://tinyurl.com/6ktd8xw 8 years 22 weeks ago
  • Java Swing Components is currently undergoing a rebranding exercise to Custom Swing Components. The url however will remain the same. 8 years 44 weeks ago
  • Java Swing Components is proud to announce the release of our rater component. http://www.javaswingcomponents.com/product/rater 8 years 46 weeks ago
  • Java Swing Components is proud to announce the release of our first bundle including a fun demo. http://www.javaswingcomponents.com/products 8 years 50 weeks ago
  • New post: New Component Teaser http://tinyurl.com/35hxfnn 8 years 50 weeks ago

User login


Syndicate content