How can I use the system context menu for files in a Java application?

I want to display file search results. I want to enable a context menu to select a file that will be the system context menu.

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

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

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

If this is impossible or too complicated, I would at least enable the "Find on disk" option, which will open Windows Explorer (or its equivalent on another system) in the folder with the file and select the file.

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

+3
source share
2 answers

See an example of using a popup menu:

Snippet131

Once you are in the handleEvent() method, you can execute any logic necessary to add menu items to the context menu.

To get specific platform behavior, you can use System.getProperty () with a combination of the strings os.name, os.arch, and os.version to determine which platform you are running on. Then simply use the instructions to conditionally add menu items to the menu.

+1
source

On Windows, you can achieve this, but you need to call some native COM methods. I did this with local calls, maybe this is possible with JNA. You need the functionality of IContextMenu2 .

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

0
source

All Articles