GWT MenuItem扩展了UIObject - 任何想法如何添加事件处理程序,例如我想要一个特殊的上下文菜单来右键单击MenuItem。谢谢!
答案 0 :(得分:1)
您可以使用MenuItem对象的底层元素:
final Element element = menuItem.getElement();
Event.addNativePreviewHandler(new Event.NativePreviewHandler() {
@Override
public void onPreviewNativeEvent(Event.NativePreviewEvent event) {
if (event.getTypeInt() == Event.ONMOUSEDOWN
&& event.getNativeEvent().getButton() == Event.BUTTON_RIGHT
&& event.getNativeEvent().getEventTarget().equals(element)) {
showSpecialMenu();
}
}
});
Event.sinkEvents(element, Event.ONMOUSEDOWN);
或者像这样(你也可以阻止默认的上下文菜单):
final Element element = menuItem.getElement();
Event.addNativePreviewHandler(new Event.NativePreviewHandler() {
@Override
public void onPreviewNativeEvent(Event.NativePreviewEvent event) {
if (event.getTypeInt() == Event.ONCONTEXTMENU && event.getNativeEvent().getEventTarget().equals(element)) {
// prevent default context menu
event.getNativeEvent().stopPropagation();
event.getNativeEvent().preventDefault();
// show custom context menu
showSpecialMenu();
}
}
});
Event.sinkEvents(element, Event.ONCONTEXTMENU);
你可以这样做:
......
Event.sinkEvents(element, Event.ONCONTEXTMENU | Event.ONMOUSEDOWN);