8

I am building an AJAX web app with GWT, and I want to use right-click for various things, just like in a desktop app. However, right-click produces the standard Web context menu and void onClick(ClickEvent event) never gets called. Has anyone figured out how to get this to work? thanks!

ire_and_curses
  • 68,372
  • 23
  • 116
  • 141
Soren Johnson
  • 2,561
  • 4
  • 21
  • 17
  • if it is any help without preventing default browser right-click menu, you can catch a right click by using MouseDownHandler instead of ClickHandler. – pistolPanties Sep 13 '11 at 14:55

3 Answers3

7

easy peasy, add a listener on the contextmenuhandler which will display a widget based on where the user right clicks. 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();
  }

}

lastly you will want to disable the browsers menu for full overloading of this type of context menu. That should work in all of the browsers except opera. but honestly who uses that these days neways ^_______^

<body oncontextmenu="return false;">
blacelle
  • 2,199
  • 1
  • 19
  • 28
1-14x0r
  • 1,697
  • 1
  • 16
  • 19
  • You get an upvote for the . That part was driving me nuts! – Jamie Sep 22 '15 at 17:51
  • That is the magic! You have no idea how many years it took me to figure that one out back before fancy JS frameworks were around! – 1-14x0r Sep 23 '15 at 18:47
4

It turns out you can do it by extending DeckPanel. Here's an excellent discussion, along with a nice demo that proves it works.

http://whatwouldnickdo.com/wordpress/370/gwt-right-click-context-menu/

ire_and_curses
  • 68,372
  • 23
  • 116
  • 141
  • thats in efficent and will hiccup complex apps with lots of objects rendered on the screen as you aren't using the core event engine inside gwt. – 1-14x0r Aug 06 '12 at 15:02
1

Although there are ways of doing it I believe the GWT team had a debate about this and decided enabling right click in a web app was a bad thing and so made the concious decision not to support it. The argument was that right click should continue to work as expected (bring up the host browser's right click context menu) and overriding this was breaking that expected behaviour and that and would be bad practice. While I have had instances where a right click context menu would be useful generally I tend to agree with the GWT team's decision.

Daniel Vaughan
  • 666
  • 1
  • 5
  • 14
  • 5
    I couldn't disagree more. I would guess that the majority of GWT devs use GWT in an attempt to better emulate the desktop experience. And that includes contextual (right) clicks. There are many enterprise apps where it makes sense to use right clicks. – JP Richardson Sep 17 '09 at 21:36
  • I know what you mean, I got quite frustrated that right click was missing at first but after reading the GWT team's argument I slowly came round. – Daniel Vaughan Sep 18 '09 at 07:54
  • @Daniel Vaughan: Interesting point. I would vote this up if you added a link to the GWT discussion you mention. – ire_and_curses Sep 18 '09 at 12:22
  • Please >_>that's why they have a ContextMenuHandler that was implements around 2008/2009. They just didn't put it on there showcase becuase its one of those advanced features that requires you to read the API. – 1-14x0r Aug 06 '12 at 15:01