Saturday, May 20, 2006

News: Google Web Toolkit

At Java One one of the big announcements was a Google Web Toolkit (GWT). The toolkit allows a programmer to write Java code and compile that code to JavaScript. This allows the programmer to leverage the abilities of their IDE, unit test their client side code, and easily create RPC services.

Here is a sample Java class which adds some elements to the page when the page is loaded.


package org.hanson.gwt.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
public class MyApplication implements EntryPoint
{
// executed on load
public void onModuleLoad ()
{
final Button button = new Button("Click me");
final Label label = new Label();

button.addClickListener(new ClickListener()
{
public void onClick (Widget sender)
{
if (label.getText().equals("")) {
label.setText("Hello World!");
}
else {
label.setText("");
}
}
});

// add the objects to HTMl elements in the web page
RootPanel.get("div1").add(button);
RootPanel.get("div2").add(label);
}
}

This code creates a button with the text "Click me", and a blank label, and adds them to the HTML page. It then adds an event handler to the button to detect when the button is clicked. The event will trigger the text of the label to change to say "Hello World". This is a trivial example, but it shows how you can create objects an behaviours on the HTML page without directly writing any HTML of JavaScript code.

The question is, how big is this. I think it is BIG with a capital B. As long as Google maintains this tool, making sure that the code works across all browsers, this tool will save tremendous amouts of development time.

technorati tags:

1 comment:

Robert Hanson said...

I posted a simple sample. I hope that helps.

http://roberthanson.blogspot.com/2006/06/trivial-gwt-example.html