在 GWT 中右键单击?

发布于 2024-08-04 18:18:28 字数 150 浏览 5 评论 0原文

我正在使用 GWT 构建一个 AJAX Web 应用程序,并且我想使用右键单击来执行各种操作,就像在桌面应用程序中一样。然而,右键单击会生成标准的 Web 上下文菜单,并且 void onClick(ClickEvent event) 永远不会被调用。有谁知道如何让它发挥作用吗?谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨轻弹 2024-08-11 18:18:28

很简单,在 contextmenuhandler 上添加一个侦听器,它将根据用户右键单击的位置显示一个小部件。 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();
  }

}

最后,您需要禁用浏览器菜单以完全重载此类上下文菜单。这应该适用于除 Opera 之外的所有浏览器。但老实说,现在谁用它了^_______^

<body oncontextmenu="return false;">

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;">
思念绕指尖 2024-08-11 18:18:28

事实证明,您可以通过扩展 DeckPanel 来做到这一点。这里有一个精彩的讨论,还有一个证明它有效的演示。

http://whatwouldnickdo.com/wordpress/370/gwt-right -单击上下文菜单/

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/

别挽留 2024-08-11 18:18:28

尽管有多种方法可以做到这一点,但我相信 GWT 团队对此进行了辩论,并认为在 Web 应用程序中启用右键单击是一件坏事,因此有意识地决定不支持它。争论的焦点是,右键单击应该继续按预期工作(调出主机浏览器的右键单击上下文菜单),并且覆盖此行为会破坏预期行为,并且这将是不好的做法。虽然我曾经遇到过右键单击上下文菜单通常很有用的情况,但我倾向于同意 GWT 团队的决定。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文