Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handler on DOM elements in GWT

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);
like image 213
Mani Avatar asked May 14 '13 11:05

Mani


People also ask

What is a DOM event handler?

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).


1 Answers

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

        }
    });
like image 87
bNd Avatar answered Oct 16 '22 06:10

bNd