Javadoc of the done() method of SwingWorker:
Executed on the Event Dispatch Thread after the doInBackground method is
Until the SwingWorker is fixed http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6826514 Here a simple (tested) version with the basic (similar) functions then SwingWorker
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tools;
import java.util.LinkedList;
import java.util.List;
import javax.swing.SwingUtilities;
/**
*
* @author patrick
*/
public abstract class MySwingWorker {
protected abstract R doInBackground() throws Exception;
protected abstract void done(R rvalue, Exception ex, boolean canceled);
protected void process(List chunks){}
protected void progress(int progress){}
private boolean cancelled=false;
private boolean done=false;
private boolean started=false;
final private Object syncprogress=new Object();
boolean progressstate=false;
private int progress=0;
final private Object syncprocess=new Object();
boolean processstate=false;
private LinkedList
chunkes= new LinkedList<>();
private Thread t= new Thread(new Runnable() {
@Override
public void run() {
Exception exception=null;
R rvalue=null;
try {
rvalue=doInBackground();
} catch (Exception ex) {
exception=ex;
}
//Done:
synchronized(MySwingWorker.this)
{
done=true;
final Exception cexception=exception;
final R crvalue=rvalue;
final boolean ccancelled=cancelled;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
done(crvalue, cexception, ccancelled);
}
});
}
}
});
protected final void publish(P p)
{
if(!Thread.currentThread().equals(t))
throw new UnsupportedOperationException("Must be called from worker Thread!");
synchronized(syncprocess)
{
chunkes.add(p);
if(!processstate)
{
processstate=true;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
List
list;
synchronized(syncprocess)
{
MySwingWorker.this.processstate=false;
list=MySwingWorker.this.chunkes;
MySwingWorker.this.chunkes= new LinkedList<>();
}
process(list);
}
});
}
}
}
protected final void setProgress(int progress)
{
if(!Thread.currentThread().equals(t))
throw new UnsupportedOperationException("Must be called from worker Thread!");
synchronized(syncprogress)
{
this.progress=progress;
if(!progressstate)
{
progressstate=true;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
int value;
//Acess Value
synchronized(syncprogress)
{
MySwingWorker.this.progressstate=false;
value=MySwingWorker.this.progress;
}
progress(value);
}
});
}
}
}
public final synchronized void execute()
{
if(!started)
{
started=true;
t.start();
}
}
public final synchronized boolean isRunning()
{
return started && !done;
}
public final synchronized boolean isDone()
{
return done;
}
public final synchronized boolean isCancelled()
{
return cancelled;
}
public final synchronized void cancel()
{
if(started && !cancelled && !done)
{
cancelled=true;
if(!Thread.currentThread().equals(t))
t.interrupt();
}
}
}