Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Judging by the age of the postings on this issue, by now most people have found a solution that works for them. However it may not be the simplest...

The simplest way of implementing a custom context menu is certainly the following:

Code Block

class MyWidget extends Composite implements ContextMenuHandler {
  
  // this can be anything, as long as it extends widget
  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();


    initWidet(this.base);

    // of course it would be better if base would implement HasContextMenuHandlers, but the effect is the same
    addDomHandler(this, ContextMenuEvent.getType());
  }


  public 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();
  }
            event.stopPropagation();

}