Adding ClickHandler to div which contains many other widget

后端 未结 3 993
我寻月下人不归
我寻月下人不归 2020-12-14 22:25

It seems that Panel doesn\'t inherit from HasClickHandlers and I can\'t add a clickHandler to DivElement. Is there any way to add the clickHandler to a DIV?

EDIT:

3条回答
  •  囚心锁ツ
    2020-12-14 23:06

    By default, Panel doesnt 'sink' the onCLick event. That is, clicking on Panels doesnt actually result into a 'ClickEvent' and hence the handler isnt fired. To use click events with Panel (or for that matter any other Widget) you must first invoke sinkEvents() on it with the appropriate event bits.

    For example:

    SimplePanel p = new SimplePanel();
        p.sinkEvents(Event.ONCLICK);
        p.setTitle("Click me");
        p.setSize("600px", "600px");
        p.addHandler(new ClickHandler(){
    
            @Override
            public void onClick(ClickEvent event) {
    
                Window.alert("SimplePanel clicked!");
    
            }
    
        }, ClickEvent.getType());
    

    The above piece of code first 'enables' click events for the SimplePanel using p.sinkEvents(Event.ONCLICK); and then goes on to add the standard ClickHandler onto it. Note that even though SimplePanel doesnt implement HasClickHandlers, you can always add a handler using the addHandler() method.

提交回复
热议问题