Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing Timer

Tags:

java

timer

swing

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.

like image 559
Hamza Yerlikaya Avatar asked Jun 17 '09 12:06

Hamza Yerlikaya


People also ask

What is timer in Java Swing?

You can use Swing timers in two ways: To perform a task once, after a delay. For example, the tool tip manager uses Swing timers to determine when to show a tool tip and when to hide it. To perform a task repeatedly. For example, you might perform animation or update a component that displays progress toward a goal.


1 Answers

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);
    }
}
like image 171
kgiannakakis Avatar answered Oct 12 '22 22:10

kgiannakakis