容易腻,在contextmenuhandler上添加一个监听器,它将根据用户右键单击的位置显示一个小部件.
https://confluence.clazzes.org/pages/viewpage.action?pageId=425996
class MyWidget extends Composite implements ContextMenuHandler {
// just an example,use a meaningful Widget here...
private Widget base;
private PopupPanel contextMenu;
public MyWidget() {
// initialize base widget,etc...
this.contextMenu = new PopupPanel(true);
this.contextMenu.add(new HTML("My Context menu!"));
this.contextMenu.hide();
initWidget(this.base);
// of course it would be better if base would implement HasContextMenuHandlers,but the effect is the same
addDomHandler(this,ContextMenuEvent.getType());
}
public void onContextMenu(ContextMenuEvent event) {
// stop the browser from opening the context menu
event.preventDefault();
event.stopPropagation();
this.contextMenu.setPopupPosition(event.getNativeEvent().getClientX(),event.getNativeEvent().getClientY());
this.contextMenu.show();
}
}
最后,您将要禁用浏览器菜单以完全重载此类上下文菜单.除了opera之外,这应该适用于所有浏览器.但老实说,这几天新人使用了^ _______ ^