Results 1 to 7 of 7
  1. #1
    Doctor Cactus is offline Member
    Join Date
    Oct 2008
    Posts
    63
    Rep Power
    0

    Default [SOLVED] Timer help

    Hi, I need help with the Timer Task. I have posted code below. Basically, when i click the menu, various instructions are executed at certain intervals. So, for example, instructions would execute after 5 seconds, then 10 seconds later etc.
    Note: When I click the menu, instructions should execute after 5 seconds, then 15 seconds as in the example above.

    Help is greatly appreciated.

    //----------------------------------------------------------------------
    import java.util.*;

    int numberOfMillisecondsInTheFuture1 = 5000; // 5 sec
    Date timeToRun1 = new Date(System.currentTimeMillis()+numberOfMillisecon dsInTheFuture1);

    int numberOfMillisecondsInTheFuture2 = 15000; // 15 sec
    Date timeToRun2 = new Date(System.currentTimeMillis()+numberOfMillisecon dsInTheFuture2);

    Timer timer = new Timer();


    menu1.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent event)
    {

    timer.schedule(new TimerTask()
    {
    public void run()
    {
    System.out.println("Hello");
    }
    }, timeToRun1);


    timer.schedule(new TimerTask()
    {
    public void run()
    {
    System.out.println("Hello2");
    }
    }, timeToRun2);

    }

    });

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    I'm sorry, but, okay, your question/problem is?

  3. #3
    Doctor Cactus is offline Member
    Join Date
    Oct 2008
    Posts
    63
    Rep Power
    0

    Default

    it doesn't do what I want it to do as I said in the last post. Any ideas?

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by Doctor Cactus View Post
    it doesn't do what I want it to do as I said in the last post. Any ideas?
    No you didn't. You simply said what it needed to do, you said nothing about what it does (or does not) do.

    Do get compiler errors/messages? Do you get exceptions?

    The way you have posted what you've posted you should be getting a couple of compiler messages.

  5. #5
    Doctor Cactus is offline Member
    Join Date
    Oct 2008
    Posts
    63
    Rep Power
    0

    Default

    No, I don't get any compiler messages/errors. What happens is it doesn't wait the number of seconds I want it to wait. So, what happens is the first timer executes and then immediately after, the second one executes.

    Note that it works when used with public static void main(String [] args), but not with the menu.

  6. #6
    Doctor Cactus is offline Member
    Join Date
    Oct 2008
    Posts
    63
    Rep Power
    0

    Default

    I tried something else and it works now. :cool:

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by Doctor Cactus View Post
    No, I don't get any compiler messages/errors.
    with that exact code?

    Java Code:
       void whateverMethod() {
        Timer timer = new Timer();
    
    
        menu1.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent event)
          {
            timer.schedule(new TimerTask()
            {
              public void run()
              {
                System.out.println("Hello");
              }
            }, timeToRun1);
    
    
            timer.schedule(new TimerTask()
            {
              public void run()
              {
                System.out.println("Hello2");
              }
            }, timeToRun2);
    
          }
    
        });
      }
    should complain that timer is not final.

    It would really help if you posted code using code tags, and made sure that the code was compilable while still displaying the problem.

    What happens is it doesn't wait the number of seconds I want it to wait. So, what happens is the first timer executes and then immediately after, the second one executes.

    Note that it works when used with public static void main(String [] args), but not with the menu.

Similar Threads

  1. Best way to implement a timer
    By Nuluvius in forum New To Java
    Replies: 11
    Last Post: 01-16-2009, 01:27 AM
  2. EJB Timer
    By mrjunsy in forum Advanced Java
    Replies: 0
    Last Post: 08-22-2008, 04:09 PM
  3. EJB Timer
    By mrjunsy in forum New To Java
    Replies: 0
    Last Post: 08-04-2008, 06:47 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:46 PM
  5. problem with timer
    By Marcus in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 05:13 AM

Posting Permissions

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