2

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;
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
recursion.ninja
  • 5,377
  • 7
  • 46
  • 78
  • Have you tried the [usual approach](http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html)? – trashgod Nov 09 '12 at 20:04
  • 1
    I wish I could use the `JPopupMenu` aproach. I have to use the framework approaches {Homework Requirement}. I'm in a software engineering class with loose requirements about HOW we achieve functionality, so long as we utilize the JHotDraw framework. This means I need to override certain methods, like `getActions`. I'm not even sure if it is possible to create a submenu using this `Action` collection approach of the JHotDraw framework, it just would be an exceedingly convenient and intuitive way to implement the desired functionality. – recursion.ninja Nov 09 '12 at 22:37
  • After many hours of struggling, I determined that a submenu isn't possible given the framework's structure handling context menus. – recursion.ninja Dec 01 '12 at 16:23

1 Answers1

3

Adding submenus is supported.

The class org.jhotdraw.gui.JPopupButton extends JButton to provide the popup menus used throughout the framework. The method add(javax.swing.JMenu submenu) may be used to construct hierarchical submenus. For example, org.jhotdraw.samples.draw.DrawingPanel adds a Zoom submenu to the rightmost popup menu in creationToolbar, along with other miscellaneous editing actions. The method createFontButton() in org.jhotdraw.draw.action.ButtonFactory is another example used to addFontButtonsTo() the editor.

In contrast, right-clicking on the org.jhotdraw.draw.DefaultDrawingView is handled by an instance of javax.swing.JPopupMenu in org.jhotdraw.draw.DelegationSelectionTool, as shown in org.jhotdraw.samples.mini.EditorSample. In particular, the AbstractAction property Actions.SUBMENU_KEY identifies a submenu name. Set DEBUG = true to see the effect. None are presently defined.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045