Handler on DOM elements in GWT
19,403
Your code is correct, you might added widget after sink event. you have to add widget before sink event. just example:
Button button=new Button("Click");
Element buttonElement = button.getElement();
RootPanel.get().add(button);
Event.sinkEvents(buttonElement, Event.ONCLICK);
Event.setEventListener(buttonElement, new EventListener() {
@Override
public void onBrowserEvent(Event event) {
System.out.println("ok");
if(Event.ONCLICK == event.getTypeInt()) {
Window.alert("ok");
System.out.println("CLICK");
}
}
});
Related videos on Youtube
Author by
Mani
Updated on June 21, 2022Comments
-
Mani almost 2 years
I want to add the handler on the buttonelement and i have implemented it as follow. Please help me in resolving the error in this code. I do not want to add handler directly on the button widget.
Button button = new Button("Click"); Element buttonElement = button.getElement(); Event.setEventListener(buttonElement, new EventListener() { @Override public void onBrowserEvent(Event event) { String string = event.getType(); if(string.equalsIgnoreCase("click")) { System.out.println("CLICK"); } } }); Event.sinkEvents(buttonElement, Event.ONCLICK);
-
Mani almost 11 yearsbut why we need to call sinkEvents function afterwards.
-
Thomas Broyer almost 11 yearsBecause
Button
(and most widgets) will only sink an event if there's a handler for it; this is done automatically byaddDomHandler
. Here for some unknown reason you don't want toaddClickHandler
, so you have to callsinkEvents
by yourself. BTW, why create aButton
widget if you don't use its events? How aboutDocument.get().createButtonElement()
? -
Mani almost 11 years@ThomasBroyer actually i wanted to make the rows of datagrid draggable and for that i get row as an element and wanted to add handler on it. [stackoverflow.com/questions/16536118/…
-
bNd almost 11 years@Mani Because Each widget needs to have a single "root" element. Whenever the widget becomes attached, it create exactly one "back reference" from the element to the widget that is,
elem.__listener = widget
, performed in DOM.setEventListener())This is set whenever the widget is attached, and cleared whenever it is detached -
Thomas Broyer almost 11 years@Bhumika: this is only if you don't explicitly use
Event.setEventListener
(which works at the element level, not the widget level, and has therefore no notion of attached/detached);sinkEvents
is also only called in the firstonAttach
on widgets. -
bNd almost 11 yearsOk, Thanks you sir for correction :) Upto now I thought sink event called on both attached/detached.