An event to be performed when I click a Panel in G

2019-04-16 17:34发布

I want to perform an event when I click on a panel, in the same way this happens when the user clicks on a button.

I need this in order to handle events on click for this panel.

标签: gwt mgwt
2条回答
爷的心禁止访问
2楼-- · 2019-04-16 17:50

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());
查看更多
迷人小祖宗
3楼-- · 2019-04-16 17:56

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.
    }
});
查看更多
登录 后发表回答