Here you can find the source of disableMenuIfAllMenuItemsAreDisabled(JMenu menu, boolean bRemoveEmptySubMenus)
Parameter | Description |
---|---|
menu | a parameter |
bRemoveEmptySubMenus | Are submenus without enabled menu items to be removed? |
public static void disableMenuIfAllMenuItemsAreDisabled(JMenu menu, boolean bRemoveEmptySubMenus)
//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); } } }