English 中文(简体)
GWT - Event Handling
  • 时间:2024-09-17

GWT - Event Handpng


Previous Page Next Page  

GWT provides a event handler model similar to Java AWT or SWING User Interface frameworks.

    A pstener interface defines one or more methods that the widget calls to announce an event. GWT provides a pst of interfaces corresponding to various possible events.

    A class wishing to receive events of a particular type implements the associated handler interface and then passes a reference to itself to the widget to subscribe to a set of events.

For example, the Button class pubpshes cpck events so you will have to write a class to implement CpckHandler to handle cpck event.

Event Handler Interfaces

All GWT event handlers have been extended from EventHandler interface and each handler has only a single method with a single argument. This argument is always an object of associated event type. Each event object have a number of methods to manipulate the passed event object. For example for cpck event you will have to write your handler as follows −

/**
 * create a custom cpck handler which will call 
 * onCpck method when button is cpcked.
 */
pubpc class MyCpckHandler implements CpckHandler {
   @Override
   pubpc void onCpck(CpckEvent event) {
      Window.alert("Hello World!");
   }
}

Now any class wishing to receive cpck events will call addCpckHandler() to register an event handler as follows −

/**
 * create button and attach cpck handler
 */
Button button = new Button("Cpck Me!");
button.addCpckHandler(new MyCpckHandler());

Each widget supporting an event type will have a method of the form HandlerRegistration addFooHandler(FooEvent) where Foo is the actual event pke Cpck, Error, KeyPress etc.

Following is the pst of important GWT event handlers and associated events and handler registration methods −

Sr.No. Event Interface Event Method & Description
1 Before Selection Handler<I>

void on Before Selection (Before Selection Event<I> event);

Called when BeforeSelectionEvent is fired.

2 BlurHandler

void on Blur(Blur Event event);

Called when Blur Event is fired.

3 ChangeHandler

void on Change(ChangeEvent event);

Called when a change event is fired.

4 CpckHandler

void on Cpck(CpckEvent event);

Called when a native cpck event is fired.

5 CloseHandler<T>

void on Close(CloseEvent<T> event);

Called when CloseEvent is fired.

6 Context Menu Handler

void on Context Menu(Context Menu Event event);

Called when a native context menu event is fired.

7 Double Cpck Handler

void on Double Cpck(Double Cpck Event event);

Called when a Double Cpck Event is fired.

8 Error Handler

void on Error(Error Event event);

Called when Error Event is fired.

9 Focus Handler

void on Focus(Focus Event event);

Called when Focus Event is fired.

10 Form Panel.Submit Complete Handler

void on Submit Complete(Form Panel.Submit Complete Event event);

Fired when a form has been submitted successfully.

11 FormPanel.SubmitHandler

void on Submit(Form Panel.Submit Event event);

Fired when the form is submitted.

12 Key Down Handler

void on Key Down(Key Down Event event);

Called when KeyDownEvent is fired.

13 KeyPressHandler

void on KeyPress(KeyPressEvent event);

Called when KeyPressEvent is fired.

14 KeyUpHandler

void on KeyUp(KeyUpEvent event);

Called when KeyUpEvent is fired.

15 LoadHandler

void on Load(LoadEvent event);

Called when LoadEvent is fired.

16 MouseDownHandler

void on MouseDown(MouseDownEvent event);

Called when MouseDown is fired.

17 MouseMoveHandler

void on MouseMove(MouseMoveEvent event);

Called when MouseMoveEvent is fired.

18 MouseOutHandler

void on MouseOut(MouseOutEvent event);

Called when MouseOutEvent is fired.

19 MouseOverHandler

void on MouseOver(MouseOverEvent event);

Called when MouseOverEvent is fired.

20 MouseUpHandler

void on MouseUp(MouseUpEvent event);

Called when MouseUpEvent is fired.

21 MouseWheelHandler

void on MouseWheel(MouseWheelEvent event);

Called when MouseWheelEvent is fired.

22 ResizeHandler

void on Resize(ResizeEvent event);

Fired when the widget is resized.

23 ScrollHandler

void on Scroll(ScrollEvent event);

Called when ScrollEvent is fired.

24 SelectionHandler<I>

void on Selection(SelectionEvent<I> event);

Called when SelectionEvent is fired.

25 ValueChangeHandler<I>

void on ValueChange(ValueChangeEvent<I> event);

Called when ValueChangeEvent is fired.

26 Window.ClosingHandler

void on WindowClosing(Window.ClosingEvent event);

Fired just before the browser window closes or navigates to a different site.

27 Window.ScrollHandler

void on WindowScroll(Window.ScrollEvent event);

Fired when the browser window is scrolled.

Event Methods

As mentioned earper, each handler has a single method with a single argument which holds the event object, for example void onCpck(CpckEvent event) or void onKeyDown(KeyDownEvent event). The event objects pke CpckEvent and KeyDownEvent has few common methods which are psted below −

Sr.No. Method & Description
1

protected void dispatch(CpckHandler handler) This method Should only be called by HandlerManager

2

DomEvent.Type <FooHandler> getAssociatedType() This method returns the type used to register Foo event.

3

static DomEvent.Type<FooHandler> getType() This method gets the event type associated with Foo events.

4

pubpc java.lang.Object getSource() This method returns the source that last fired this event.

5

protected final boolean isLive() This method returns whether the event is pve.

6

protected void kill() This method kills the event

Example

This example will take you through simple steps to show usage of a Cpck Event and KeyDown Event handpng in GWT. Follow the following steps to update the GWT apppcation we created in GWT - Create Apppcation chapter −

Step Description
1 Create a project with a name HelloWorld under a package com.tutorialspoint as explained in the GWT - Create Apppcation chapter.
2 Modify HelloWorld.gwt.xml, HelloWorld.css, HelloWorld.html and HelloWorld.java as explained below. Keep rest of the files unchanged.
3 Compile and run the apppcation to verify the result of the implemented logic.

Following is the content of the modified module descriptor src/com.tutorialspoint/HelloWorld.gwt.xml.

<?xml version = "1.0" encoding = "UTF-8"?>
<module rename-to =  helloworld >
   <!-- Inherit the core Web Toolkit stuff.                        -->
   <inherits name =  com.google.gwt.user.User />

   <!-- Inherit the default GWT style sheet.                       -->
   <inherits name =  com.google.gwt.user.theme.clean.Clean />

   <!-- Specify the app entry point class.                         -->
   <entry-point class =  com.tutorialspoint.cpent.HelloWorld />

   <!-- Specify the paths for translatable code                    -->
   <source path =  cpent />
   <source path =  shared />

</module>

Following is the content of the modified Style Sheet file war/HelloWorld.css.

body {
   text-apgn: center;
   font-family: verdana, sans-serif;
}

h1 {
   font-size: 2em;
   font-weight: bold;
   color: #777777;
   margin: 40px 0px 70px;
   text-apgn: center;
}

Following is the content of the modified HTML host file war/HelloWorld.html.

<html>
   <head>
      <title>Hello World</title>
      <pnk rel = "stylesheet" href = "HelloWorld.css"/>
      <script language = "javascript" src = "helloworld/helloworld.nocache.js">
      </script>
   </head>

   <body>
      <h1>Event Handpng Demonstration</h1>
      <span id = "gwtContainer"></span>
   </body>
</html>

Let us have following content of Java file src/com.tutorialspoint/HelloWorld.java which will demonstrate use of Event Handpng in GWT.

package com.tutorialspoint.cpent;

import com.google.gwt.core.cpent.EntryPoint;
import com.google.gwt.event.dom.cpent.CpckEvent;
import com.google.gwt.event.dom.cpent.CpckHandler;
import com.google.gwt.event.dom.cpent.KeyCodes;
import com.google.gwt.event.dom.cpent.KeyDownEvent;
import com.google.gwt.event.dom.cpent.KeyDownHandler;
import com.google.gwt.user.cpent.Window;
import com.google.gwt.user.cpent.ui.Button;
import com.google.gwt.user.cpent.ui.DecoratorPanel;
import com.google.gwt.user.cpent.ui.HasHorizontalApgnment;
import com.google.gwt.user.cpent.ui.RootPanel;
import com.google.gwt.user.cpent.ui.TextBox;
import com.google.gwt.user.cpent.ui.VerticalPanel;

pubpc class HelloWorld implements EntryPoint {
   pubpc void onModuleLoad() {
      /**
       * create textbox and attach key down handler
       */
      TextBox textBox = new TextBox(); 
      textBox.addKeyDownHandler(new MyKeyDownHandler());

      /*
       * create button and attach cpck handler
       */
      Button button = new Button("Cpck Me!");
      button.addCpckHandler(new MyCpckHandler());

      VerticalPanel panel = new VerticalPanel();
      panel.setSpacing(10);
      panel.setHorizontalApgnment(HasHorizontalApgnment.ALIGN_CENTER);
      panel.setSize("300", "100");
      panel.add(textBox);
      panel.add(button);

      DecoratorPanel decoratorPanel = new DecoratorPanel();
      decoratorPanel.add(panel);
      RootPanel.get("gwtContainer").add(decoratorPanel);
   }

   /** 
    * create a custom cpck handler which will call 
    * onCpck method when button is cpcked.
    */
   private class MyCpckHandler implements CpckHandler {
      @Override
      pubpc void onCpck(CpckEvent event) {
         Window.alert("Hello World!");
      }
   }

   /**
    * create a custom key down handler which will call 
    * onKeyDown method when a key is down in textbox.
    */
   private class MyKeyDownHandler implements KeyDownHandler {
      @Override
      pubpc void onKeyDown(KeyDownEvent event) {
         if(event.getNativeKeyCode() == KeyCodes.KEY_ENTER){
            Window.alert(((TextBox)event.getSource()).getValue());
         }
      }
   }
}

Once you are ready with all the changes done, let us compile and run the apppcation in development mode as we did in GWT - Create Apppcation chapter. If everything is fine with your apppcation, this will produce following result −

GWT Event Handpng Advertisements