An event to be performed when I click a Panel in GWT/MGWT [closed]

ε祈祈猫儿з 提交于 2019-12-04 23:06:33

You have to use GWT FocusPanel which makes its contents focusable, and adds the ability to catch mouse and keyboard events. So wrap your panel inside FocusPanel.

Panel panel = new Panel();    //Your panel here(ex;hPanel,vPanel)
FocusPanel focusPanel = new FocusPanel();

focusPanel.addClickListener(new ClickListener(){

    public void onClick(Widget sender) {
        // TODO Auto-generated method stub
    }

});

focusPanel.add(panel); 

One more possibility(without FocusPanel)

 HorizontalPanel hpanel = new HorizontalPanel();
        hpanel.sinkEvents(Event.CLICK);
        hpanel.addHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                // TODO Auto-generated method stub

            }
        }, ClickEvent.getType());

I would make a new widget ( extending in this example an Absolute panel ) that implements the HasClickHandlers interface like this

public class MyCustomPanel extends AbsolutePanel
implements HasClickHandlers
{
    public HandlerRegistration addClickHandler(
        ClickHandler handler)
    {
        return addDomHandler(handler, ClickEvent.getType());
    }
}

And then in my code I would do it like this

MyCustomPanel mPanel = new MyCustomPanel();
mPanel.addClickHandler(new ClickHandler() {
    public void onClick(ClickEvent event) {
        // Do on click stuff here.
    }
});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!