-
Pause in Timer..
Hello!
I'm looking for a way to pause a timer :) here is my code
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!
-
Re: Pause in Timer..
I'm looking for a woman in her 20s, who can cook.
-
Re: Pause in Timer..
I'm asking properly.. Answer me properly.
-
Re: Pause in Timer..
What methods does the class have that could do what you want? Have you tried any of them?
-
Re: Pause in Timer..
-
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.
-
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..
-
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.
-
Re: Pause in Timer..
It may be that you have to stop the timer and start a new one with the remaining time.