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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:47:45+00:00 2026-06-01T10:47:45+00:00

I was reading different threads on the subject which suggested the Swing Timer class

  • 0

I was reading different threads on the subject which suggested the Swing Timer class or SwingUtilities.InvokeLater

…but I am having a lot of trouble wrapping my head around them.

I used atomicInteger to create my countdown timer and it works fine in the console. However, When I try to incorporate it in Swing, it only updates the starting and ending value (e.g. set a 5 sec countdown will display in the frame: “5” -> after 5 seconds -> “0”.

Is there any simple way for me to keep and “refresh” my atomicInteger countdown label, or the only way is using the Swing Timer class?

Thank you for your patience!

ps. not homework, just trying to make myself a custom timer to study. (ie. procrastinating)

I hope this class is enough, please let me know if you need the frame/panel code as well.

private class ClickListener implements ActionListener{

     public void actionPerformed(ActionEvent e){            
         int t_study = 5;
         atomicDown.set(t_study);

         if (e.getSource() == b_study){
             while(atomicDown.get() > 0){       
                t_study = atomicDown.decrementAndGet(); 
        l_studyTime.setText(Integer.toString(t_study));
             try {
                Thread.sleep(1000);
             }
             catch (InterruptedException e1) {
                 System.out.println("ERROR: Thread.sleep()");   
                 e1.printStackTrace();
             }
          }
     }
     else if(e.getSource() == b_exit){
         System.exit(0); 
     }
     else
         System.out.println("ERROR: button troll");
     }
}
  • 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-01T10:47:46+00:00Added an answer on June 1, 2026 at 10:47 am

    After turning the code snippet into an SSCCE, this is what I get (which seems to work – as best as I understand the original code).

    I have not changed the variable names. Please learn common Java naming conventions1 for class, method & attribute names & use it consistently.

    1. Specifically names like b_study should be more along the lines of studyButton or similar. Some will note that ‘button’ should not be part of the name, but when you have a GUI with both a ‘Study’ button & label, I don’t see any other logical way to separate them.

    import java.awt.event.*;
    import javax.swing.*;
    import java.util.concurrent.atomic.AtomicInteger;
    
    class TimerTicker {
    
        public static final int STUDY_TIME = 15;
        AtomicInteger atomicDown = new AtomicInteger(STUDY_TIME);
        JButton b_study;
        JButton b_exit;
        JLabel l_studyTime;
    
        TimerTicker() {
            JPanel gui = new JPanel();
    
            b_study = new JButton("Study");
            ClickListener listener = new ClickListener();
            b_study.addActionListener(listener);
            gui.add(b_study);
    
            b_exit = new JButton("Exit");
            b_exit.addActionListener(listener);
            gui.add(b_exit);
    
            l_studyTime = new JLabel("" + atomicDown.get());
            gui.add(l_studyTime);
    
            JOptionPane.showMessageDialog(null, gui);
        }
    
        private class ClickListener implements ActionListener {
    
            Timer timer;
    
            public void actionPerformed(ActionEvent e){
                if (e.getSource() == b_study) {
                    ActionListener countDown = new ActionListener() {
    
                        public void actionPerformed(ActionEvent ae) {
                            if (!(atomicDown.get() > 0)) {
                                timer.stop();
                                // reset the count.
                                atomicDown.set(STUDY_TIME);
                            } else {
                                l_studyTime.setText(
                                    Integer.toString(
                                        atomicDown.decrementAndGet()));
                            }
                        }
                    };
                    timer = new Timer(1000,countDown);
                    timer.start();
                } else if(e.getSource() == b_exit) {
                    System.exit(0);
                } else {
                    System.out.println("ERROR: button troll");
                }
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new TimerTicker();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does having 2 different threads : one reading from a C# array (e.g from
I was reading http://embeddedgurus.com/embedded-bridge/2010/03/different-bit-types-in-different-registers/ , which said: With read/write bits, firmware sets and clears
I know this is not a specific question, but I'm reading about this subject
After reading a lot on this subject and discussing on IRC, the response seems
Could I close tcp::socket in different thread from the sync-reading thread? It looks like:
I have been reading different articles on memory management in preparing for how I
I am trying to learn assembly my self, and I have been reading different
There seem to be very different opinions about using transactions for reading from a
I have been reading the docs and playing with different EventQuery parameters for days
Are these actually three different concepts or am I getting jumbled? (I've been reading

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.