Java JMenu disableMenuIfAllMenuItemsAreDisabled(JMenu menu, boolean bRemoveEmptySubMenus)

Here you can find the source of disableMenuIfAllMenuItemsAreDisabled(JMenu menu, boolean bRemoveEmptySubMenus)

Description

disables (recursively) the given menu if all menu items in the menu (all direct children) are disabled.

License

Open Source License

Parameter

Parameter Description
menu a parameter
bRemoveEmptySubMenus Are submenus without enabled menu items to be removed?

Declaration

public static void disableMenuIfAllMenuItemsAreDisabled(JMenu menu, boolean bRemoveEmptySubMenus) 

Method Source Code


//package com.java2s;
//it under the terms of the GNU Affero General Public License as published by

import java.awt.Component;

import javax.swing.JMenu;
import javax.swing.JMenuItem;

public class Main {
    /**//from   ww  w. j ava  2  s .  c  o  m
     * disables (recursively) the given menu if all menu items in the menu (all direct children)
     * are disabled.
     * @param menu
     * @param bRemoveEmptySubMenus Are submenus without enabled menu items to be removed?
     */
    public static void disableMenuIfAllMenuItemsAreDisabled(JMenu menu, boolean bRemoveEmptySubMenus) {
        int iEnabledMenuItems = 0;
        for (Component comp : menu.getMenuComponents()) {
            if (comp instanceof JMenuItem) {
                final JMenuItem mi = (JMenuItem) comp;
                if (mi instanceof JMenu) {
                    final JMenu menuChild = (JMenu) mi;
                    disableMenuIfAllMenuItemsAreDisabled(menuChild, bRemoveEmptySubMenus);
                    if (bRemoveEmptySubMenus && !mi.isEnabled()) {
                        menu.remove(mi);
                    }
                }
                if (mi.isEnabled()) {
                    ++iEnabledMenuItems;
                }
            }
        }
        if (iEnabledMenuItems == 0) {
            menu.setEnabled(false);
        }
    }
}

Related

  1. createMenuBar(JMenu... menues)
  2. createMenuItem(JMenu parent, String text)
  3. createMenuItemCustomAction(JMenu menu, int type, String text, String command, ImageIcon image, int acceleratorKey, String toolTip, ActionListener action)
  4. createMenuItems(JMenu menu, String[] menuItemNames)
  5. createTooltiplessJMenu(Action action)
  6. enableAll(JMenu menu)
  7. fillFontMenu(JMenu fontMenu)
  8. findMenuComponent(JMenu menu, String menuComponentName, Class componentClass)
  9. findMenuItem(JMenu menu, String menuItemName)