Need help with TIMER reset code
Hey guys I need help with this timer I'm doing. I have it working counting down from 35:00mins to 00:00, now I want it to stay that way until I click reset, Then it should display 35:00 mins on the clock again and when I click start again it should just start counting down until it reaches 00:00.
I'm not sure on how I have to do this at all. I did make a reset button with code like this.
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt)
{
jLabel1.setText("35:00");
}
all it does is make sets the display on the clock to 35:00 but when I click start again it starts counting down in 2's and 3's. Not sure how to fix this.
This is the code of my timer with start button
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
for(int ss=0; ss<1; ss++) {
// Change the set name
// Setup the counter to begin the countdown
counter = *60;
timer = new Timer(1000, startCycle());
timer.start();
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Countdown().setVisible(true);
}
});
}
private Action startCycle() {
return new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
new ClockTask(null).execute();
}
};
}
class ClockTask extends SwingWorker<Void, String> {
private Callback callback;
private Object jLblTimer;
public ClockTask(Callback callback) {
this.callback = callback;
}
@Override
protected Void doInBackground() throws Exception {
if (counter >= 0) {
int millis = counter*1000;
String time = String.format("%02d:%02d", TimeUnit.MILLISECONDS.toMinutes(millis),
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.t oMinutes(millis))
);
publish(time); // call publish which will call process() method in EDT
counter--;
} else {
// this.callback.call();
this.callback.notify();
timer.stop();
}
return(null);
}
@Override
protected void process(List<String> times) {
String lastTime = times.get(times.size()-1);
jLabel1.setText(lastTime); // just update ui with lastTimer, ohters are ignorable
}
@Override
protected void done() {
super.done();
} // TODO add your handling code here:[/COLOR]
}
}
Re: Need help with TIMER reset code
That's a lot of difficult to read unformatted code. Please have a look at the forum FAQ including how to use [code] [/code] tags, and then edit this post.
Re: Need help with TIMER reset code
Why did you highlight a bunch of it in bright green? On a white background that's pretty much impossible to read.