I am overriding the getActions method in the Java based framework JHotDraw, an open source project hosted here. The method getActions creates a right click context menu on Figure in the Drawingview. I can correctly add addidtional context menu options using the code below. I need to know how to add a sub menu to the context menu.
@Override
public Collection<Action> getActions(Point2D.Double p) {
Collection<Action> popupMenu = new ArrayList<Action>();
popupMenu.add(new AbstractAction("add Context Option 1") {
public void actionPerformed(ActionEvent event) {
preformThisMethod("params");
}
});
popupMenu.add(new AbstractAction("add Context Option 2") {
public void actionPerformed(ActionEvent event) {
preformThisMethod("params");
}
});
// How to add a sub menu to the context menu?
return popupMenu;
}
Adding submenus is supported.
The class
org.jhotdraw.gui.JPopupButtonextendsJButtonto provide the popup menus used throughout the framework. The methodadd(javax.swing.JMenu submenu)may be used to construct hierarchical submenus. For example,org.jhotdraw.samples.draw.DrawingPaneladds aZoomsubmenu to the rightmost popup menu increationToolbar, along with other miscellaneous editing actions. The methodcreateFontButton()inorg.jhotdraw.draw.action.ButtonFactoryis another example used toaddFontButtonsTo()the editor.In contrast, right-clicking on the
org.jhotdraw.draw.DefaultDrawingViewis handled by an instance ofjavax.swing.JPopupMenuinorg.jhotdraw.draw.DelegationSelectionTool, as shown inorg.jhotdraw.samples.mini.EditorSample. In particular, theAbstractActionpropertyActions.SUBMENU_KEYidentifies a submenu name. SetDEBUG = trueto see the effect. None are presently defined.