Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
854 views
in Technique[技术] by (71.8m points)

swing - Creating a submenu in context menu from overridden getActions in Java JHotDraw

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...