Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop a event from bubbling in GWT

Tags:

java

events

gwt

I have the following snippet of code, changeTextArea is a TextArea object.

changeTextArea.addKeyboardListener(new KeyboardListenerAdapter()
  public void onKeyPress( Widget sender, char keyCode, int modifier){
    //do something
    //I WISH TO STOP THE EVENT THAT MAPS TO THIS KEYPRESS FROM BUBBLING ANY FURTHER
  }
}

How would I stop the Event that is causing this method to be called from bubbling up from changeTextArea into the Panels/Widgets/Composites/Whatever that contain changeTextArea. Put succinctly, how do I stop it from bubbling any further. Any help would be appreciated (especially code samples).

like image 300
Stephen Cagle Avatar asked Oct 18 '08 00:10

Stephen Cagle


People also ask

What is event propagation aka event bubbling )?

Event bubbling is a type of event propagation where the event first triggers on the innermost target element, and then successively triggers on the ancestors (parents) of the target element in the same nesting hierarchy till it reaches the outermost DOM element or document object (Provided the handler is initialized).


2 Answers

As far as I know you can't do it via a keyboard listener, but it is possible by adding an event preview using the DOM class:

DOM.addEventPreview(EventPreview preview) 

Then when you get the event:

onEventPreview(Event event) 

You should return false, to say you want to cancel the event. The Event object also supports this method:

public final void cancelBubble(boolean cancel)

Cancels bubbling for the given event. This will stop the event from being propagated to parent elements.

You can find more details here: http://google-web-toolkit.googlecode.com/svn/javadoc/1.5/index.html?overview-summary.html

like image 134
rustyshelf Avatar answered Oct 07 '22 16:10

rustyshelf


You can definitely use the Event's cancelBubble() and preventDefault() methods from within any code that has access to the Event. There's no need to have an event preview...

like image 24
jgindin Avatar answered Oct 07 '22 18:10

jgindin