Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cancelling an actionListener...?

Status
Not open for further replies.

greenonions79

Technical User
Jan 4, 2003
1
GB
Hello all,

I'm kind of new to Java and having problem using actionListeners...

In the application I'm develping when a user clicks on the startButton, it fires an actionListener which which displays a countdown on screen and updates it every second. My problem is that if the user clicks on the startButton for a second time it fires another actionlistener - so that there are two countdowns displayed on screen. I would prefer it if the first countdown could be cancelled and only the new one displayed. Is there a way to do this?

This is an extract from my code

Code:
if (source == game.startButton){
     Date startTime = new Date();
     ActionListener timeListen = new TimeLogic(startTime);
     Timer stopWatch = new Timer(1000, timeListen);
Thanks a lot
 
Code:
if (source == game.startButton){
   if not Active then {  // Active is a boolean declared
                         // at the class level
      Active = true;
      Date startTime = new Date();
      ActionListener timeListen = new TimeLogic(startTime);
      Timer stopWatch = new Timer(1000, timeListen);
   } else {
      // 1) Stop the current countdown here
      // 2) restart the countdown
      // I suggest putting the code which is identical
      // for both branches of the if statement in a 
      // separate procedure.
   }

So in short every time the startButton is pressed you check to see if a count down is running, if it is cancel it and start again. Easy. ----------------------------------------
There are no onions, only magic
----------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top