Java 5 SwingWorker replacement

Our Swing application performs some lengthy tasks in the background thread using the excellent SwingWorker class. However, many older Macs only support Java 5, so we want to compile our application as 5, not 6. Since SwingWorker was introduced in Java 6, we can no longer use it.

Will the next replacement be acceptable if I only need to do something in the background and then transfer it to the GUI when this is done? Or am I forgetting something important?

public static void wannabeSwingWorker(final Runnable doInBackground, final Runnable callback) {
    Thread backgroundThread = new Thread(new Runnable() {
        public void run() {
            doInBackground.run();
            SwingUtilities.invokeLater(callback);
        }
    });
    backgroundThread.start();
}
+5
source share
4 answers

- , backport Swingworker Java 5 .

+10

; , SwingWorker ( , , ),

+3

Foxtrot, -, Swing concurrency libs.

, , , Swing, , , , Swing EDT GUI .

When working with a long-term task that returns a value that you need to immediately use, this leads to a very pleasant, procedural / non-competitive search for the program code (just like checking the return value from JOptionPane.showConfirmDialog(...)).

+2
source

If you have no problem with the LGPL code in your product, you can use the backport version of SwingWorker for Java 5.

http://java.net/projects/swingworker

+1
source

All Articles