Table of contents
1.
Introduction
2.
Event Handling in GWT
3.
Event Handler Interfaces in GWT
3.1.
Event Methods
4.
Frequently Asked Questions
4.1.
What is GWT?
4.2.
What is an Event?
4.3.
What is Event Handler in GWT?
4.4.
What is GWT UI Binder?
4.5.
What is GWT Servlet?
5.
Conclusion
Last Updated: Mar 27, 2024

GWT Event Handling

Author Rajat Agrawal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The Google Web Toolkit (GWT) is a development toolkit for creating and improving sophisticated browser-based applications. For typical web-app activities like bookmarking, UI abstraction, cross-browser compatibility, etc., GWT focuses on reusable techniques. Google AdWords and Orkut are just some of the products that use GWT. A large number of developers use GWT, which is open source and totally free. 

In this blog, we will deep dive into the concepts of Event Handling in GWT.

Event Handling in GWT

Event: A change in the state of any object is referred to as an Event. As an illustration, consider pressing a button, typing text into a text box, clicking or dragging the mouse, etc.

Event Handler: A handler interface specifies one or more methods the widget can use to call an event. When a class wants to listen to a group of events of a certain type, it implements the handler interface and then passes a reference to that class to the widget.

GWT has an event handler mechanism similar to Java AWT or SWING User Interface frameworks. When a widget wants to announce an event, it calls one or more defined methods defined by a listener interface. GWT offers a variety of interfaces that correspond to numerous possible events. When a class wants to listen to a group of events of a certain type, it implements the handler interface and then sends a reference to that class to the widget.

For example, the Button class emits click events, therefore to handle click events, you will need to create a class that implements ClickHandler.

Event Handler Interfaces in GWT

Each of the GWT event handlers is an extension of the EventHandler interface and contains a single method with a single parameter. This parameter is consistently a connected event-type object. Various methods are available for each event object to alter the passed event object. 

For example, to handle the click event, you must code your handler as follows:-

public class OnlyClickHandler implements ClickHandler {
  @Override
  public void onClick(ClickEvent event) {
      Window.alert("Coding Ninjas!");
  }
}
You can also try this code with Online Java Compiler
Run Code


Now, to register an event handler for click events, any class wishing to receive click events must call addClickHandler().

Button button = new Button("Send Button!");
button.addClickHandler(new OnlyClickHandler());
You can also try this code with Online Java Compiler
Run Code


The list of significant GWT event handlers and relevant events and handler registration methods is given below.

Event Methods

Each handler has a single method that takes a single parameter and stores the event object, such as void onClick(ClickEvent event) or void onKeyDown(KeyDownEvent event). The following list includes a few typical methods that are available for event objects like ClickEvent and KeyDownEvent:-

1.) protected void dispatch(ClickHandler handler): HandlerManager should be the only one to invoke this function.

2.) public java.lang.Object getSource(): This method returns the source from which this event was last triggered.

3.) DomEvent.Type <FooHandler> getAssociatedType(): The type that was used to register the Foo event is returned by this method.

4.) static DomEvent.Type<FooHandler> getType(): This method retrieves the event type related to Foo events.

5.) protected final boolean isLive(): Whether the event is live is returned by this method.

6.) protected void kill(): This method terminates the event. 

Frequently Asked Questions

What is GWT?

The Google Web Toolkit (GWT) is a development toolkit for creating and improving sophisticated browser-based applications. For typical web-app activities like bookmarking, UI abstraction, cross-browser compatibility, etc., GWT focuses on reusable techniques.

What is an Event?

A change in the state of any object is referred to as an Event. As an illustration, consider pressing a button, typing text into a text box, clicking or dragging the mouse, etc.

What is Event Handler in GWT?

A handler interface specifies one or more methods the widget can use to call an event. When a class wants to listen to a group of events of a certain type, it implements the handler interface and then passes a reference to that class to the widget.

What is GWT UI Binder?

GWT UI Binder is a framework that enables users to create GWT applications as HTML pages. The GWT UI Binder is used to declarative design the user interface of a web application built with GWT, i.e., to isolate the programming logic from the UI.

What is GWT Servlet?

GWT supports several methods for communicating with a server through HTTP. You may use the GWT RPC framework to perform transparent calls to Java servlets while GWT handles low-level complexities like object serialization.

Conclusion

In this article, we have extensively discussed Event Handling in Google Web Toolkits(GWT), different event handlers provided by GWT, and how different event handlers work. If you want to learn more, check out our articles on GWT UI BinderGWT DeckPanel WidgetGWT TabPanel Widget, and GWT Composite Widget.

Do upvote our blog to help other ninjas grow.

Happy Coding!

Live masterclass