Results 1 to 13 of 13
  1. #1
    jackkyip is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default Please help!!!! how to kill Timer listener?

    Does anyone know how to kill following Timer listener action? i have tried very hard, but still duno how to kill it..:(

    Thanks

    --------------------------------------------------------------------------------

    int timeRemaining =10;
    int timing=60000;
    Timer countdownTimer = new Timer(timing, new CountdownTimerListener()); // Initial game time


    public void CountDownTimer(int timing, int timeRemaining) {
    System.out.println("countdown time");

    if ( countdownTimer.isRunning())
    {
    System.out.println("is running - inside ");
    countdownTimer.stop();
    }
    else
    {
    System.out.println("no running - inside ");
    countdownTimer.start();
    }

    }

    class CountdownTimerListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e) {
    --timeRemaining;
    if (timeRemaining ==5)
    {
    String timervalue=String.valueOf(timeRemaining);
    System.out.println(timervalue);
    timeRemaining=10;
    }
    else if (timeRemaining >5) {
    String timervalue=String.valueOf(timeRemaining);
    System.out.println("timeRemaining"+timeRemaining);
    System.out.println("show only");
    }
    else if (timeRemaining==11) {
    System.out.println("stop only");
    countdownTimer.stop();
    }
    else {
    System.out.println("timeRemaining"+timeRemaining);
    timeRemaining=10;
    countdownTimer.stop();
    } }
    }

    ???

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    jackkyip is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    since no ppl can answer there, so i post it here..sorry for any inconvenice cause

  4. #4
    JvlShz is offline Member
    Join Date
    Jun 2010
    Posts
    26
    Rep Power
    0

    Default

    use the .kill() method

  5. #5
    jackkyip is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    i used java.1.5 and javax.swing.Timer dun have .kill() method
    only have removeActionListener(ActionListener listener)

    Thanks

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    1) Agree with Phil: please notify us if you cross-post the same question. No one likes risking duplication of effort when the question may have already been answered elsewhere.
    2) What do you mean by "kill" and what is the problem with your current code? Errors? Non-functioning? Other?

    Perhaps you need to get the reference to your Timer object before calling stop from ActionEvent's getSource() method? But I honestly don't know given info as provided.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    Have you looked at the Timer class to see if there is a method to do what you want?
    How about the removeActionListener() method?

  8. #8
    jackkyip is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    Fubarable :
    1. noted! sorry abt that. wont do this next time.

    2. Well! i hope i can remove the timer listener when calling another class object, and another class inside have the timer listener as well.
    Currently when i calling anther class, its trigger another Timer listener, i hope i can remove/kill the first Timer b4 calling the second class object.

  9. #9
    jackkyip is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    Norm:
    Yes. i have looked the method inside JAVA API, and found the removeActionListener() method. i have hard, but still idea how to apply this removeaction method in my code.
    --> ?????
    public void removeActionListener(ActionListener listener)

    {
    CountdownTimerListener .remove(ActionListener.class, listener);
    } <---? am i code it correctly?

    Thanks alot

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    how to apply this removeaction method in my code.
    You need a reference to the Timer object and a reference to the listener.

    timerRef.removeActionListener(refToListener);

  11. #11
    jackkyip is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    Hi Norm:

    this is my timer object ->
    Timer countdownTimer = new Timer(timing, new CountdownTimerListener()); // Initial game time

    so i shd code as
    countdownTimer.removeActionListener(countdownTimer );
    ??

    Thanks alot!

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Why would you want to remove the ActionListener? I don't see what purpose that would serve. Wouldn't stopping the Timer simply be enough?

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    so i shd code as
    countdownTimer.removeActionListener(countdownTimer );
    Read the API doc for the removeActionListener() method.
    What type of argument does it take?
    You're passing it a Timer object, not what it requires.

Similar Threads

  1. kill a thread
    By Peter in forum Advanced Java
    Replies: 6
    Last Post: 06-22-2010, 08:08 AM
  2. Kill sessions
    By Subscribe.name in forum Java Servlet
    Replies: 1
    Last Post: 10-05-2009, 10:22 PM
  3. How I launch an external program and kill the father?
    By Dr_Misterio in forum New To Java
    Replies: 2
    Last Post: 08-05-2009, 03:31 PM
  4. Identify and kill forked jvm (ant java task)
    By stockbroker in forum Advanced Java
    Replies: 1
    Last Post: 04-09-2009, 06:27 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •