This article shows how to add nested menu items to a menu in an Eclipse RCP application and builds off of a clean Hello World Eclipse RCP Application. As shown in the article Add a Menu to an Eclipse RCP Application, it is very easy to add a File menu containing the Exit action. Here the Exit action as well as About and Preferences are added to a submenu called Expand in the File menu to demonstrate nested menu items.Step 0: Create a HelloWorld RCP application.
Step 1: Add the nested menu actions. Open up the ApplicationActionBarAdvisor class and add the three About, Preferences, and Exit actions as private fields. In makeactions(), define the actions and register them. In the fillMenuBar() method, create two instances of MenuManager, one for the File and one for Expand. Add the three actions to the Expand MenuManager. A Separator is added before the Exit action for the sake of demonstrating this nice menu organizing feature. Next add the Expand MenuManager to the File MenuManager. Finally add the File MenuManager to the menubar.
package com.blogspot.obscuredclarity.nestedmenu;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
private IWorkbenchAction aboutAction;
private IWorkbenchAction preferencesAction;
private IWorkbenchAction exitAction;
public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
super(configurer);
}
protected void makeActions(IWorkbenchWindow window) {
//ActionFactory Actions, the ActionFactory defines a set of common actions and can be used in our application.
aboutAction = ActionFactory.ABOUT.create(window);
register(aboutAction); //register the action so it is deleted when the Workbench window is closed
preferencesAction = ActionFactory.PREFERENCES.create(window);
register(preferencesAction);
exitAction = ActionFactory.QUIT.create(window);
register(exitAction);
}
protected void fillMenuBar(IMenuManager menuBar) {
MenuManager fileMenu = new MenuManager("&File", "file"); //create a menuManager to take care of all submenus in "File"
MenuManager expandingMenu = new MenuManager("&Expand", "expand");
expandingMenu.add(aboutAction); //Add the "about" action
expandingMenu.add(preferencesAction); //Add the "preferences" action
expandingMenu.add(new Separator()); //Add a horizontal separator
expandingMenu.add(exitAction); //Add the "exit" action
fileMenu.add(expandingMenu); //Add the expanding menu to the "File" menu
menuBar.add(fileMenu); //Add the "File" menu to the menuBar
}
}
Step 2: Run the application and test if everything worked. Your application should now have a new menu containing a group of actions nested in a parent menu:

Piece of cake!
<--- Previous - Verify User Intent Before Closing an Eclipse RCP Application
---> Next - Add a Custom Menu Action to an Eclipse RCP Application
Also see: Eclipse RCP Tutorial Table of Contents


































