Ho fatto una cosa più o meno uguale all'esempio compresso nel file zip della pagina che mi hai linkato!
SwingWorker per la creazione e la gestione del Thread:
codice:
import javax.swing.SwingUtilities;
public abstract class SwingWorker
{
private Object valore;
private Thread thread;
//classe privata ThreadVar per mantenere il riferimento al worker thread corrente
private static class ThreadVar
{
private Thread thread;
ThreadVar(Thread t)
{
thread=t;
}
synchronized Thread get()
{
return thread;
}
synchronized void clear()
{
thread=null;
}
}
private ThreadVar threadVar;
//metodi classe SwingWorker
protected synchronized Object getValore()
{
return valore;
}
private void setValore(Object newValore)
{
valore=newValore;
}
// computa il valore ritornato applicando il metodo get()
public abstract Object construct();
public void finished(){
}
public void interrupt()
{
Thread t=threadVar.get();
if(t != null)
t.interrupt();
threadVar.clear();
}
//questo metodo restituisce il valore fornito dal metodo construct
public Object get()
{
while(true)
{
Thread t = threadVar.get();
if(t != null)
{
return getValore();
}
try
{
t.join();
}
catch(InterruptedException e)
{
Thread.currentThread().interrupt();
return null;
}
}
}
public SwingWorker()
{
final Runnable doFinished = new Runnable()
{
public void run()
{
finished();
}
};
Runnable doConstruct = new Runnable()
{
public void run()
{
try
{
setValore(construct());
}
finally
{
threadVar.clear();
}
SwingUtilities.invokeLater(doFinished);
}
};
Thread t=new Thread(doConstruct);
threadVar = new ThreadVar(t);
}
//metodo start
public void start()
{
Thread t=threadVar.get();
if(t != null)
t.start();
}
}
Classe che utilizza i metodi dello SwingWorker
codice:
/**
* @(#)Sender.java
*
*
* @author
* @version 1.00 2009/1/24
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
class Sender extends JPanel
{
//variabili
JProgressBar progressBar = new JProgressBar();
JLabel label = new JLabel("", JLabel.CENTER);
SwingWorker worker;
JButton startButton;
JButton annullaButton;
//per file
int fileSize;
JButton getStartButton()
{
return startButton;
}
void updateStato(final int i)
{
Runnable setProgressBarValore = new Runnable()
{
public void run()
{
progressBar.setValore(i); //non lo trova
}
};
SwingUtilities.invokeLater(setProgressBarValore);
}
int getFileSize(FileInputStream lettura)
{
int nbyte=0, n=0;
byte[] dati = new byte[10000];
while((nbyte=lettura.read(dati)) > 0)
n=n+nbyte;
return fileSize=n;
}
Object Work()
{
int nbyte=0, n=0;
try
{
for(int i=0; i<fileSize; i++)
{
upadateStato(i);
if(Thread.interrupted())
{
throw new InterruptedException();
}
Thread.sleep(500);
}
}
catch(InterruptedException e)
{
updateStato(0);
return "Upload Interrotto";
}
return "Upload effettuato con successo";
}
//azione chiamata dal bottone start
ActionListener startListener = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
startButton.setEnabled(false);
annullaButton.setEnabled(true);
// da finire
}
};
//azione chiamata alla pressione del tasto annulla
ActionListener annullaListener = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
//da fare
}
};
//interfaccia del programma
Sender()
{
//da fare
}
//altri metodi
}