JWait
public class JWait extends JLabel implements Runnable {
private final String MESSAGE = "Wait, please";
private boolean running = false;
public JWait() {
this.setText(MESSAGE);
this.setOpaque(true);
this.setBackground(Color.YELLOW);
}
public void start() {
if (!running) {
running = true;
Thread t = new Thread(this);
t.setDaemon(true);
t.start();
}
}
public void stop() {
running = false;
}
public void reset() {
this.setText(MESSAGE);
}
@Override
public void run() {
while (running) {
this.setText(this.getText() + ".");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}
Progetto Eclipse: ThreadExample.zip
Per la spiegazione teorica sui thread fare riferimento al testo: Java per Robot, pag. 121
oppure alla pagina: http://www.claudiodesio.com/java/tutorialjavathread/threads.htm