I cannot really get the advantage of using a swingworker instead of just simply putting the heavy task in a (simple) background-thread.
This is just a codesnippet but shows the task of counting an integer and putting the values in a JLabel. (this is within the ActionPerformed)
Thread t = new Thread() {
public void run() {
for (int i = 0; i < 2000000; i++) {
counter++;
label.setText(Integer.toString(counter));
}
}
};
t.start();
Instead of this simple codesnippet I have been told to use the abstract SwingWorker-class and override methods such as doInBackground, process and done.
It works very fine when the label is updating simply using Thread. Why should I create a new class that extends swingworker and implement the abstract methods? I can at the moment only think of the Swing-worker as threadsafe, is that the answer?