Use javax.swing.Timer to make countdown timer in Java

CodeAddict picture CodeAddict · Jan 18, 2013 · Viewed 21.1k times · Source

Possible Duplicate:
Stop timer with conditional only works first time?

I'm very confused as to how to make a timer using the swing and not util timer.

I am making a game where users have to answer questions in a 30 second time limit. I have a PlayFrame, where the time is shown, and a method inside PlayFrame called startTimer which contains all the timer stuff.

public static void startTimer() {

    int elapsedSeconds = 0;
    javax.swing.Timer myTimer = new javax.swing.Timer(1000, new MyTimerActionListener());
    elapsedSeconds++;

    if (elapsedSeconds == 30) {
        myTimer.stop();
        timerLabel.setText("0");
        wrong();
    } else {
        String text = String.format("f", 30 - elapsedSeconds);
        timerLabel.setText(text);
    }

    if (myTimer != null && myTimer.isRunning()) {
        myTimer.stop();
        myTimer = null;
        timerLabel.setText("0");
    } else {
        elapsedSeconds = 0;
        myTimer = new javax.swing.Timer(1000, new MyTimerActionListener());
        myTimer.start();
        String text = String.format("t", 30);
        timerLabel.setText(text);
    }
}

What I want this method to do is have a timer that counts down from 30 until the question is answered correctly. If the answer is answered incorrectly I want the timer to stop.

For an answer perhaps some psuedocode (or real code) to move me in the right direction. And this is for personal use, not homework or anything.

Please remember that this is a part of my whole code and it needs to work with other parts of it. I'll give more information upon request, thanks!

EDIT: New startTimer() method NOTE all System.out.print is for testing only:

  public static void startTimer() {
    class TimerListener implements ActionListener {
        Timer timer = new Timer(1000, new TimerListener());
        int elapsedSeconds = 30;
        String seconds = Integer.toString(elapsedSeconds);

        @Override
        public void actionPerformed(ActionEvent evt) {
            timer.start();
            if (elapsedSeconds == 0) {
                //System.out.print("here");
                timer.stop();
                PlayFrame.wrong();
            }
            else{
                //System.out.print("hersfde");
                elapsedSeconds--;
            PlayFrame.timerLabel.setText(seconds);
            }
            //System.out.println(elapsedSeconds);
        }
    }
    //System.out.print("l");
}

Doesn't do anything and not sure why.

Answer

Mordechai picture Mordechai · Jan 18, 2013

This is how I would make a countdown timer:

Timer timer = new Timer(1000, new TimerListener());

class TimerListener implements ActionListener{
    int elapsedSeconds = 30;

    public void actionPerformed(ActionEvent evt){
        elapsedSeconds--;
        timerLabel.setText(elapsedSeconds)
        if(elapsedSeconds <= 0){
            timer.stop();
            wrong()
            // fill'er up here...
        }
    }

}