如何在 Java 应用程序中使用系统的文件上下文菜单?

发布于 2024-09-09 06:22:35 字数 366 浏览 1 评论 0原文

我想显示文件搜索的结果。我想启用文件选择的上下文菜单,这将是系统的上下文菜单。

例如,如果用户右键单击 Windows 中的文件 - 我想显示带有选项的弹出菜单:

  • 打开
  • 打开方式...
  • 编辑
  • 发送到...
  • 复制
  • 剪切
  • 等...

并且,如果可能 - 这菜单将根据操作系统自动生成。

如果这不可能或太复杂 - 我想至少启用“在磁盘上查找”选项,该选项将在文件的文件夹中打开 Windows 资源管理器(或其他系统中的等效项)并选择该文件。

该应用程序是使用 SWT 用 Ja​​va (JDK 7) 编写的。

I want to display results of a file search. I want to enable a context menu for a file selection, which will be the system's context menu.

For example, if the user right-clicks a file in Windows - I want to display the popup-menu with the options:

  • Open
  • Open with...
  • Edit
  • Send to...
  • Copy
  • Cut
  • etc...

And, if possible - this menu will be generated automatically, depending on the operating system.

If that is not possible or too complex - I'd like to at least enable a "Locate on disk" option which will open a Windows Explorer (or its equivalent in other system) in the file's folder and select the file.

The application is written in Java (JDK 7) using SWT.

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

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

发布评论

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

评论(2

猫七 2024-09-16 06:22:35

看一下如何使用弹出菜单的示例:

Snippet131

一旦进入 handleEvent() 方法,您就可以执行所需的任何逻辑,以便将菜单项添加到上下文菜单中。

为了获得特定于平台的行为,您可以将 System.getProperty() 与“os.name”、“os.arch”和“os.version”字符串组合使用,以确定您正在运行的平台。然后只需使用 if 语句有条件地将菜单项添加到菜单中。

Take a look at the example of how to use a popup menu:

Snippet131

Once you are in the handleEvent() method you can perform any logic you need in order to add menu items to your context menu.

In order to get platform specific behavior you can use System.getProperty() with a combination of the "os.name", "os.arch", and "os.version" strings in order to determine which platform you are running. Then just use if statements to conditionally add menu items to your menu.

怪我太投入 2024-09-16 06:22:35

对于 Windows,您可以实现此目的,但必须调用一些本机 COM 方法。我用本地调用做到了,也许用 JNA 是可能的。您需要 IContextMenu2< 的功能/a>.

然后,您可以扩展 SWT Menu 类,用 QueryContextMenu() 填充它,将其子类化并处理 WM_DRAWITEMWM_MEASUREITEM >、WM_INITMENUPOPUPWM_MENUSELECTWM_COMMAND 并通过 HandleMenuMsg 将它们转发到 IContextMenu2 实例。

For Windows you can achieve this but you have to call some native COM methods. I did it with native calls, maybe it is possible with JNA. You need the functionality from IContextMenu2.

Then you can extend the SWT Menu class, populate it with QueryContextMenu(), subclass it and handle WM_DRAWITEM, WM_MEASUREITEM, WM_INITMENUPOPUP, WM_MENUSELECT and WM_COMMAND and forward them to IContextMenu2 instance via HandleMenuMsg.

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