I'm new in GWT! Can anyone help me to implement a context menu with GWT? I have seen some example like:
MenuBar options = new MenuBar(true);
MenuBar gwtPopup = new MenuBar(true);
options.addItem("GWT", gwtPopup );
MenuItem entryPoint = new MenuItem(new SafeHtmlBuilder().appendEscaped("EntryPoint").toSafeHtml());
entryPoint.setScheduledCommand(new ScheduledCommand()
{
public void execute()
{
Window.alert( "hello" );
}
} );
final DialogBox menuWrapper = new DialogBox( true );
menuWrapper.add( options );
gwtPopup.addItem( entryPoint );
Button showMenu = new Button( "Click me", new ClickHandler()
{
public void onClick( ClickEvent event )
{
menuWrapper.showRelativeTo( menuWrapper );
}
} );
RootPanel.get().add( showMenu );
but it doesn't works. Thanks.
Just blogged the steps to make it done with my working code for you.
Context Menu or Right Click Handler in GWT.
Here is the essential part of that code.
lable.sinkEvents(Event.ONCONTEXTMENU);
lable.addHandler(
new ContextMenuHandler() {
@Override
public void onContextMenu(ContextMenuEvent event) {
event.preventDefault();
event.stopPropagation();
popupMenu.setPopupPosition(
event.getNativeEvent().getClientX(),
event.getNativeEvent().getClientY());
popupMenu.show();
}
}, ContextMenuEvent.getType()
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With