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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:07:37+00:00 2026-06-18T05:07:37+00:00

Let me explain what I am trying to do. I have two classes extending

  • 0

Let me explain what I am trying to do.

I have two classes extending JFrame, the StartJFrame and TestingJFrame. In the main method, I start up a StartJFrame. It has a single button, start. When that is pressed, I have it set up to hide that frame and start up the TestingJFrame. Right now I don’t have anything in the TestingJFrame.

In that screen, I want to have a label in the bottom right corner that is a timer, starting on 45 seconds and counting down to 0. I also need to have some code run every 10th of a second, and collect some data. There will be two buttons in the TestingJFrame, Yes and No. When one of them is pressed, it should stop the timer and save the information.

The data is basically just doubles. I am only going to be collecting data once per run of the program. I have a UserData class that holds some information about the test subject, and a list of doubles, it is added to every 10th of a second. I have a general idea how to save the data in java.

My question is, how should I set up the timer, so that it will count down from 45 seconds, and when it reaches 0 or the user presses the yes or no button it will call a function to save the data? I think I can handle the saving data part.

Sorry if this is really easy, I’m new to Java (from c#) and Swing has been confusing me a bit.

  • 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-18T05:07:38+00:00Added an answer on June 18, 2026 at 5:07 am

    The first part (show the count down and stopping the timer) is relatively easy…

    public class TimerTest01 {
    
        public static void main(String[] args) {
            new TimerTest01();
        }
    
        public TimerTest01() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
    
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private JLabel label;
            private Timer timer;
            private long startTime;
    
            public TestPane() {
                setLayout(new GridLayout(0, 1));
                label = new JLabel("...");
                label.setHorizontalAlignment(JLabel.CENTER);
                final JButton btn = new JButton("Start");
                btn.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (timer.isRunning()) {
                            timer.stop();
                            btn.setText("Start");
                        } else {
                            startTime = System.currentTimeMillis();
                            timer.restart();
                            btn.setText("Stop");
                        }
                        repaint();
                    }
                });
                add(label);
                add(btn);
    
                timer = new Timer(250, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        long endTime = (startTime + 45000);
                        long timeLeft = endTime - System.currentTimeMillis();
                        if (timeLeft < 0) {
                            timer.stop();
                            label.setText("Expired");
                            btn.setText("Start");
                        } else {
                            label.setText(Long.toString(timeLeft / 1000));
                        }
                        repaint();
                    }
                });
            }
        }
    }
    

    Take a look at Swing Timer for more info

    The second part of you question is to vague to reliably provide you with an answer. Where is the data coming from? How is collected??

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with Facebook. Let me explain what I'm trying to do.
I have only started learning Python recently. Let me explain what I am trying
Let me first explain what's I'm trying to do. So I have a entity
Let me try to explain what I have on view and what's I'm trying
Let me try to explain My case, I have two table and each of
Let me explain a bit more, I am trying to have a large area
Quite new to maven here so let me explain first what I am trying
I know this sounds somewhat counterintuitive, but let me explain what I am trying
Let me explain best with an example. Say you have node class that can
Let me explain you my situation. I have a base class called Shape, and

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.