Adding ClickHandler to div which contains many other widget

走远了吗. 提交于 2019-11-28 21:33:08

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.

Use the FocusPanel.

A simple panel that makes its contents focusable, and adds the ability to catch mouse and keyboard events.

public class ClickableDiv extends FlowPanel implements HasClickHandlers {
    @Override
    public HandlerRegistration addClickHandler(ClickHandler handler) {
        return addDomHandler(handler, ClickEvent.getType());
    }
}

That should be all you need. addDomHandler will take care of "sinking" the event type for you.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!