CODEXE

Add menu to table

add menu to table

Please call createContextMenu method after creating a table viewer. For TableViewerInput or other codes of creating table viewer, please refer to "Create A Table".

protected void createContextMenu(Viewer viewer) {
	MenuManager contextMenu = new MenuManager("#ViewerMenu"); //$NON-NLS-1$
	contextMenu.setRemoveAllWhenShown(true);
	contextMenu.addMenuListener(new IMenuListener() {
		@Override
		public void menuAboutToShow(IMenuManager manager) {
			StructuredSelection selection = (StructuredSelection) viewer.getSelection();
			if (selection != null) {
				if (selection.getFirstElement() instanceof TableViewerInput) {
					manager.add(new Action("Action 1") {
						@Override
						public void run() {
							System.out.println("Action 1");
						}
					});
					manager.add(new Action("Action 2") {
						@Override
						public void run() {
							System.out.println("Action 2");
						}
					});
				}
			}
		}
	});

	Menu menu = contextMenu.createContextMenu(viewer.getControl());
	viewer.getControl().setMenu(menu);
}