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

        }
    });
Share:
19,403

Related videos on Youtube

Mani
Author by

Mani

Updated on June 21, 2022

Comments

  • Mani
    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
    Mani almost 11 years
    but why we need to call sinkEvents function afterwards.
  • Thomas Broyer
    Thomas Broyer almost 11 years
    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()?
  • Mani
    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
    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
    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 first onAttach on widgets.
  • bNd
    bNd almost 11 years
    Ok, Thanks you sir for correction :) Upto now I thought sink event called on both attached/detached.