/** * Add a JMenuItem (or JMenu) to the end of this JMenu. * * @return the JMenuItem that was added. */ public JMenuItem add(JMenuItem item_) { _menuItems.add(item_); if (item_ instanceof JMenu) { ((JMenu) item_).setParentMenu(this); } return item_; }
/** * Displays this menu's popup menu if the specified value is true; hides the menu if it is false. */ public void setPopupMenuVisible(boolean visible_) { _popupMenuVisible = visible_; if (!visible_) { _popup.hide(); return; } if (_popup == null) _popup = new JPopupMenu(_menuItems); Point p; if (!this.isTopLevelMenu()) { /* If this menu is a submenu (i.e. it is not a direct * child of the menubar), check if there is enough * space on the right hand side of the parent menu. * If there is not enough space, position it on the * left of the parent menu. */ JMenu parentmenu = (JMenu) getParentMenu(); JPopupMenu parentpopup = parentmenu.getPopupMenu(); p = parentpopup.getLocation(); int verticalOffset = parentpopup.getComponentIndex(this); _popup.setInvoker(parentpopup); int parentwidth = parentpopup.getSize().width; Toolkit term = Toolkit.getDefaultToolkit(); if (p.x + parentwidth + _popup.getWidth() < term.getScreenColumns()) { _popup.setLocation(p.addOffset(parentwidth - 1, verticalOffset)); } else { _popup.setLocation(p.addOffset(-_popup.getWidth() + 1, verticalOffset)); } } else { JMenuBar parentMenuBar = (JMenuBar) getParent(); p = parentMenuBar.getPopupMenuLocation(this); _popup.setInvoker(parentMenuBar); _popup.setLocation(p); } _popup.show(); }