Skip to end of metadata
Go to start of metadata

The problem of providing a customized context-menu on right-click in HTML are an old hat, as this example from 2002 (sic!) demonstates, but solving this problem in GWT is still a major operation.. or is it?

Googling for "gwt context menu" produces a host of "solutions" to the problem, notably most of them at least two years old. They mostly involve registering a DOM event-listener for the oncontextmenu event, more or less manually, and at least one custom class.
What they all miss is that google silently introduced the ContextMenuEvent (possibly as early as 2008), a DomEvent, which offers an unspectacularly painless solution to the problem. Just implement the handler and add it to the widget you want to use it in (if need be you can do this using addDomHandler() directly, although arguably this is not in good style) - finished!

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:

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!"));


    // 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

    this.contextMenu.setPopupPosition(event.getNativeEvent().getClientX(), event.getNativeEvent().getClientY());;

  • No labels


  1. Anonymous

    This is the proper way to do it. Good job! +1

  2. Anonymous

    Hello, this works perfect!, but i need to add a context menu to a trecell abstractcell, how is this possible?

  3. Anonymous

    Is there a way to do this in an AbstractCell instead of a widget?

     Thanks. Please to

  4. Anonymous

    Thanks, This helped

  5. Anonymous

    Don't forget, you have to register Event sinking for your client area where the content menu should be active:


    Here is my working solution for Root Panel (content menu for potentially whole browser window):

      new ContextMenuHandler() {
        public void onContextMenu(ContextMenuEvent event) {
    }, ContextMenuEvent.getType());

    Best Regards
    Jan Uhlir

  6. Anonymous

    Thanks, nice (smile)


    I have an issue: my page is scrollable. So after I scroll it down, contextMenu appears upper... looks like it doesn't know anything about scrolling (sad)

  7. Anonymous

    I found the solution - Y should be get like this: 


    And I think X in the same way.