Results 1 to 2 of 2

Thread: java.util.Timer

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Question java.util.Timer

    Any ideas on how to use the Timer class? As far as i understand it you are supposed to create a class that subclasses the abstract TimerTask class to implement the run() method and pass an instance of that class to the Timer constructor plus two long values denoting the delay time and the period. For some reason the Timer constructor dosent want to take this Any ideas why?

    Also there are no methods within the Timer class that return the number of milliseconds that have elapsed. So how would i get this value? I thought about using the System.current.TimeMillis() but wouldn't that just give me the time in milliseconds that have passed from midnight GMT, Janurary 1, 1970?
    Code:
    class TimeTracker extends TimerTask{
       private Timer timer;  
       private TimeTrackerTest tt; 
       private Long l;
       private long millis;
       Object[] o;  
        
       public TimeTracker(TimeTrackerTest tt){
          this.tt = tt;  
          timer = new Timer(this,0l,1000l); 
       }
      public void run(){
       try{
         millis = scheduledExecutionTime();
         
         l = new Long(millis); 
         o[0] = l;   
      
     
         tt.jtf.setText(MessageFormat.format("{0}",o));
         }catch(Exception e){System.err.println(e);}
       }    
      }

  2. #2
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    Why don't you just create an actionlistener and then create a timer object of the actionlistener?

    Code:
        class my_class implements ActionListener
        {
          public void actionPerformed(ActionEvent event)
          {
             System.out.println("Hello");
          }
        }
    
        ActionListener timerlistener = new my_class();
        Timer timer = new Timer(50,timerlistener);

Posting Permissions

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



Click Here to Expand Forum to Full Width