ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//...Perform a task...
logger.finest("Reading SMTP Info.");
}
};
Timer timer = new Timer(100 ,taskPerformer);
timer.setRepeats(false);
timer.start();
According to the documentation this timer should fire once but it never fires. I need it to fire once.
This simple program works for me:
Your task likely only needs to report results on the event thread (EDT) but do the actual work in a background thread at some periodic rate.
ScheduledExecutorService is EXACTLY what you want. Just remember to update the state of your UI on the EDT via SwingUtility.invokeLater(...)
I'm guessing from the log statement that you're doing some sort of SMTP operation. I think I'm right in saying the
java.swing.Timer
is intended for UI related timed operations, hence why it needs and EDT running. For more general operations you should usejava.util.Timer
.This article is linked from the JavaDocs - http://java.sun.com/products/jfc/tsc/articles/timer/
This Program will work fine...
setRepeats(boolean flag)
function used to set call thefunction(actionPerformed)
repeatedly or only one time iftimer.setRepeats(false) == timer
calls the actionperformed method for only one timetimer.setRepeats(true) == timer
calls the actionPerformed method repeatedly based on specified timeSwing Timer Work
steps to create swing timer:
actionPerformed()
function in which do your tasktimer.start()
for start the task between the time specified in timer constructor, usetimer.stop()
for stop the taskExample: