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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

2 Answers2

3

You can use javax.swing.Timer to setup your timer. You can have a look at the official tutorial too.

Cyrille Ka
  • 15,328
  • 5
  • 38
  • 58
  • Thanks! Although that wasn't my only question. How do I use the label to be put in the bottom right corner and be updated every second? – user2018829 Feb 04 '13 at 00:06
  • @user2018829 You use a layout manager that meets your needs. Take a look at [A Visual Guide to Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) and [Using Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html). Also, you use the timer to count backwards ;) – MadProgrammer Feb 04 '13 at 00:13
  • +1 for the Timer suggestion and the link to the tutorial. As a new Swing programmer I'm sure the poster will find all the section in the tutorial beneficial. – camickr Feb 04 '13 at 00:24
3

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??

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks! Nevermind the second question, all I really need to do is have the timer be updated every 10th of a second, and have a method to place code in. But what I don't understand is how to have the timer be every 10th a a second, but only have the label updated every second. If I have a variable to count the number of updates in the timer and use modulus to get every tenth one would that be suitable? Or is there a different way? – user2018829 Feb 04 '13 at 00:25
  • Just be aware that `SwingTimer` only guarantees that it will trigger a result at least every *n* milliseconds. It is better to sync back to the system clock (which has it's own issues), but would be more reliable. – MadProgrammer Feb 04 '13 at 00:31