15

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
  • 508
  • 1
  • 7
  • 18

1 Answers1

23

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");
             }

        }
    });
bNd
  • 7,512
  • 7
  • 39
  • 72
  • but why we need to call sinkEvents function afterwards. – Mani May 14 '13 at 12:28
  • 3
    Because `Button` (and most widgets) will only _sink_ an event if there's a handler for it; this is done automatically by `addDomHandler`. Here for some unknown reason you don't want to `addClickHandler`, so you have to call `sinkEvents` by yourself. BTW, why create a `Button` widget if you don't use its events? How about `Document.get().createButtonElement()`? – Thomas Broyer May 14 '13 at 12:39
  • @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. [http://stackoverflow.com/questions/16536118/make-the-rows-of-the-datagrid-draggable-in-gwt/16538946?noredirect=1#16538946](thread) – Mani May 14 '13 at 13:02
  • @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 – bNd May 14 '13 at 13:18
  • 1
    @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 first `onAttach` on widgets. – Thomas Broyer May 14 '13 at 13:34
  • Ok, Thanks you sir for correction :) Upto now I thought sink event called on both attached/detached. – bNd May 14 '13 at 13:39