Re: Field does not have 'addClickHandler' method associated.

706 views
Skip to first unread message

Jens

unread,
Dec 4, 2012, 2:50:12 PM12/4/12
to google-we...@googlegroups.com
For receiving events on DOM elements, check out the DOM class.

DOM.sinkEvents(element, Event.ONCLICK);

and for multiple events: 

DOM.sinkEvents(element, Event.ONCLICK | Event.ONFOCUS | .... | ....)

and finally:

DOM.setEventListener(element, new EventListener() {...implement...});

Make sure to clean up the EventListener if the element is removed from the DOM using DOM.setEventListener(element, null). Otherwise you may risk a memory leak.

You also need to hard cast your DivElement into com.google.gwt.user.client.Element to make it useable with the DOM event methods above.

-- J.

Thomas Broyer

unread,
Dec 4, 2012, 2:51:29 PM12/4/12
to google-we...@googlegroups.com


On Tuesday, December 4, 2012 6:10:58 PM UTC+1, ArtG wrote:
A little help here please. I am experimenting with our exiting non-gwt web page to see if it can be converted.  It is quite complicated. Therefore I simplified it down to just a few statements to try and get a feel for how gwt would handle things.  The following is a very straight forward ui.xml file. It uses HTML DIV statements because the non-gwt version does. It is as follows":

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:g="urn:import:com.google.gwt.user.client.ui" xmlns:a="urn:import:com.google.gwt.dev.asm">
<ui:style>
</ui:style>
<g:HTMLPanel>
<!-- Arts code starts here -->
<div>
<div>
&nbsp;
<h1>This is a big title.</h1>
<h2>This is a smaller title.</h2>

<div >
<div ui:field="fubarHome">
<a style="float:left" href='#'>Home</a>
</div>
</div> >
</div>
</div>
<!-- Arts code ends here. -->
</g:HTMLPanel>
</ui:UiBinder> 

My supporting java form is as followings:

public class TanHpForm extends Composite{

private static TanHpFormUiBinder uiBinder = GWT
.create(TanHpFormUiBinder.class);

interface TanHpFormUiBinder extends UiBinder<Widget, TanHpForm> {
}

public TanHpForm() {
initWidget(uiBinder.createAndBindUi(this));
}
@UiField DivElement fubarHome ;
@UiHandler("fubarHome")
void handleFubarHomeClick(ClickEvent e) {
Window.alert("Hello!");
}
}

This causes a "does not have 'addClikcHandler' method associated error. I know that the easy answer is to use widgets in the ui.xml file but at this time it is not practical. Therefore, I placed the ui:field with the div. How can I associate a click handler to this field?

You can't.

You can add a ClickHandler on the HTMLPanel and then filter out events that happened outside the div (aka event delegation pattern).
Or you can simply replace your <div> with a <g:HTML> widget, which is "a div as a widget with many event handlers and containing HTML". If you need a HTMLPanel instead (because you have other ui:field to put within it), then you'd have to trade the @UiHandler with a call to addDomHandler in your Java code.
Reply all
Reply to author
Forward
0 new messages