问题
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.
回答1:
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());
回答2:
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.
}
});
来源:https://stackoverflow.com/questions/15547070/an-event-to-be-performed-when-i-click-a-panel-in-gwt-mgwt