Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9178119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:28:41+00:00 2026-06-17T17:28:41+00:00

Possible Duplicate: Stop timer with conditional only works first time? I’m very confused as

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:28:43+00:00Added an answer on June 17, 2026 at 5:28 pm

    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...
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: NSTimer doesn't stop I am having a hard time stopping my timer,
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: Show and hide divs at a specific time interval using jQuery I
Possible Duplicate: NSTimer doesn't stop I am using a NSTimer to update the value
Possible Duplicate: NSTimer doesn't stop I am using [NSThread exit] to leave the NSTimer
Possible Duplicate: Problem of using cin twice This code works but not what I
Possible Duplicate: How to stop C++ console application from exiting immediately? I'm using fstream
Possible Duplicate: How To Start And Stop A Continuously Running Background Worker Using A
Possible Duplicate: Why does overflow hidden stop floating elements escaping their container? Why my
Possible Duplicate: Best way to stop SQL Injection in PHP I am creating a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.