Rahul
Rahul

Reputation: 1

How to create context menu or right click menu on button

when i right click on button open a context menu. how i will do it. i try this code also

MenuManager menuManager = new MenuManager();
Menu menu = menuManager.createContextMenu(btn.getText());

btn.getText().setMenu(menu);

getSite().registerContextMenu(menuManager, btn);

getSite().setSelectionProvider(btn);

I got problem in createContextMenu. Please Help me

Upvotes: 0

Views: 572

Answers (1)

greg-449
greg-449

Reputation: 111142

createContextMenu requires a Control as its argument, you are passing the button text String. The setMenu method also belongs to Control. So:

MenuManager menuManager = new MenuManager();
Menu menu = menuManager.createContextMenu(btn);

btn.setMenu(menu);

getSite().registerContextMenu(menuManager, btn);

Button does not implement ISelectionProvider so you will have to write one if you want to use the button as the selection provider.

Note that a part can only register one context menu like this and there can only be one selection provider for a part.

If you want context menu contributions from other parts of Eclipe to be added to the menu you must add the line:

menuManager.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));

Upvotes: 2

Related Questions