Results 1 to 4 of 4

Thread: Timer

  1. #1
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default Timer

    Can't get this to work, not sure why -- any help is appreciated

    Java Code:
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    public class MyCounter extends JFrame {
    
    	JLabel label;
    	JTextField textfield;
    	JButton button;
    	int timer = 0;
    
    	public MyCounter() {
    
    		super("MyCounter");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setLayout(new FlowLayout());
    		setSize(250, 250);
    
    		label = new JLabel("0 minutes");
    		add(label);
    
    		textfield = new JTextField(15);
    		add(textfield);
    
    		button = new JButton("Start timer");
    		add(button);
    
    		button.addActionListener(new ActionHandler());
    
    	}
    
    	public class ActionHandler implements ActionListener {
    
    		public void actionPerformed(ActionEvent e) {
    
    			timer = Integer.parseInt(textfield.getText());
    
    			while(timer > 0) {
    
    				try {
    
    					Thread.sleep(60000);
    
    				} 
    
    				catch (Exception exception) {
    
    				}
    				
    				timer--;
    				label.setText(Integer.toString(timer));			
    
    			}
    
    		}
    
    	}
    
    }

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

    Default

    You're calling Thread.sleep on the main Swing thread known as the event dispatch thread or EDT, and this should never be done as this will do nothing but put your entire application to sleep. Your solution is to read the Swing tutorials on how to use a Swing Timer. Also there are plenty of posted code samples in this forum that uses Swing Timers.

    Luck.

  3. #3
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    I don't know what that means; I'm new to Java. I'll read the tutorial, thanks :)

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

Similar Threads

  1. Need help with timer
    By firecat318 in forum New To Java
    Replies: 6
    Last Post: 05-31-2010, 03:38 AM
  2. Timer help
    By Kinyo in forum New To Java
    Replies: 15
    Last Post: 03-15-2009, 03:37 AM
  3. EJB Timer
    By mrjunsy in forum Advanced Java
    Replies: 0
    Last Post: 08-22-2008, 05:09 PM
  4. EJB Timer
    By mrjunsy in forum New To Java
    Replies: 0
    Last Post: 08-04-2008, 07:47 PM
  5. Replies: 0
    Last Post: 04-04-2008, 03:46 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
  •