Java/Swing Components/Timer
Версия от 18:01, 31 мая 2010; (обсуждение)
Timer Bean
/**
* @version 1.10 1999-09-28
* @author Cay Horstmann
*/
import java.io.Serializable;
import java.util.Date;
import java.util.EventListener;
import java.util.EventObject;
import java.util.Vector;
public class TimerBean implements Runnable, Serializable {
public int getInterval() {
return interval;
}
public void setInterval(int i) {
interval = i;
}
public boolean isRunning() {
return runner != null;
}
public void setRunning(boolean b) {
if (b && runner == null) {
runner = new Thread(this);
runner.start();
} else if (!b && runner != null) {
runner.interrupt();
runner = null;
}
}
public synchronized void addTimerListener(TimerListener l) {
timerListeners.addElement(l);
}
public synchronized void removeTimerListener(TimerListener l) {
timerListeners.removeElement(l);
}
public void fireTimerEvent(TimerEvent evt) {
Vector currentListeners = null;
synchronized (this) {
currentListeners = (Vector) timerListeners.clone();
}
for (int i = 0; i < currentListeners.size(); i++) {
TimerListener listener = (TimerListener) currentListeners
.elementAt(i);
listener.timeElapsed(evt);
}
}
public void run() {
if (interval <= 0)
return;
try {
while (!Thread.interrupted()) {
Thread.sleep(interval);
fireTimerEvent(new TimerEvent(this));
}
} catch (InterruptedException e) {
}
}
private int interval = 1000;
private Vector timerListeners = new Vector();
private Thread runner;
}
class TimerEvent extends EventObject {
public TimerEvent(Object source) {
super(source);
now = new Date();
}
public Date getDate() {
return now;
}
private Date now;
}
interface TimerListener extends EventListener {
public void timeElapsed(TimerEvent evt);
}