how to prevent timer from repeats

hello there i've made a countdown timer

and i want it when it finishes counting till 0 not repeating from the beginning but do some action

i uesd the method

timer.setRepeats(false);

but it stops the timer from counting

i'm using javax.swing.timer;

and here is the code:

import java.math.*;

import java.util.*;

import java.text.*;

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

publicclass Timer1extends JFrame{

JComboBox combo =new JComboBox();

JButton start =new JButton("Start");

JButton stop =new JButton("Stop");

JButton clear =new JButton("Clear");

JTextField time =new JTextField("");

javax.swing.Timer timer;

SimpleDateFormat timef =new SimpleDateFormat("HH:mm:ss",Locale.getDefault());

long startT,stopT;

Date date=new Date();

public Timer1(){

super("Timer");

timef.setTimeZone(TimeZone.getTimeZone("GMT"));

addWindowListener(new WindowAdapter(){

publicvoid windowClosing(WindowEvent ev){

dispose();

System.exit(0);

}

}

);

setBounds(10,10,400,200);

getContentPane().setLayout(null);

start.setBounds(10,30,100,24);

start.setBackground(Color.green);

stop.setBounds(10,60,100,24);

stop.setBackground(Color.red);

combo.setBounds(10,90,100,25);

combo.setBackground(Color.blue);

clear.setBounds(150,60,100,24);

clear.setBackground(Color.green);

combo.addItem("1 Hour");

combo.addItem("2 Hours");

combo.addItem("3 Hours ");

combo.addItem("4 Hours");

combo.addItem("5 Hours");

combo.addItem("6 Hours");

combo.addItem("7 Hours");

combo.setSelectedIndex(0);

start.addActionListener(new ActionListener(){

publicvoid actionPerformed( ActionEvent e ){

try{

startT=(combo.getSelectedIndex()+1)*60*60*1000;// change if combo box values changed

}

catch (Throwable fe){

startT=0;

}

startT+=System.currentTimeMillis();

timer.start();

}

}

);

stop.addActionListener(new ActionListener(){

publicvoid actionPerformed( ActionEvent e ){

timer.stop();

}

}

);

clear.addActionListener(new ActionListener(){

publicvoid actionPerformed( ActionEvent e ){

time.setText("");

}

}

);

time.setBounds(150,30,100,24);

time.setOpaque(true);

time.setBackground(Color.pink);

getContentPane().add(stop);

getContentPane().add(start);

getContentPane().add(time);

getContentPane().add(combo);

getContentPane().add(clear);

timer =new javax.swing.Timer(1000,new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

stopT = System.currentTimeMillis();

time.setText(timef.format(new Date(startT-stopT)));

}

}

);

setVisible(true);

}

publicstaticvoid main (String[] args){

UIManager.put("Label.font" ,new Font("Times New Roman",0,23));

UIManager.put("Button.font",new Font("Arial",1,21));

new Timer1();

}

}

[6101 byte] By [First_knighta] at [2007-11-27 8:01:59]
# 1
The ActionListener listening to the timer can always stop() it, not?
Hippolytea at 2007-7-12 19:44:07 > top of Java-index,Java Essentials,New To Java...
# 2

Put your countdown code in the actionPerformed method that your timer calls. You can reset the countdown and such in that method and use the timer only for simply timing things. (In other words, don't think of your timer as doing anything but telling you when a certain time period has passed).

Message was edited by:

hunter9000

hunter9000a at 2007-7-12 19:44:07 > top of Java-index,Java Essentials,New To Java...
# 3

did you mean to do that hunter9000

public void actionPerformed(ActionEvent ee)

{

if (ee.getActionCommand()==("Start"))

{

try {

startT=(combo.getSelectedIndex()+1)*1*60*1000; // change if combo box values changed

startT+=System.currentTimeMillis();

timer.start();

}

catch (Throwable fe) {

startT=0;

}

}

}

because it gives me the same result after the timer finishes it repeats the defult time

First_knighta at 2007-7-12 19:44:07 > top of Java-index,Java Essentials,New To Java...
# 4

That code is for when the start button is pressed right? You'll have some other code in an action performed method since you're using a swing timer. The actionperformed is what gets called when the timer "fires". That's where you'd decrement your counter or whatever and do whatever needs to be done.

public void actionPerformed(ActionEvent ae) {

if (ae came from timer) {

counter--;

if (counter >0) {

// do the thing with the time

}

else {

timer.stop();

}

}

}

That's a very basic sample, I'm still not entirely sure what operation you're doing each time.

hunter9000a at 2007-7-12 19:44:07 > top of Java-index,Java Essentials,New To Java...
# 5

hello hunter i want simplly not repeating the timer after it finishes

but when the timer finishes exit the frame

i've adjust the code as you said

if (ee.getActionCommand()==("Start"))

{

if(timer>0){

try {

startT=(combo.getSelectedIndex()+1)*1*60*1000; // change if combo box values changed

startT+=System.currentTimeMillis();

timer.start();

}

catch (Throwable fe) {

startT=0;

}

}

else {

timer.stop();

}

}

}

but it gave me error:operator>cannot be applied to javax.swing.timer

First_knighta at 2007-7-12 19:44:07 > top of Java-index,Java Essentials,New To Java...
# 6

> but it gave me error:operator>cannot be applied to

> javax.swing.timer

You're comparing a reference to 0, you can't do that. You need some kind of integer counter. Each time the timer "ticks" decrement it. When it hits 0, then do your action. If it's not a 0 yet, don't do anything and just wait until the next tick.

hunter9000a at 2007-7-12 19:44:07 > top of Java-index,Java Essentials,New To Java...
# 7

thanks alot dear hunter9000 but i can't get it well

so i think of another way to do so

that if the("HH:mm:ss")were equal to ("00:00:00")meaning

that if the timer is 00:00:00 exit the frame and not including the action>0

because it is done by default

but i don't know how the if statement would be

First_knighta at 2007-7-12 19:44:07 > top of Java-index,Java Essentials,New To Java...
# 8

> thanks alot dear hunter9000 but i can't get it well

> so i think of another way to do so

> that if the("HH:mm:ss")were equal to

> ("00:00:00")meaning

> that if the timer is 00:00:00 exit the frame and not

> including the action>0

> because it is done by default

> but i don't know how the if statement would be

Ok, so I don't understand what you mean by the timer equalling 00:00:00. A timer doesn't have a "time" associated with it, it simply triggers an event after a certain amount of time has passed. If you start it with a 1 second period, then every 1 second it will tick. You can't tell it to tick at a certain time. So if you want something to happen in 8 seconds, start the timer to tick every 1 second, and count 8 ticks before doing the operation. Or, more simply, start the timer for 8 seconds and just do the operation the first time it ticks.

Why don't you explain a typical use of the timer from the user perspective. It seems like they click a button to start a timer right? Then after a certain number of second, something happens right?

hunter9000a at 2007-7-12 19:44:07 > top of Java-index,Java Essentials,New To Java...
# 9

simply the user chooses a time from the combobox say 1 hour

and then hits the start button so the timer begins to countdown.

my problem here is i want when the countdown timer reaches 0 hour 0 minute 0 second the frame exit (or any action)not repeating the default time .

got it?

First_knighta at 2007-7-12 19:44:07 > top of Java-index,Java Essentials,New To Java...
# 10

> simply the user chooses a time from the combobox say

> 1 hour

> and then hits the start button so the timer begins to

> countdown.

> my problem here is i want when the countdown timer

> reaches 0 hour 0 minute 0 second the frame exit (or

> any action)not repeating the default time .

> got it?

Ah, yes, now I understand. So when the user chooses an interval, 1 hour in this case, you calculate the number of seconds that make up that interval. (Doesn't have to be seconds, depends on the granularity of the choices) Then you count down that many seconds like I described before. Each time the timer ticks, decrement the counter, and also convert the number of second left back into hours, minues, seconds and display that.

So the counter becomes the data model that everything's based on, and you format it for display each tick.

startTimer() {

int interval = // get the interval selected from the combobox

int seconds = // convert interval to seconds

start timer with 1 second period

}

actionPerformed() {

seconds--;

if (seconds == 0) {

stop timer

do whatever we were waiting for

}

else {

convert seconds to HH:MM:SS format and display in gui

}

}

hunter9000a at 2007-7-12 19:44:07 > top of Java-index,Java Essentials,New To Java...
# 11

i did as you said but it gave me two errors:

public void actionPerformed(ActionEvent ee)

{

if (ee.getActionCommand()==("Start"))

{

startTimer() {

int interval = (combo.getSelectedIndex()+1);

int seconds = (combo.getSelectedIndex()+1)*1*60*1000;

}

actionPerformed() {

seconds--;

if (seconds == 0) {

timer.stop();

System.exit(0);

}

else {

//convert seconds to HH:MM:SS format and display in gui

}

}

}

}

}

here startTimer() and here actionPerformed() the error is ';' expected?

and what is the code for this step?

convert seconds to HH:MM:SS format and display in gui

First_knighta at 2007-7-12 19:44:07 > top of Java-index,Java Essentials,New To Java...
# 12
You've got an opening brace after startTimer() instead of a ;If you format the code to use some consistent indentation things like that will be easier to spot.
hunter9000a at 2007-7-12 19:44:07 > top of Java-index,Java Essentials,New To Java...
# 13
> You've got an opening brace after startTimer()> instead of a ;> If you format the code to use some consistent> indentation things like that will be easier to spot.so why it gaves me that stranger error ?and how to fix it?
First_knighta at 2007-7-12 19:44:07 > top of Java-index,Java Essentials,New To Java...
# 14
If the error is ";" expected, add a semicolon!
DrLaszloJamfa at 2007-7-12 19:44:07 > top of Java-index,Java Essentials,New To Java...
# 15

where i add a semicolon here there is no need?

after the opening braces of the startTimer ?the same error

public void actionPerformed(ActionEvent ee)

{

if (ee.getActionCommand()==("Start"))

{

startTimer()

{

int interval = (combo.getSelectedIndex()+1);

int seconds = (combo.getSelectedIndex()+1)*1*60;

}

actionPerformed()

{

seconds--;

if (seconds == 0) {

timer.stop();

System.exit(0);

}

else {

//convert seconds to HH:MM:SS format and display in gui

}

}

}

}

First_knighta at 2007-7-21 22:27:35 > top of Java-index,Java Essentials,New To Java...
# 16

i've adjust it to the following

public void actionPerformed(ActionEvent ee)

{

if (ee.getActionCommand()==("Start"))

{

timer.start();

int seconds = (combo.getSelectedIndex()+1)*1*60; //here the combobox value is in minute and i convert it to second

seconds--;

if (seconds == 0) {

timer.stop();

System.exit(0);

}

else {

//convert seconds to HH:MM:SS format and display in gui

}

}

}

so there is no errors but the timer begins counting down from 5:40:choosen minute

and i want it to start from the enterd time

First_knighta at 2007-7-21 22:27:35 > top of Java-index,Java Essentials,New To Java...
# 17

You need to separate the code that starts the timer (which is triggered by the user pressing the start button) and the code that happens everytime the timer ticks (which is triggered by the swing timer). Also, don't compare Strings with ==, use the equals method.

public void actionPerformed(ActionEvent ee) {

if (ee.getActionCommand()==("Start")) {// caused by start button

timer.start();

int seconds = (combo.getSelectedIndex()+1)*1*60; //here the combobox value is in minute and i convert it to second

}

else { // not caused by start button

seconds--;

if (seconds == 0) {

timer.stop();

System.exit(0);

} else {

//convert seconds to HH:MM:SS format and display in gui

}

}

}

hunter9000a at 2007-7-21 22:27:35 > top of Java-index,Java Essentials,New To Java...
# 18

when i separated the code it doesn't recognize the seconds variable here

else { // not caused by start button

seconds--;

if (seconds == 0) {

timer.stop();

System.exit(0);

} else {

//convert seconds to HH:MM:SS format and display in gui

}

}

First_knighta at 2007-7-21 22:27:35 > top of Java-index,Java Essentials,New To Java...
# 19

so i made it as follows:

else { // not caused by start button

int seconds = (combo.getSelectedIndex()+1)*1*60;

seconds--;

if (seconds == 0) {

timer.stop();

System.exit(0);

} else {

//convert seconds to HH:MM:SS format and display in gui

}

}

but it is still also chooses random valuse for the hour and the minutes?

}

First_knighta at 2007-7-21 22:27:35 > top of Java-index,Java Essentials,New To Java...
# 20

> so i made it as follows:

> else { // not caused by start button

> int seconds =

> (combo.getSelectedIndex()+1)*1*60;

>seconds--;

> if (seconds == 0) {

>timer.stop();

> System.exit(0);

>} else {

> //convert seconds to HH:MM:SS format and

> display in gui

>

> }

> s still also chooses random valuse for the hour and

> the minutes?

>}

1) you're resetting the seconds value each time the timer ticks, make that a class member.

2) You're calculating seconds based on the index that's selected, not the value that index represents in the combo box. i.e, if "20 Minutes" is at the first index in the combo, you'll calculate:

int seconds = (0+1)*1*60;

which equals 60, not 1200 like it should be. You need a way to map a combobox index to a number of minutes.

hunter9000a at 2007-7-21 22:27:35 > top of Java-index,Java Essentials,New To Java...