List of usage examples for javax.swing JMenu add
public Component add(Component c, int index)
From source file:edu.ku.brc.af.core.TaskMgr.java
/** * @param parent/*w w w . j av a 2 s. c o m*/ * @param menuItemDesc * @param menuPath * @param currIndex */ public static void buildMenuTree(final MenuElement parent, final MenuItemDesc menuItemDesc, final String[] menuPath, final int currIndex) { if (currIndex == menuPath.length) { MenuElement me = menuItemDesc.getMenuItem(); if (parent instanceof JMenuBar) { ((JMenuBar) parent).add((JMenu) me); } else if (parent instanceof JMenu) { Container menuComp = ((JMenu) parent).getPopupMenu(); boolean found = false; int insertPos = menuComp.getComponentCount(); // last position JMenu menu = (JMenu) parent; if (menuItemDesc.getPosition() == MenuItemDesc.Position.Top) { insertPos = 0; //log.debug(String.format("0 Inserted: %s - %d", ((JMenuItem)me).getText(), insertPos)); } else if (menuItemDesc.getPosition() == MenuItemDesc.Position.Bottom) { //log.debug(String.format("1 Inserted: %s - %d", ((JMenuItem)me).getText(),insertPos)); } if (menuItemDesc.getPosition() == MenuItemDesc.Position.Before || menuItemDesc.getPosition() == MenuItemDesc.Position.After) { int inx = 0; for (int i = 0; i < menuComp.getComponentCount(); i++) { Component c = menuComp.getComponent(i); if (c instanceof JMenuItem && ((JMenuItem) c).getText().equals(menuItemDesc.getPosMenuItemName())) { insertPos = inx + 1; found = true; break; } inx++; } } if (menuItemDesc.getSepPosition() == MenuItemDesc.Position.Before) { menu.add(new JPopupMenu.Separator(), insertPos); insertPos++; } if (insertPos == -1) { menu.add((JMenuItem) me); } else { menu.add((JMenuItem) me, insertPos); //log.debug(String.format("2 Inserted: %s - %d", ((JMenuItem)me).getText(),insertPos)); found = true; } insertPos++; if (menuItemDesc.getSepPosition() == MenuItemDesc.Position.After) { if (found) { menu.add(new JPopupMenu.Separator(), insertPos); //log.debug(String.format("3 Inserted: Sep - %d", insertPos)); } } } else if (parent instanceof JMenuItem) { JMenuItem mi = (JMenuItem) parent; JPopupMenu menu = (JPopupMenu) mi.getParent(); int pos = 0; for (int i = 0; i < menu.getComponentCount(); i++) { if (mi == menu.getComponent(i)) { pos = i; break; } } //log.debug(String.format("4 Inserted: %s - %d", ((JMenuItem)me).getText(), menuItemDesc.getPosition() == MenuItemDesc.Position.After ? pos + 1 : pos)); menu.insert((JMenuItem) me, menuItemDesc.getPosition() == MenuItemDesc.Position.After ? pos + 1 : pos); } } else { String label = getResourceString(menuPath[currIndex]); MenuElement menuElement = getMenuByName(parent, label); /*log.debug(menuPath[currIndex]+" -> "+label+ " "+menuElement); if (parent instanceof JMenuItem) log.debug(((JMenuItem)parent).getText()); else if (parent instanceof JMenu) log.debug(((JMenu)parent).getText()); else if (parent instanceof JMenuBar) log.debug("MenuBar"); */ if (menuElement == null) { log.error("Couldn't find menu element [" + label + "]");//$NON-NLS-1$ //$NON-NLS-2$ //UIRegistry.showError("Couldn't find menu element ["+label+"]"); //$NON-NLS-1$ //$NON-NLS-2$ } buildMenuTree(menuElement, menuItemDesc, menuPath, currIndex + 1); } }