Thursday, April 2, 2009

GWT: using Timer as in Tread sleep()

You can use the timer to put the application to sleep, or in this case to change content every 3 seconds.



    private String currentImage = "1.png";

    private int refreshIntervalMilliseconds = 3000;

    public void onModuleLoad()
    {
params = CommonUtils.parseParamString();

Timer t = new Timer()
{
    public void run()
    {
if (currentImage.equals("1.png"))
    currentImage = "2.png";
else if (currentImage.equals("2.png"))
    currentImage = "3.png";
else if (currentImage.equals("3.png"))
    currentImage = "4.png";
else if (currentImage.equals("4.png"))
    currentImage = "1.png";
RootPanel.get("content").clear();
RootPanel.get("content").add(new Label("Current time: " + new Date()));
RootPanel.get("content").add(new Image("images/" + currentImage));
System.out.println("Putting system to sleep for " + refreshIntervalMilliseconds / 1000 + " seconds. currentImage = " + currentImage);
    }
};
t.scheduleRepeating(refreshIntervalMilliseconds);
    }