2

I am trying to utilize Widget.addHandler(). However, the handler never gets called. Below is my sample code. What do I need to change to fix this?

My Handler Implementation:

public class CustomMouseMoveHandler
  extends GwtEvent.Type<MouseMoveHandler>
  implements MouseMoveHandler
{
  @Override
  public void onMouseMove(MouseMoveEvent event) {
    System.out.println("----> onMouseMove.");
  }
}

My EntryPoint.OnModuleLoad():

ContentPanel cp = new ContentPanel();
cp.setHeaderVisible(false);
cp.setHeight(com.google.gwt.user.client.Window.getClientHeight());

CustomMouseMoveHandler handler = new CustomMouseMoveHandler();
cp.addHandler(handler, handler);

RootPanel.get().add(cp);

///// Added on 7/1/2011.

The following complete GWT simple code does not work either (with Jason's hint applied). Please help me out. Thanks

 package tut.client;

 import com.google.gwt.core.client.EntryPoint;
 import com.google.gwt.event.dom.client.MouseMoveEvent;
 import com.google.gwt.event.dom.client.MouseMoveHandler;
 import com.google.gwt.user.client.ui.RootPanel;
 import com.google.gwt.user.client.ui.TextArea;

  /**
  * Entry point classes define <code>onModuleLoad()</code>.
  */
    public class GwtHandler implements EntryPoint, MouseMoveHandler {

        /**
         * This is the entry point method.
         */
        public void onModuleLoad() {
           TextArea comp = new TextArea();

           comp.setSize("200px", "200px");
           comp.setText("Testing Text");        

           comp.addHandler(this, MouseMoveEvent.getType());

           RootPanel.get().add(comp);
        }

       @Override
       public void onMouseMove(MouseMoveEvent event) {
           com.google.gwt.user.client.Window.alert("onMouseMove");
       }
}
enrybo
  • 1,787
  • 1
  • 12
  • 20
Peter
  • 67
  • 1
  • 2
  • 7

3 Answers3

9

GwtEvent.Type is used to dispatch events based on an event specific and unique object (object equality - == - is used to match event types). Passing your CustomMouseMoveHandler as the Type to addHandler indicates an event type other than that used for MouseMoveEvents (Indeed in this case every CustomMouseMoveHandler would be assigned to a different event Type since each object is different).

Instead of extending GwtEvent.Type<MouseMoveHandler> in your handler you need to get the event Type from MouseMoveEvent itself (using the static getType() method).

Don't extend GwtEvent.Type in your CustomMouseMoveHandler:

public class CustomMouseMoveHandler
  implements MouseMoveHandler
{
  ...
}

And to add the handler:

cp.addDomHandler(handler, MouseMoveEvent.getType());
Jason Terk
  • 6,005
  • 1
  • 27
  • 31
  • Thanks for help Jason. It still does not work for me yet. Please see my edited question above. – Peter Jun 30 '11 at 18:21
  • `System.out.println()` is a no-op in GWT, because there is no cross-browser "stdout" to print to. Try using something like `Window.alert()` to detect the event instead. – Jason Terk Jun 30 '11 at 18:47
  • 1
    GWT.log() can also be used to print debug messages. – Daniel Lundmark Jun 30 '11 at 18:51
  • Also, Peter, please don't make edits to questions that change them fundamentally. If you, for example, change the code in the question to match an answer then those who look at the question later will be confused as to what the original problem was. – Jason Terk Jun 30 '11 at 18:52
  • For the sake of simple to put System.out.println on my question. However, I used Log.info() in my actual code, and also set debugger break point in the Log.info() statement. It does not jump to the statement at all. Thanks for help Jason and Daniel! – Peter Jul 01 '11 at 12:05
  • I just realized that my edited question is rolled back to the original. According to Jason hint, I edited my code as cp.addHandler(this, MouseMoveEvent.getType()); where this implements MouseMoveListener. – Peter Jul 01 '11 at 12:09
  • Jason, combine your hint with Thomas'. It works now. Thanks for your help! – Peter Jul 01 '11 at 14:45
  • I changed addHandler to addDomHandler in my answer. Thanks Thomas. – Jason Terk Jul 01 '11 at 16:36
5

DomEvents have to be registered using addDomHandler, or you have to sinkEvents for their event type. addDomHandler is a shortcut for sinkEvents+addHandler.

Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164
4

Here's how I solved my problem. I wanted to add handlers to a NumberLabel. This is what worked:

    final NumberLabel<Long> label = new NumberLabel<Long>();
    label.setValue(2000l);
    label.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
    MouseOverHandler handler = new MouseOverHandler() {

        public void onMouseOver(MouseOverEvent event) {
            System.out.println("mouse over");
        }
    };
    Widget widget = label.asWidget();
    widget.addDomHandler(handler, MouseOverEvent.getType());

Treating is as a Widget did the trick.

By the way, System.out.println worked.

Shantha Kumara
  • 3,272
  • 4
  • 40
  • 52
Bucky Pope
  • 135
  • 2
  • 11