I have created a TreeViewer
using JFace, but now I have to add a right click listener to the nodes. When the right click is done it has to show a menu like:
- Do something
- Do Nothing
- Delete
I am trying to do this as follows, but it is throwing a null pointer exception.
MenuManager menuMgr = new MenuManager();
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener() {
@Override
public void menuAboutToShow(IMenuManager menuManager) {
IContributionManager menu = null;
MenuItem[] items = (MenuItem[]) menu.getItems();
for (int i = 0; i < items.length; i++)
items[i].dispose();
MenuItem itemCollectionFolder = new MenuItem((Menu) menu, SWT.NONE);
itemCollectionFolder.setText("Add Something" );
MenuItem itemNewTestCase = new MenuItem((Menu) menu, SWT.NONE);
itemNewTestCase.setText("Do Nothing" );
}
});
Control tree = treeViewer.getControl();
Menu menu = menuMgr.createContextMenu(tree);
tree.setMenu(menu);
Try this, don't forget to call: createContextMenu(viewer);
To get the selected element of the treeviewer, do this: