Results 1 to 5 of 5
  1. #1
    BiteMuncher's Avatar
    BiteMuncher is offline Member
    Join Date
    Feb 2011
    Location
    South Africa
    Posts
    51
    Rep Power
    0

    Default Timer double firing

    Hi all.
    I've a question on javax.swing.Timer
    Sorry I don't have the code with me. Go's something like this.import
    Java Code:
    Import javax.swing.Timer;
    import java.awt.event.*;
    public class TimerTest implements ActionListener
    {
    Timer tmr; 
    
    public TimerTest() 
    {
    tmr = new Timer(1000, this);
    tmr.addActionListener(this);
    tmr.start();
    }
    
    public void actionPerformed(ActionEvent e)
    {
    System.out.println("Timer fired");
    }
    }
    The test output prints twice every time tmr fires. I've been using swing timers with a boolean switch so my code executes every second time it fires for my code to work. Any ideas?
    Last edited by BiteMuncher; 11-17-2013 at 02:34 PM.
    Sorry, I only speak machine language. Yes or a No?:confused:

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Timer double firing

    Please make a small simple program that compiles, executes and shows the problem.

    Be sure to wrap the code in code tags.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,650
    Rep Power
    5

    Default Re: Timer double firing

    The second argument to the Timer constructor is the actionListener. Since you are adding it twice the event is processed twice.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    BiteMuncher's Avatar
    BiteMuncher is offline Member
    Join Date
    Feb 2011
    Location
    South Africa
    Posts
    51
    Rep Power
    0

    Default Re: Timer double firing

    Quote Originally Posted by jim829 View Post
    The second argument to the Timer constructor is the actionListener. Since you are adding it twice the event is processed twice.

    Regards,
    Jim
    Thanks Jim. I missunderstood the keyword 'this'. Last question. Say TimerTest was declared ;
    Java Code:
    class TimerTest extends JFrame,      
            implements ActionListener
    Would 'this' refer to both a JFrame
    & ActionListener?
    Sorry, I only speak machine language. Yes or a No?:confused:

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,650
    Rep Power
    5

    Default Re: Timer double firing

    Yes, as well as TimerTest. But remove the comma after JFrame.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Need help on creating a bullet/firing system?
    By JoshNerd557 in forum New To Java
    Replies: 2
    Last Post: 01-14-2013, 04:01 PM
  2. Key Event Not Firing For JTable
    By Mubashir in forum AWT / Swing
    Replies: 1
    Last Post: 07-13-2012, 05:11 AM
  3. Replies: 2
    Last Post: 08-29-2011, 12:47 PM
  4. nodeChanged() not firing/working?
    By PrinceSendai in forum AWT / Swing
    Replies: 2
    Last Post: 03-24-2011, 03:29 AM
  5. setText is not firing...! Why?
    By N00Bie in forum New To Java
    Replies: 14
    Last Post: 02-20-2011, 09:59 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
  •