Results 1 to 7 of 7
  1. #1
    firecat318 is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default Need help with timer

    Hi I am new to java Timer and I can't really find a good place to teach it to me. Anyways I tried to take a shot and make a program that lets you input a number to start at then the timer will count down to 0, updating the JLabel all the while. It doesn't work (it compiles, but it won't function like I want it to). I'm sure it has to do with the Timer thing because I really don't know what to do in regards to that.

    Java Code:
    package MeLearningh;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class time extends JFrame {
        JLabel label, timer;
        int counter;
        JTextField tf;
        JButton button;
    
        public time() {
            setLayout(new GridLayout(2, 2, 5, 5));
    
            label = new JLabel("Enter seconds:", SwingConstants.CENTER);
            add(label);
    
            tf = new JTextField(5);
            add(tf);
    
            button = new JButton("Start Timing");
            add(button);
    
            timer = new JLabel("Time left: " + counter, SwingConstants.CENTER);
            add(timer);
    
            event e = new event();
            button.addActionListener(e);
        }
    
        public class event implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                int seconds = (int)(Double.parseDouble(tf.getText()));
    
                do {
                    Timer t = new Timer(1000, this);
                    counter--;
                } while(counter >= seconds);
    
                timer.setText("Time left: " + counter);
            }
        }
    
        public static void main(String args[]) {
            time gui = new time();
            gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            gui.setSize(300,90);
            gui.setVisible(true);
            gui.setTitle("Timer Program");
        }
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    Start by reading the section from the Swing tutorial on How to Use Timers. Your code will be much simpler as all you need to do is subtract 1 from your counter every time the Timer fires and the set the text of the label.

  3. #3
    firecat318 is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default

    I tried reading that but it doesn't really click for me... I added this and now at least it counts.. but not correctly

    Java Code:
    public class event implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                int seconds = (int)(Double.parseDouble(tf.getText()));
    
                do {
                    Timer t = new Timer(1000, this);
                    t.start();
                    counter--;
                } while(counter >= seconds);
                
                timer.setText("Time left: " + counter);
            }
        }
    It starts at like -1 then goes -2, -4, -8, -16 etc etc

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Get that while loop out of there as the Timer itself will do the looping for you. You should not use "this" as an ActionListener as your class will likely need more than one ActionListener -- at least one for JButton(s?), and at least one for your Timer, and they really won't have code that they'll share..

    But mostly I second Rob Camick's recommendation: re-read the tutorial and study the sample code intently as it's all there laid out for you. The more you use the tutorials the easier they'll be to use, trust me!

    Much luck!

  5. #5
    firecat318 is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default

    This is the closest I've gotten so far... It starts but it won't time (it just displays the number that was entered into the textbox)

    Java Code:
    package MeLearningh;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class time extends JFrame {
        JLabel label, timer;
        int counter;
        JTextField tf;
        JButton button;
    
        timeClass tc = new timeClass();
        Timer timer1 = new Timer(1000, tc);
    
        public time() {
            setLayout(new GridLayout(2, 2, 5, 5));
    
            label = new JLabel("Enter seconds:", SwingConstants.CENTER);
            add(label);
    
            tf = new JTextField(5);
            add(tf);
    
            button = new JButton("Start Timing");
            add(button);
    
            timer = new JLabel("Time left: " + counter, SwingConstants.CENTER);
            add(timer);
    
            event e = new event();
            button.addActionListener(e);
        }
    
        public class event implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                timer1.start();
            }
        }
    
        public class timeClass implements ActionListener {
            public void actionPerformed(ActionEvent tc) {
                int timeStart = (int)(Double.parseDouble(tf.getText()));
                counter = timeStart;
    
                if(counter > 0) {
                    timer.setText("Time left: " + counter);
                } else {
                    timer1.stop();
                }
            }
        }
    
        public static void main(String args[]) {
            time gui = new time();
            gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            gui.setSize(300,90);
            gui.setVisible(true);
            gui.setTitle("Timer Program");
        }
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Let's look at your timeClass (that should be TimeClass as the first letter of classes should be capitalized):

    Java Code:
      public class TimeClass implements ActionListener {
        public void actionPerformed(ActionEvent tc) {
          int timeStart = (int) (Double.parseDouble(tf.getText()));
          counter = timeStart;
    
          if (counter > 0) {
            timer.setText("Time left: " + counter);
          } else {
            timer1.stop();
          }
        }
      }

    The key thing to note here is that everything in the Timer's ActionListener's actionPerformed method will get called each time that the timer loops. And so since your actionPerformed starts with this code:
    Java Code:
          int timeStart = (int) (Double.parseDouble(tf.getText()));
          counter = timeStart;

    Your timer will re-set itself to the number in the JTextField with each tick, and this is not what you want to do. The key here is to set the counter to this number before you start the timer and then decrement the counter in the Timer. For e.g.,

    Java Code:
    @SuppressWarnings("serial")
    public class FireCatTime extends JFrame {
      public static final int TIMER_DELAY = 1000; //!! change!
      JLabel label, timer;
      int counter;
      JTextField tf;
      JButton button;
    
      Timer timer1; //!! change
    
      public FireCatTime() {
        setLayout(new GridLayout(2, 2, 5, 5));
    
        label = new JLabel("Enter seconds:", SwingConstants.CENTER);
        add(label);
    
        tf = new JTextField(5);
        add(tf);
    
        button = new JButton("Start Timing");
        add(button);
    
        timer = new JLabel("Time left: " + counter, SwingConstants.CENTER);
        add(timer);
    
        event e = new event();
        button.addActionListener(e);
      }
    
      public class event implements ActionListener {
        public void actionPerformed(ActionEvent e) {
          //!! change
          try {
            int count = (int)(Double.parseDouble(tf.getText()));
            timer.setText("Time left: " + count); 
            TimeClass timeClass = new TimeClass(count);
            timer1 = new Timer(TIMER_DELAY, timeClass); 
            timer1.start();
          } catch (NumberFormatException e2) {
            e2.printStackTrace();
          }
        }
      }
    
      public class TimeClass implements ActionListener {
        private int counter;
        
        //!! when constructing the Timer object, set it's counter
        public TimeClass(int counter) {
          this.counter = counter;
        }
        
        public void actionPerformed(ActionEvent tc) {
          counter--;  // decrement the counter
          if (counter >= 0) {
            timer.setText("Time left: " + counter);
          } else {
            timer1.stop();
          }
        }
      }
    
      public static void main(String args[]) {
        FireCatTime gui = new FireCatTime();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(300, 90);
        gui.setVisible(true);
        gui.setTitle("Timer Program");
      }
    }

  7. #7
    firecat318 is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Timer help
    By Kinyo in forum New To Java
    Replies: 15
    Last Post: 03-15-2009, 02:37 AM
  2. EJB Timer
    By mrjunsy in forum Advanced Java
    Replies: 0
    Last Post: 08-22-2008, 04:09 PM
  3. EJB Timer
    By mrjunsy in forum New To Java
    Replies: 0
    Last Post: 08-04-2008, 06:47 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:46 PM
  5. Page Timer
    By deeadeed in forum New To Java
    Replies: 0
    Last Post: 12-05-2007, 08:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •