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

  • SEARCH
  • Home
  • 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 677073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:59:55+00:00 2026-05-14T00:59:55+00:00

I am trying to write the simples GUI countdown. I found in Internet some

  • 0

I am trying to write the simples GUI countdown. I found in Internet some code but it is already too fancy for me. I am trying to keep it as simple as possible. So, I just want to have a window saying “You have 10 second left”. The number of second should decrease every second from 10 to 0. I wrote a code. And I think I am close to the working solution. But I still missing something. Could you pleas help me to find out what is wrong? Here is my code:

import javax.swing.*;

public class Countdown {

    static JLabel label;

    // Method which defines the appearance of the window.   
    private static void showGUI() {
        JFrame frame = new JFrame("Simple Countdown");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel label = new JLabel("Some Text");
        frame.add(label);
        frame.pack();
        frame.setVisible(true);
    }

    // Define a new thread in which the countdown is counting down.
    static Thread counter = new Thread() {
        public void run() {
            for (int i=10; i>0; i=i-1) {
                updateGUI(i,label);
                try {Thread.sleep(1000);} catch(InterruptedException e) {};
            }
        }
    };

    // A method which updates GUI (sets a new value of JLabel).
    private static void updateGUI(final int i, final JLabel label) {
        SwingUtilities.invokeLater(new Runnable(i,label) {

            public Runnable(int i, JLabel label) {
                this.i = i;
                this.label = label;
            }

            public void run() {
                label.setText("You have " + i + " seconds.");
            }

        });
    }

    // The main method (entry point).
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                showGUI();
                //counter.start();
            }
        });
        //counter.start();
    }

}

And I have several concrete question about this code:

  1. Where should I place the counter.start();? (In my code I put it on 2 places. Which one is correct?)

  2. Why compiler complains about the constructor for Runnable? It says that I have an invalid method declaration and I need to specify the returned type.

ADDED:
I made the suggested corrections. And then I execute the code and get:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Worker.run(Worker.java:12)

In the Worker.java in the line 12 I have: label.setText("You have " + i + " seconds.");.

  • 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-05-14T00:59:56+00:00Added an answer on May 14, 2026 at 12:59 am

    Call counter.start() inside the runnable:

    // The main method (entry point).
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                showGUI();
                counter.start();
            }
        });
    }
    

    You really want a specific order of invocation, if you place it outside the thread then the counter will start even before the GUI exists and it will fail on you.

    For the second question:

    // A method which updates GUI (sets a new value of JLabel).
    private static void updateGUI(final int i, final JLabel label) {
        SwingUtilities.invokeLater(new Worker(i, label));
    }
    

    Here is the worker:

    import javax.swing.JLabel;
    
    public class Worker implements Runnable{
        private int i;
        private JLabel label;
        public Worker(int i, JLabel label) {
            this.i = i;
            this.label = label;
        }
    
        public void run() {
            label.setText("You have " + i + " seconds.");
        }
    }
    

    And your main now:

    // The main method (entry point).
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Countdown.showGUI();
                counter.start();
            }
        });
    }
    

    UPDATE:
    Or if you still want to use the anonymous pattern then:

    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingUtilities;
    
    public class Countdown {
    
        static JLabel label;
    
        // Method which defines the appearance of the window.   
        public static void showGUI() {
            JFrame frame = new JFrame("Simple Countdown");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            label = new JLabel("Some Text");
            frame.add(label);
            frame.pack();
            frame.setVisible(true);
        }
    
        // Define a new thread in which the countdown is counting down.
        public static Thread counter = new Thread() {
            public void run() {
                for (int i=10; i>0; i=i-1) {
                    updateGUI(i,label);
                    try {Thread.sleep(1000);} catch(InterruptedException e) {};
                }
            }
        };
    
        // A method which updates GUI (sets a new value of JLabel).
        private static void updateGUI(final int i, final JLabel label) {
            SwingUtilities.invokeLater( 
                new Runnable() {
                    public void run() {
                        label.setText("You have " + i + " seconds.");
                    }
                }
            );
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 411k
  • Answers 411k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You are mixing two things. LINQ to SQL is an… May 15, 2026 at 7:51 am
  • Editorial Team
    Editorial Team added an answer The new line needs to be "\r\n". Better still -… May 15, 2026 at 7:51 am
  • Editorial Team
    Editorial Team added an answer MVC has three main components. Model - View and Controller.… May 15, 2026 at 7:51 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.