17
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.

stealthjong
  • 10,858
  • 13
  • 45
  • 84
Hamza Yerlikaya
  • 49,047
  • 44
  • 147
  • 241

4 Answers4

21

This simple program works for me:

import java.awt.event.*;
import javax.swing.*;

public class Test {
    public static void main(String [] args) throws Exception{
        ActionListener taskPerformer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                //...Perform a task...

                System.out.println("Reading SMTP Info.");
            }
        };
        Timer timer = new Timer(100 ,taskPerformer);
        timer.setRepeats(false);
        timer.start();

        Thread.sleep(5000);
    }
}
stealthjong
  • 10,858
  • 13
  • 45
  • 84
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • Thanks, i found my problem logger is initialized after this code is run that's why i never saw my test messages. switching logger with println helped. – Hamza Yerlikaya Jun 17 '09 at 12:24
  • IIRC, you shouldn't use javax.swing.Timer off the EDT. – Tom Hawtin - tackline Jun 17 '09 at 13:14
  • I think I love you right now! :D Thank you very much! – misty Sep 05 '16 at 02:34
  • This answer suggests that the main thread was finishing before the timer finishes. Since the main thread is special, all other non-daemon threads will also terminate when main() terminates. To avoid this (without using daemon threads) you should get a reference to the timer thread, and in main() call `.join();` – Lightfire228 Jul 13 '17 at 14:04
6

This Program will work fine...

setRepeats(boolean flag) function used to set call the function(actionPerformed) repeatedly or only one time if

  1. timer.setRepeats(false) == timer calls the actionperformed method for only one time
  2. timer.setRepeats(true) == timer calls the actionPerformed method repeatedly based on specified time

Swing Timer Work

  1. do the task one time
  2. do the task repeated time

steps to create swing timer:

  1. create the actionlistener
  2. create the timer constructor then pass time and actionlistener in that
  3. implement the actionPerformed() function in which do your task
  4. use timer.start() for start the task between the time specified in timer constructor, use timer.stop() for stop the task

Example:

ActionListener al=new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
        //do your task
        if(work done)
            timer.stop();//stop the task after do the work
    }
};
Timer timer=new Timer(1000,al);//create the timer which calls the actionperformed method for every 1000 millisecond(1 second=1000 millisecond)
timer.start();//start the task
Star Man
  • 171
  • 1
  • 2
  • 11
nandhakumar.c
  • 71
  • 1
  • 2
  • hey your syntax is slightly wrong but I like the simple snippet `ActionListener al=new ActionListener(){//code};` is correct instead of `ActionListener al=new ActionListener({//code});` – Georodin Sep 03 '20 at 10:35
1

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(...)

basszero
  • 29,624
  • 9
  • 57
  • 79
1

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 use java.util.Timer.

This article is linked from the JavaDocs - http://java.sun.com/products/jfc/tsc/articles/timer/

Nick Holt
  • 33,455
  • 4
  • 52
  • 58