Sunday, January 15, 2006

Generified SwingWorker class

The folks at Sun created a nice little helper class called SwingWorker.
It was designed to help with the pain with Swing and working in threads.
You can find the details here.

There was one problem though, even with Version 3 of the SwingWorker.java: it did not use generics, and therefore promped unchecked warnings everytime I used it, mainly in the get() method, used to synchronize the eventdispatcher thread with the worker thread and retrieve the result of the hard worked that has been done by then. It simply returns an java.lang.Object, which of course was OK (and the only way) before J2SE 1.5

I therefore took the liberty to modify the SwingWorker to now take a type parameter and returns exactly this type.
So it now reads:
  public abstract class SwingWorker<T>

and the get() method now is a
  public T get()

And some other more internal changes...

You can find it here.

No comments: