Reputation: 65
i'm trying to make a countdown timer using swingworker, problem is when seconds get to zero, programs sets them back to 60 and deducts one.I did make the program without GUI and it works just perfect, but using swingworker my timer looks like this
1: 0: 2
1: 0: 1
0: 59: 60
which is kinda wrong and should be
1: 0: 1
1: 0: 0
0: 59: 59
Both of my class lie on the same logic.I guess it has something to do with sending the whole Time object to the 'process' but i just can't explain it to myself.
doInBackground() method :
protected Void doInBackground() throws Exception {
Integer hours = Integer.parseInt(hoursField.getText());
Integer minutes = Integer.parseInt(minutesField.getText());
Integer seconds = Integer.parseInt(secondsField.getText());
Time time = new Time(hours, minutes, seconds);
if(minutes < 59 & seconds < 59) {
if(hours >=0 & minutes >=0 & seconds >=0) {
boolean count = true;
while(count) {
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
Logger.getLogger(Chronometer.class.getName()).log(Level.SEVERE, null, e);
}
time.setSeconds(time.getSeconds() - 1);
publish(time);
if(time.getHours() == 0 & time.getMinutes() == 0 & time.getSeconds() == 0) {
count = false;
}
if(time.getSeconds() == 0) {
time.setSeconds(60);
if(time.getMinutes() != 0) {
time.setMinutes((time.getMinutes() - 1));
}else if(time.getMinutes() == 0) {
time.setHours((time.getHours() - 1));
if(time.getHours() >= 0) {
time.setMinutes(59);
}
if(time.getHours() < 0) {
time.setHours(0);
}
}
}
}
}else {
System.exit(0);
}
}
return null;
}
And this is the simple java class that works on the same logic :
boolean count = true;
while(count) {
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
}
seconds--;
System.out.println(hours + ": " + minutes + ": " + seconds);
if(hours == 0 & minutes == 0 & seconds == 0) {
count = false;
}
if(seconds == 0) {
seconds = 60;
if(minutes != 0){
minutes--;
}else if(minutes == 0) {
hours--;
if(hours >=0){
minutes = 59;
}
if(hours < 0) {
hours = 0;
}
}
}
}
If anyone could point me my mistake, i'd be very grateful. Thanks
Adding process method
public void process(List<Time> time) {
for(Time t : time) {
showField.setText(String.valueOf(t.getHours()) + ": " + String.valueOf(t.getMinutes())+ ": " + String.valueOf(t.getSeconds()));
}
}
Upvotes: 1
Views: 456
Reputation: 1900
your line
if(time.getSeconds() == 0) time.setSeconds(60);
needs to be
if(time.getSeconds() == 0) time.setSeconds(59);
And invert this tow lines:
time.setSeconds(time.getSeconds() - 1);
publish(time);
like this:
publish(time);
time.setSeconds(time.getSeconds() - 1);
The deduction is correct but it's not showing the result because when seconds reaches cero the conditional it's executed, not showing the intermediate time.
Change this line:
if(time.getSeconds() == 0)
for this one:
if(time.getSeconds() < 0)
And this:
if(time.getHours() == 0 & time.getMinutes() == 0 & time.getSeconds() == 0)
if(time.getHours() == 0 & time.getMinutes() == 0 & time.getSeconds() < 0)
Upvotes: 1
Reputation: 17622
I did not dig at your logic, but you can achieve the same using Calendar
which looks more simple.
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hours);
c.set(Calendar.MINUTE, minutes);
c.set(Calendar.SECOND, seconds);
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date d = c.getTime();
System.out.println(format.format(d));
while(count) {
c.set(Calendar.SECOND, c.get(Calendar.SECOND)-1); //decrement 1 second at a time
d = c.getTime();
System.out.println(format.format(d));
Thread.sleep(1000); //Pause for a second between each print
}
I have used SimpleDateFormat("HH:mm:ss")
for simplicity. If you need different fields of time, then you can use Calendar.get(..)
method to get the same
Upvotes: 1