views:

103

answers:

2

Any suggestions on how I can cleanup the following code pattern that repeats multiple times in my app.

new Thread(new Runnable() {
  public void run() {
    // Do some work here
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        // Update the Swing Interface to reflect the change
      }

    });
  }
}).start();

Basically there are two code blocks the section that does the work on another thread, and the code block that executes in the Swing UI Thread.

I'm pretty sure I can create a class to sub in these blocks, but I'm hoping there something in the Swing Library that makes this easier.

Thanks.

+1  A: 

look at the SwingWorker framework

akf
Swing worker uses a background thread. Allain wants to put the action on the Event Dispatch Thread.
jjnguy
Actually, the only portion I need to run in the Event Dispatch Thread is the portion *after* the main block has completed, and by overriding SwingWorker.done() in the SwingWorker class, I get that behaviour.
Allain Lalonde
Well, there ya go!!
jjnguy
+1  A: 

The Concurrency in Swing tutorial is another good place to look. There's discussion about SwingWorker there, too.

Brian T. Grant