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 8437551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:27:45+00:00 2026-06-10T07:27:45+00:00

I have a JFrame on which I want to simulate a countdown (like a

  • 0

I have a JFrame on which I want to simulate a countdown (like a rocket launch). So I set up the frame by hiding various controls (setVisible(false)) and displaying a JLabel with the text (this is the text that is supposed to countdown: 3, 2, 1, Go).

The text on that JLabel starts out at “3”. My intent is to simply have the execution of the program wait for 1 second, then change the text to “2”, wait another second, change to “1”, etc.). At the end, I hide the JLabel and redisplay all of the controls and everything proceeds as normal.

What I am doing isn’t working. It seems to wait for the correct amount of time, and when it is done, my JFrame looks great and works as intended. But during the 4 seconds of the countdown method, all I see is a white JFrame. Not the 3, 2, 1 that I want.

Here is my code. Can anyone see what I am doing wrong? Thanks!

public void countdown() {
    long t0, t1;        

    myTest.hideTestButtons(true);
    myTest.repaint();

    t0 =  System.currentTimeMillis();
    do {
        t1 = System.currentTimeMillis();
    } while ( (t1 - t0) < 1000);

    myTest.TwoSeconds();
    myTest.repaint();
    t0 =  System.currentTimeMillis();
    do {
        t1 = System.currentTimeMillis();
    } while ( (t1 - t0) < 1000);


    myTest.OneSecond();
    myTest.repaint();
    t0 =  System.currentTimeMillis();
    do {
        t1 = System.currentTimeMillis();
    } while ( (t1 - t0) < 1000);


    myTest.Go();
    myTest.repaint();
    t0 =  System.currentTimeMillis();
    do {
        t1 = System.currentTimeMillis();
    } while ( (t1 - t0) < 1000);

    myTest.hideTestButtons(false);
    myTest.repaint();
}

public void TwoSeconds() {
    lblCountdown.setText("2");
}

public void OneSecond() {
    lblCountdown.setText("1");
}

public void Go() {
    lblCountdown.setText("Go!");
}
  • 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-10T07:27:47+00:00Added an answer on June 10, 2026 at 7:27 am

    Use a Timer instead. An active wait is in most of the cases highly discouraged.
    Here is the type of code you’ll need to integrate:

    final Timer ti = new Timer(0, null);
    ti.addActionListener(new ActionListener() {
        int countSeconds = 3;
    
        @Override
        public void actionPerformed(ActionEvent e) {
            if(countSeconds == 0) {
                lblCountdown.setText("Go");
                ti.stop();
            } else {
                lblCountdown.setText(""+countSeconds);
                countSeconds--;
            }
        }
    });
    ti.setDelay(1000);
    ti.start();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JFrame and In which I want to show a JInternal Frame
I have a JFrame which is maximized ( frame.setExtendedState(JFrame.MAXIMIZED_BOTH) ). I want to find
I want one JFrame to have a method like this: private void someEvent(java.awt.event.ActionEvent evt){
So I have created this JFrame window which has a lot of items like
I have this Java JFrame class, in which I want to use a boxlayout,
I have a JFrame and a Jpanel over that in which various buttons are
I have a JFrame (well, a class which extends JFrame) and I want to
I have a JFrame which on instantiation calls a custom JDialog(Login). If login is
I have a JFrame which consists only of a JList , the JList has
I have created jframe in which jpanels are added dynamically what i can't do

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.