We help IT Professionals succeed at work.

TimerListener...

Mike Eghtebas
on
The following code prints 1000 times in a second. How can I change it to fire only once per second:

    private class TimerListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
           System.out.print("Somthing");
        }
Comment
Watch Question

Awarded 2011
Awarded 2011

Commented:


http://docs.oracle.com/javase/6/docs/api/javax/swing/Timer.html#Timer%28int,%20java.awt.event.ActionListener%29

"Creates a Timer and initializes both the initial delay and between-event delay to delay milliseconds"


isn't betwenn-event delay - what yiou wnat?
Awarded 2011
Awarded 2011

Commented:

make it this way - and you'll see -  it changes every 3 seconds
 private Timer timer = new Timer(3000, new TimerListener());

Commented:
Where is your timer initialised.

You will probably have something along the lines of

new Timer(delay, new TimerListener()).start();

Change your delay value from 1 to 1000.
Awarded 2011
Awarded 2011
Commented:
this of course will fire only once
private Timer timer = new Timer(1000, new TimerListener());
Mike EghtebasDatabase and Application Developer

Author

Commented:
yes, i will do.