Results 1 to 3 of 3
Thread: Need help with TIMER reset code
- 06-12-2012, 07:39 PM #1
Member
- Join Date
- Jun 2012
- Posts
- 7
- Rep Power
- 0
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]
}
}Last edited by elbert.dewet@yahoo.com; 06-13-2012 at 12:57 PM.
-
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.
- 06-12-2012, 11:58 PM #3
Similar Threads
-
Urgently need a countdown timer code...
By BZwap in forum New To JavaReplies: 5Last Post: 05-08-2011, 06:51 AM -
Timer java code does not work
By mariapatrawala in forum New To JavaReplies: 10Last Post: 12-12-2010, 10:34 AM -
Stopping a Timer from Inside the timer
By krishnan in forum Java AppletsReplies: 2Last Post: 10-04-2010, 11:15 PM -
Quit Button, Load new Frame, Timer Code
By IHateNetbeans in forum New To JavaReplies: 1Last Post: 03-18-2009, 03:58 PM -
How to cancel an individual timer in spite of canceling whole timer
By Java Tip in forum java.utilReplies: 0Last Post: 04-04-2008, 02:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks