/** Override of JMenu::add(Action); */ public JMenuItem add(Action a) { // Actions do not have a getPreferredSize call // Therefore, the only way to determine the desired height // of an action to be inserted is to forcibly insert // it and then ask the resulting menu item that // is returned for its preferred height. Then based // on that height, determine if we can add it to the // base menu or if we must JMenuItem retVal = null; // Forcibly insert the item into the menu JMenuItem tempMenuItem = super.add(a); // super.remove(tempMenuItem); // Determine if we can insert this into the primary menu // or if we must insert into the more menu. // Use locals for convenient reference points when debugging double preferredHeight = getPreferredSize().getHeight(); double menuItemHeight = tempMenuItem.getPreferredSize().getHeight(); if ((preferredHeight + menuItemHeight) < maximumHeight) { retVal = tempMenuItem; } else { // Create the more menu if necessary super.remove(tempMenuItem); createSubMoreMenu(); // Add item to the More Menu. retVal = moreMenu.add(a); } return retVal; }
/** Override of JMenu::add(JMenuItem) */ public JMenuItem add(JMenuItem menuItem) { JMenuItem retVal = null; // Use locals for a convenient reference point // to check when debugging. double menuItemSize = menuItem.getPreferredSize().getHeight(); if ((myHeight + menuItemSize) < maximumHeight) { retVal = super.add(menuItem); myHeight += menuItemSize; } else { // Create the more menu if necessary createSubMoreMenu(); retVal = moreMenu.add(menuItem); } return retVal; }
/** Override of JMenuItem::add(string) */ public JMenuItem add(String string) { // Strings do not have a getPreferredSize call // Therefore, the only way to determine the height // needed is to forcibly insert the item, then remove it. JMenuItem retVal = null; // Use locals for a convenient reference point when // debugging double menuItemHeight = 0; JMenuItem tempMenuItem = super.add(string); if (tempMenuItem != null) { menuItemHeight = tempMenuItem.getPreferredSize().getHeight(); } super.remove(tempMenuItem); if ((myHeight + menuItemHeight) < maximumHeight) { retVal = super.add(string); } else { createSubMoreMenu(); retVal = moreMenu.add(string); } return retVal; }