如何在Java应用程序中使用系统的上下文菜单? [英] How can I use the system's context menu for files in a Java application?

查看:253
本文介绍了如何在Java应用程序中使用系统的上下文菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

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

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.

应用程序是使用SWT以Java(JDK 7)编写的。

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

推荐答案

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

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

Snippet131

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

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

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

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.

这篇关于如何在Java应用程序中使用系统的上下文菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆