How can I use the system's context menu for files in a Java application?
Asked Answered
M

2

4

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.

Mart answered 14/7, 2010 at 14:18 Comment(0)
E
1

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.

Electrokinetics answered 14/7, 2010 at 21:0 Comment(3)
If I understand correctly, that is not a general enough solution. I don't know if it can be done, but I'd like to get the system's context menu, instead of creating it myself. It seems as if applications that have that feature use the system's actual menu somehow. Maybe they've just done a good work creating the menu themselves. I don't know. If that's not possible - how can I at least create a menu option "Locate on disk" which opens the system's file explorer GUI and selects the file?Mart
There is no such thing as the "system's context menu". The context menu is application specific. Even if you think of the context menu that pops up when you right click on your Windows Desktop it is still application - it just so happens the application is Windows Explorer. And you are right applications just create the menu themselves and the SWT menu is a native menu so it will look like those other applications. If you just want to open a file dialog then use FileDialog. An example can be found at: java2s.com/Code/Java/SWT-JFace-Eclipse/FileDialogExample.htmElectrokinetics
Since this context menu is available from both FileDialog dialogs and Windows Explorer (and probably other locations), I thought it might be some system object which is available. It's not a FileDialog I'm displaying, but search results - and I wanted to enable that menu. I'll settle on just implementing a "Open file location" for Windows systems (I found here how to do it). Thanks!Mart
A
0

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.

Adze answered 7/8, 2015 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.