14.4. Preventing default actions

The browser is quite happy to handle some events by itself—press the Enter key when you’re in a form, and the browser will generally submit it; drag an image to the location bar, and the browser will reload it; press Ctrl+N in most browsers, and a new browser window will open.

Sometimes these default actions are useful, and sometimes not. In the example application from chapter 3 we prevent the logo image from being dragged to the location bar by overriding the onBrowserEvent method when we create it:

logo = new Image(GWT.getModuleBaseURL() + "../" + LOGO_IMAGE_NAME){
   public void onBrowserEvent(Event evt){
      evt.preventDefault();
      super.onBrowserEvent(evt);
   }
};

By calling the preventDefault method on the event, ...

Get GWT in Action, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.