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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:39:32+00:00 2026-05-24T01:39:32+00:00

I’ve got a status JLabel in one class (named Welcome) and the timer in

  • 0

I’ve got a “status” JLabel in one class (named Welcome) and the timer in another one (named Timer). Right now, the first one displays the word “status” and the second one should be doing the countdown. The way I would like it to be, but don’t know how to – display 10, 9, 8, 7 … 0 (and go to the next window then). My attempts so far:

// class Welcome

setLayout(new BorderLayout());
JPanel area = new JPanel();
JLabel status = new JLabel("status");
area.setBackground(Color.darkGray);
Font font2 = new Font("SansSerif", Font.BOLD, 25);
status.setFont(font2);
status.setForeground(Color.green);      
area.add(status, BorderLayout.EAST); // can I put it in the bottom-right corner?
this.add(area);

and the timer:

 public class Timer implements Runnable {

//  public void runThread() {
//      new Thread(this).start();
//  }

public void setText(final String text) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            setText(text); // link to status here I guess
        }
    });
}

public void run() {
    for (int i = 10; i > 0; i--) {
        // set the label
        final String text = "(" + i + ") seconds left";
        setText(text);

//          // sleep for 1 second
//          try {
//              Thread.currentThread();
//              Thread.sleep(1000);
//          } catch (Exception ex) {
//          }
    }
    // go to the next window
    UsedBefore window2 = new UsedBefore();
    window2.setVisible(true);
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    // runThread();
}

} // end class
  • 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-24T01:39:32+00:00Added an answer on May 24, 2026 at 1:39 am

    I agree that you should consider using a “Java” Timer as per Anh Pham, but in actuality, there are several Timer classes available, and for your purposes a Swing Timer not a java.util.Timer as suggested by Anh would suit your purposes best.

    As for your problem, it’s really nothing more than a simple problem of references. Give the class with the label a public method, say setCountDownLabelText(String text), and then call that method from the class that holds the timer. You’ll need to have a reference of the GUI class with the timer JLabel in the other class.

    For example:

    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class Welcome extends JPanel {
       private static final String INTRO = "intro";
       private static final String USED_BEFORE = "used before";
       private CardLayout cardLayout = new CardLayout();
       private JLabel countDownLabel = new JLabel("", SwingConstants.CENTER);
    
       public Welcome() {
          JPanel introSouthPanel = new JPanel();
          introSouthPanel.add(new JLabel("Status:"));
          introSouthPanel.add(countDownLabel);
    
          JPanel introPanel = new JPanel();
          introPanel.setPreferredSize(new Dimension(400, 300));
          introPanel.setLayout(new BorderLayout());
          introPanel.add(new JLabel("WELCOME", SwingConstants.CENTER), BorderLayout.CENTER);
          introPanel.add(introSouthPanel, BorderLayout.SOUTH);
    
          JPanel usedBeforePanel = new JPanel(new BorderLayout());
          usedBeforePanel.setBackground(Color.pink);
          usedBeforePanel.add(new JLabel("Used Before", SwingConstants.CENTER));
    
          setLayout(cardLayout);
          add(introPanel, INTRO);
          add(usedBeforePanel, USED_BEFORE);
    
          new HurdlerTimer(this).start();
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("Welcome");
          frame.getContentPane().add(new Welcome());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    
       public void setCountDownLabelText(String text) {
          countDownLabel.setText(text);
       }
    
       public void showNextPanel() {
          cardLayout.next(this);
       }
    }
    
    class HurdlerTimer {
       private static final int TIMER_PERIOD = 1000;
       protected static final int MAX_COUNT = 10;
       private Welcome welcome; // holds a reference to the Welcome class
       private int count;
    
       public HurdlerTimer(Welcome welcome) {
          this.welcome = welcome; // initializes the reference to the Welcome class.
          String text = "(" + (MAX_COUNT - count) + ") seconds left";
          welcome.setCountDownLabelText(text);
       }
    
       public void start() {
          new Timer(TIMER_PERIOD, new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                if (count < MAX_COUNT) {
                   count++;
                   String text = "(" + (MAX_COUNT - count) + ") seconds left";
                   welcome.setCountDownLabelText(text); // uses the reference to Welcome
                } else {
                   ((Timer) e.getSource()).stop();
                   welcome.showNextPanel();
                }
             }
          }).start();
       }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but

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.