Results 1 to 4 of 4
  1. #1
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Timer Class Issue

    Hi, I'm try to get this program to count continuously when the timer starts (simple as that), but there's either some code I'm missing or mis-formatting something..

    Here's the code:

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    
    public class TimerExample extends JFrame implements ActionListener
    {
      
      private Timer tmrTimer;
      private JLabel lblTimer = new JLabel();
      
      public TimerExample()
      {
        
        tmrTimer = new Timer (1000, this);
        tmrTimer.start();
        
        JPanel pnlTimer = new JPanel();
        
        
        pnlTimer.add(lblTimer);
        add(pnlTimer);
        
        setTitle ("Timer Example");
        setSize(300, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
      }
      
      public void actionPerformed (ActionEvent e)
      {
        int seconds = 0;
        seconds++;
        lblTimer.setText(Integer.toString(seconds));
      }
      
      public static void main (String[]args)
      {
        new TimerExample();
      }
    }
    A frame is supposed to pop up with nothing visible on it. Then after an interval of one second, the Timer starts, and displays a number. But after that it doesn't continue to count upwards. Anyone know a way to fix this or a proper solution?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Timer Class Issue

    But after that it doesn't continue to count upwards.

    Java Code:
    int seconds = 0;
    seconds++;
    The above code will not count upwards...it will always result in1. Move the seconds variable to be an instance variable.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Timer Class Issue

    it doesn't continue to count upwards.
    Have a look at what you do in actionPerformed() when there is a "tick":

    Java Code:
    public void actionPerformed (ActionEvent e)
    {
        int seconds = 0;
        seconds++;
        lblTimer.setText(Integer.toString(seconds));
    }
    You declare and initialise a variable, increment it, and then use its value - which will always be 1 - for the label. Make seconds an instance variable instead, so its value can be remembered from tick to tick.

  4. #4
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: Timer Class Issue

    One of my ridiculously silly moments again Why didn't I notice such a mistake..

    Thanks everyone for the clearance (or rather sorry for such carelessness )

Similar Threads

  1. Stop a timer in other class
    By warchieflll in forum Advanced Java
    Replies: 15
    Last Post: 02-02-2011, 09:13 PM
  2. How to make swing.Timer as a separate class
    By nethz13 in forum New To Java
    Replies: 9
    Last Post: 04-18-2010, 10:14 AM
  3. Java Game Timer Issue! Help
    By smithywill in forum Advanced Java
    Replies: 2
    Last Post: 03-11-2010, 10:09 AM
  4. Help with Timer Class
    By morfasto in forum New To Java
    Replies: 2
    Last Post: 11-03-2009, 10:13 PM
  5. [SOLVED] Swing Timer issue
    By Doctor Cactus in forum New To Java
    Replies: 6
    Last Post: 03-03-2009, 01:25 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
  •