4

I'm trying to have a Java Timer in my EntryPoint:

Timer timer = new Timer();
timer.schedule(
   new TimerTask() {
       public void run() {
           //some code
       }
   }
   , 5000);

But when trying to compile this I got: No source code is available for type java.util.Timer; did you forget to inherit a required module?

What can I do to fix this error?

javanna
  • 59,145
  • 14
  • 144
  • 125
Igor Kostenko
  • 2,754
  • 7
  • 30
  • 57
  • 1
    possible duplicate of [GWT - did you forget to inherit a required module?](http://stackoverflow.com/questions/5575909/gwt-did-you-forget-to-inherit-a-required-module) – dunni Mar 21 '13 at 21:01
  • Not really a duplicate of http://stackoverflow.com/questions/5575909/gwt-did-you-forget-to-inherit-a-required-module because its in the context of Libgdx. Though that link is definitely useful to explain where the error is originating. – P.T. Mar 22 '13 at 00:12

2 Answers2

14

In GWT you are restricted to use all the Util package classes.

Here is the List of Classes only you can use from util class.

You can use GWT Timer class.

Example(from docs);

 public void onClick(ClickEvent event) {
    // Create a new timer that calls Window.alert().
    Timer t = new Timer() {   //import (com.google.gwt.user.client.Timer)
      @Override
      public void run() {
        Window.alert("Nifty, eh?");
      }
    };

    // Schedule the timer to run once in 5 seconds.
    t.schedule(5000);
  }
Henrik Aasted Sørensen
  • 6,966
  • 11
  • 51
  • 60
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
5

If you're using Libgdx, you can use the libgdx Timer infrastructure to schedule work to run in the future:

Timer t = new Timer();
t.scheduleTask(new Timer.Task() { 
      public void run() { /* some code */ } 
  }), /* Note that libgdx uses float seconds, not integer milliseconds: */ 5);

This way you can schedule the timer in your platform independent code. (The GWT-specific solution will only work in the platform dependent part of your project.)

P.T.
  • 24,557
  • 7
  • 64
  • 95