Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Tolls

Thread: Pause in Timer..

  1. #1
    tjt
    tjt is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default Pause in Timer..

    Hello!

    I'm looking for a way to pause a timer :) here is my code
    Java Code:
    import javax.swing.JFrame;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class TimerType extends JFrame {
    	
    		JLabel promptLabel, timerLabel;
    		int counter;
    		JTextField tf;
    		JButton button;
    		Timer timer;
    		
    		public TimerType(){
    			
    			
    			setLayout( new GridLayout(2,2,5,5));
    			
    			promptLabel = new JLabel("Enter Seconds: " , SwingConstants.CENTER);
    			add(promptLabel);
    			
    			tf = new JTextField (5);
    			add(tf);
    			
    			button = new JButton("Start Timing");
    			add(button);
    			
    			timerLabel = new JLabel("Waiting..." , SwingConstants.CENTER);
    			add(timerLabel);
    			
    			event e = new event();
    			button.addActionListener(e);
    			
    		}
    	public class event implements ActionListener {
    
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			int count = (int)(Double.parseDouble(tf.getText()));
    			timerLabel.setText("Time Left: " + count);
    			
    			TimeClass tc = new TimeClass(count);
    			
    			timer = new Timer(1000, tc);
    			timer.start();
    	
    		
    		
    	}
    		
    		
    		
    	}
    	public class TimeClass implements ActionListener{
    		
    		int counter;
    		public TimeClass(int counter){
    			
    			this.counter = counter;
    			
    		}
    		
    		public void actionPerformed(ActionEvent tc){
    			
    			counter--;
    			if (counter >= 1){
    				timerLabel.setText("Time Left: " + counter);
    			}
    			else {
    				
    				timer.stop();
    				timerLabel.setText("Done!");
    				Toolkit.getDefaultToolkit().beep();
    			}
    		}
    	}
    	
    	public static void main (String [] args){
    		
    		
    		TimerType gui = new TimerType();
    		gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		gui.setSize(250,100);
    		
    		gui.setTitle("Timer Program");
    		gui.setVisible(true);
    	}
    
    }

    Thank You very much!

  2. #2
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default Re: Pause in Timer..

    I'm looking for a woman in her 20s, who can cook.

  3. #3
    tjt
    tjt is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default Re: Pause in Timer..

    I'm asking properly.. Answer me properly.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Pause in Timer..

    What methods does the class have that could do what you want? Have you tried any of them?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    tjt
    tjt is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default Re: Pause in Timer..

    Can you give me a clue?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Pause in Timer..

    I'm asking if you have read the API doc for the Timer class?
    Does it have any methods that could help you?
    What have you tried so far?


    EDIT: I've never tried this so I don't know if it can be done or done easily.
    Last edited by Norm; 08-07-2012 at 03:42 PM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    tjt
    tjt is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default Re: Pause in Timer..

    Yes, I have read the Timer API, and I don't think the methods can help me to create a pause..

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Pause in Timer..

    You have nothing in there at all that even tries, though.
    When do you want to pause the timer?
    Once you know that then place some code (any code) in the place you want that to happen because, at the moment, it doesn't look like you've done anything towards solving this problem. Look at it from our point of view, you've posted some code that doesn't even attempt to pause a timer and asked us pause it. There is no indication in that code of an attempt by you to do this...even placeholders (ie comments) would be something.
    Norm likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Pause in Timer..

    It may be that you have to stop the timer and start a new one with the remaining time.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Stopping a Timer from Inside the timer
    By krishnan in forum Java Applets
    Replies: 2
    Last Post: 10-05-2010, 12:15 AM
  2. is there a way to pause java?
    By zhangster in forum New To Java
    Replies: 4
    Last Post: 04-24-2010, 08:38 PM
  3. How to pause
    By ravjot28 in forum Threads and Synchronization
    Replies: 0
    Last Post: 03-31-2010, 07:09 AM
  4. Replies: 0
    Last Post: 04-04-2008, 03:46 PM
  5. Help with Pause
    By trill in forum Java Applets
    Replies: 2
    Last Post: 09-28-2007, 09:50 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
  •