I would like to listen for the mouse over event in GWT 1.6. Since GWT 1.6 has introduced handlers and deprecated listeners I\'m unsure as to how I can accomplish this with what
I was hoping we'd see an answer before I needed to tackle this myself. There are some errors in the example code he posted, but the post by Mark Renouf in this thread has most of what we need.
Let's say you want to listen for mouse over and mouse out events on a custom widget. In your widget, add two methods:
public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
return addDomHandler(handler, MouseOverEvent.getType());
}
public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
return addDomHandler(handler, MouseOutEvent.getType());
}
Then create a handler class:
public class MyMouseEventHandler implements MouseOverHandler, MouseOutHandler {
public void onMouseOver(final MouseOverEvent moe) {
Widget widget = (Widget) moe.getSource();
widget.addStyleName("my-mouse-over");
}
public void onMouseOut(final MouseOutEvent moe) {
Widget widget = (Widget) moe.getSource();
widget.removeStyleName("my-mouse-over");
}
}
Finally, add the handler to the widget:
myWidget.addMouseOverHandler(new MyMouseEventHandler());
myWidget.addMouseOutHandler(new MyMouseEventHandler());
If you are only listening to the mouse over event, you can skip the mouse out handling. And if you aren't making a custom widget, the widget my already have a method to add the handler.
Finally, per the warning from the thread, remember to addDomHandler
for the mouse events, not addHandler
.