addDomHandler and event bubbling

509 views
Skip to first unread message

Ümit Seren

unread,
Nov 15, 2012, 7:42:56 AM11/15/12
to google-we...@googlegroups.com

I have some troubles understanding event bubbling and GWT.

Let's assume I have following structure:

<g:HTMLPanel>
    <div>
       <span>Text</span>
   </div>
</g:HTMLPanel>

When I want to handle click events on the elements nested in the HTMLPanel I add a DOM handler to the HTMLPanel:


HTMLPanel.addDomHandler(new ClickHandler() {

    @Override
    public void onClick(ClickEvent event) {
        Element target = event.getNativeEvent().getEventTarget().cast();
    }

}, ClickEvent.getType());

My expectation is that if I click on the the div element the onClick method is called once with the target set to the div element. If I click on the span element I expect the onClick method to be called twice, once with the target set to div and another time set to the span element.

However that doesn't work. When I click on the span element the onClick is only called once with the target set to the span element. Shouldn't the onClick method be called twice?

Thomas Broyer

unread,
Nov 15, 2012, 9:16:05 AM11/15/12
to google-we...@googlegroups.com
No.

The event is first dispatched on the span, where nobody listens to it. It then bubbles up to the div (currentEventTarget points to the div), where nobody listens either. Then again, it bubbles up to the HTMLPanel's div, where you listen to it. There, getEventTarget() is the span, and getCurrentEventTarget() is the HTMLPanel's getElement(). (and it continues bubbling that way up until the document, or until a handler calls stopPropagation())

Ümit Seren

unread,
Nov 15, 2012, 9:22:45 AM11/15/12
to google-we...@googlegroups.com
Thanks for the explanation. 



--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/omBwFmtOjUUJ.

To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Reply all
Reply to author
Forward
0 new messages