Home > Google Web Toolkit > GWT 2.0 Declarative Interfaces Event Handler Binding

GWT 2.0 Declarative Interfaces Event Handler Binding

November 4th, 2009 admin Leave a comment Go to comments

Moving on from the previous tutorial on GWT 2.0 Declarative Interfaces version 2 of GWT also allows dynamic method binding.

This means that simply by adding a few Java annotations we can bind methods to interface objects. In the past we would have to add a new ClickHandler to our code and set the click handler in the widget. Now we can easily bind method.

Using the code we worked on last time within the WebApplication class we’re going to add a method binding to our sendButton go ahead and add the following method:

void onSendButton(ClickEvent event) 
{
    Window.alert('You clicked the sendButton');
}

The intention of this method is to display an typical Javascript alert to the web page visitor saying ‘You clicked the sendButton’. (Basic I know)

Next we can add our binding annotations before the method declaration add the following annotation:

@UiHandler("sendButton")

So the complete method looks like this:

@UiHandler("sendButton")
void onSendButton(ClickEvent event) 
{
    Window.alert('You clicked the sendButton');
}

And thats it! Now we have bound the onSendButton method to our sendButton. If you test the application you should be able to click the button and see the alert

That are a variety of method bindings possible such as KeyHandlers, for more information please see the UiBinding Wiki

  1. No comments yet.
  1. No trackbacks yet.