Results 1 to 3 of 3
  1. #1
    lithium002 is offline Member
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default Timer Program.. please help!

    Hello everyone,

    I'm trying to program an application in Java that shows a JTextField to countdown from 59 to 0, and once it hits 0, go back to 59 again.

    I'm having trouble to just trying to initialize to countdown, and after having tried so many things, I really need someone to put me in the right direction since this is my first time using event listeners and the Timer class in Java.

    Here is the version of my program that compiles:
    Java Code:
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.Timer;
    
    /**
    	This program demonstrates how to install an action listener to a timer event to countdown from 59 to 0 and then back again.
    */
    public class Button
    {
    	public static void main(String[] args)
    	{
    		JFrame frame = new JFrame();
    		JLabel label = new JLabel("Time remaining: ");
    	
    		final JTextField textField = new JTextField("59", 4);
    		
    		JPanel panel = new JPanel();
    		panel.add(label);
    		panel.add(textField);
    		frame.add(panel);
    		
    		class Click implements ActionListener
    		{
    			public void actionPerformed(ActionEvent click)
    			{
    				for(int i = 59; i > 0; i--)
    				{
    					String textCounter = Integer.toString(i);
    					textField.setText(textCounter);
    				}
    			}
    		}
    		
    		Click listener = new Click();
    		
    		Timer t = new Timer(1000, listener);
    		t.start();
    		
    		frame.setSize(400, 100);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
    }
    I tried setting up a final variable and then updating it within the Click event but I get a syntax error since a final variable cannot be changed. I also tried initializing an int variable to 59 within the Click event but each time the timer goes off, the text field only shows 59 since it's a local variable.

    Can anyone please help?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Change your for loop as follows and see first of all.

    Java Code:
    for (int i = 59; i > 0; i--) {
           System.out.println(i);
    }

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Quote Originally Posted by lithium002 View Post
    I tried setting up a final variable and then updating it within the Click event but I get a syntax error since a final variable cannot be changed. I also tried initializing an int variable to 59 within the Click event but each time the timer goes off, the text field only shows 59 since it's a local variable.
    Make it an instance field. Decrement it in the Timer's Action.

    db
    Last edited by DarrylBurke; 03-08-2010 at 07:01 PM.

Similar Threads

  1. writing a program launcher (or Timer)
    By Farzaneh in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-17-2009, 09:00 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. Timer usage in java program
    By sandeeprao.techno in forum New To Java
    Replies: 5
    Last Post: 01-24-2008, 07:16 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
  •