Book Image

ExtGWT Rich Internet Application Cookbook

By : Odili Charles Opute , Oded Nissan
Book Image

ExtGWT Rich Internet Application Cookbook

By: Odili Charles Opute , Oded Nissan

Overview of this book

<p>Get ready to build the next generation Gmail, Facebook, or Meebo, with HTML5 and Server Push, taking advantage of the power and versatility of Java with ExtGWT. Sencha Ext GWT takes GWT to the next level, giving you high-performance widgets, feature-rich templates and layouts, advanced charting, data loaders and stores,&nbsp; accessibility, and much more.<br /><br /><i>ExtGWT Rich Internet Application Cookbook will teach you to quickly build&nbsp; stunning functionality into your own apps with ExtGWT</i>.<br /><br />This is a catalog of practical solutions to get your ExtGWT web app up and running in no time, with tips for persistence and best practices. You begin by playing with panels, windows, and tabs, to learn the essentials. Next, you engage yourself with forms, buttons, toolbars and menus to build on further. Dealing with the UI and the trees will follow to help you make stunning user interfaces. Then you will be taught to work with Listview, Views, and Gridpanels, the more complex problems. The book will then deal with charts, visualization, and drag and drop to take you to the next level. Finally, you will wind up with serialization, persistence, and custom theming. Now, you are an expert!</p>
Table of Contents (22 chapters)
ExtGWT Rich Internet Application Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Event Handling — Making Those GUIs Do Something
Jakarta Commons-FileUpload

Event handling 101


In Java (and most other programming environments), event handling is based on the observer design pattern. In this pattern, an object maintains one or more dependent objects and notifies them of any changes. In event handling, listeners or handlers are registered to a UI component and listen for events coming from the component. A listener contains a method which contains the code we want to run when an event happens.

When an event happens to a component, that event is passed on to all the listeners for that component; some of which might then respond to the event. When they have done this, they go back to listening.

Control flow of delegation event model

The source component registers an event listener, which is responsible for handling the event. The event is fired by the component and an event object is passed to the listener.

The following are the steps we need to take to handle events:

  1. 1. Set up the GUI component.

  2. 2. Create a Listener object, for example, a SelectionListener for handling button clicks.

  3. 3. Associate the Listener object with the component we want to respond to, using the addListener() method, or addSelectionListener() which is recommended for a SelectionListener.

That's basically all there is to event handling.

A simple example – button presses

A simple form of event is the pressing of a button; this generates a ButtonEvent, which we will listen for with a SelectionListener. It is relatively easy to write the code to handle a button-press, so let's write one!

Button btn = new Button("Button Text");
SelectionListener<ButtonEvent> listener;
listener = new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent evt) {
String msg = evt.getButton().getText();
Info.display("Message", "Clicked - " + msg);
}
};
btn.addSelectionListener(listener);

Anonymous inner classes

Anonymous inner classes are unnamed inner classes (a class within a class), which help to simplify your code especially in event handling. The previous example can now be re-written as follows:

Button btn = new Button("Button Text",
new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent evt) {
String msg = evt.getButton().getText();
Info.display("Message", "Clicked - " + msg);
}
});

The use of anonymous inner classes in event handlers is quite common. However, for code clarity, these handlers should be very short code snippets.