Ok, so I have my code for the timer

computer = new Timer (4000, this);      
computer.start ();

The timer starts at the end of a method, and is supposed to start another method via actionlistener after 4 seconds, the problem is, after 4 seconds, the timer gives a null to actionlistener.I figured this out by adding a println in actionlistener


This is my code in actionlistener, and many more

if (event.equals ("computer"))
        {
            System.out.println ("Timer has finished");
            ptarget ();
        }

but i get an error of null pointer becouse the event is null
what am i doing wrong?

Ok, so I have my code for the timer

computer = new Timer (4000, this);      
computer.start ();

The timer starts at the end of a method, and is supposed to start another method via actionlistener after 4 seconds, the problem is, after 4 seconds, the timer gives a null to actionlistener.I figured this out by adding a println in actionlistener


This is my code in actionlistener, and many more

if (event.equals ("computer"))
        {
            System.out.println ("Timer has finished");
            ptarget ();
        }

but i get an error of null pointer becouse the event is null
what am i doing wrong?

why not just give the timer its own separate listener? similar to this:

int delay = 1000; //milliseconds
  ActionListener taskPerformer = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
          //...Perform a task...
      }
  };
  new Timer(delay, taskPerformer).start();

why not just give the timer its own separate listener?

ok, so how would i do that?

do i make another listener?, i thought u could only have one listener per program

why not just give the timer its own separate listener? similar to this:

int delay = 1000; //milliseconds
  ActionListener taskPerformer = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
          //...Perform a task...
      }
  };
  new Timer(delay, taskPerformer).start();

thanks alot works great, only problem is, it does the timer over and over again, how do i make it repeat only once?

ok, so how would i do that?

do i make another listener?, i thought u could only have one listener per program

but you can have many anonymous inner class listener though... and you create it like i showed in my example above:

ActionListener taskPerformer = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
          //...Perform a task...
      }
  };

[EDIT]Im not sure. I think you would call timer.stop() in the action listener action performed method? or directly after timer.start() and if that fails try: timer.cancel();

but you can have many anonymous inner class listener though... and you create it like i showed in my example above:

ActionListener taskPerformer = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
          //...Perform a task...
      }
  };

[EDIT]Im not sure. I think you would call timer.stop() in the action listener action performed method? or directly after timer.start() and if that fails try: timer.cancel();

how can i give my timer a name, becouse to make it go once you have to use setRepeats(false);

I tried Timer.setRepeats(false); but doesant work i get errors

how can i give my timer a name, becouse to make it go once you have to use setRepeats(false);

I tried Timer.setRepeats(false); but doesant work i get errors

what errors do you get? and to give it a name:

Timer timer=new Timer("name");

what errors do you get? and to give it a name:

Timer timer=new Timer("name");

thanks alot, heres working code

Timer computer;
 
 ActionListener taskPerformer = new ActionListener ()
            {
                public void actionPerformed (ActionEvent evt)
                {
                    computer.stop ();
                    System.out.println ("Timer finished");
                    ctarget ();
                }
            }
            ;
            computer = new Timer (7000, taskPerformer);
            computer.start ();

thanks alot, heres working code

Timer computer;
 
 ActionListener taskPerformer = new ActionListener ()
            {
                public void actionPerformed (ActionEvent evt)
                {
                    computer.stop ();
                    System.out.println ("Timer finished");
                    ctarget ();
                }
            }
            ;
            computer = new Timer (7000, taskPerformer);
            computer.start ();

Great to hear and see it :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.