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);
HTML DOM events allow JavaScript to register different event handlers on elements in an HTML document. Events are normally used in combination with functions, and the function will not be executed before the event occurs (such as when a user clicks a button).
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");
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With