Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RIght-Click in GWT?

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!

like image 762
Soren Johnson Avatar asked Sep 16 '09 17:09

Soren Johnson


3 Answers

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;">
like image 195
1-14x0r Avatar answered Oct 08 '22 15:10

1-14x0r


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/

like image 41
ire_and_curses Avatar answered Oct 08 '22 13:10

ire_and_curses


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.

like image 31
Daniel Vaughan Avatar answered Oct 08 '22 15:10

Daniel Vaughan