Java JMenu setChildrenEnabled(JMenu menu, boolean enabled)

Here you can find the source of setChildrenEnabled(JMenu menu, boolean enabled)

Description

Set the enabled state of a JMenu and all its children.

License

Open Source License

Parameter

Parameter Description
menu the JMenu of the children
enabled the enabled flag

Declaration

public static final void setChildrenEnabled(JMenu menu, boolean enabled) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Component;
import java.awt.Container;

import javax.swing.JComponent;
import javax.swing.JMenu;

public class Main {
    /**//from ww w .  ja  va 2s . c om
     * Set the enabled state of a container and all its children.
     *
     * @param  c      the container of the children
     * @param enabled   the enabled flag
     */
    public static final void setChildrenEnabled(Container c, boolean enabled) {

        if (c instanceof JMenu) {
            setChildrenEnabled((JMenu) c, enabled);
            return;
        }

        Component child[] = c.getComponents();
        int num_children = child.length;

        if (c instanceof JComponent) {
            ((JComponent) c).setEnabled(enabled);
        }

        for (int i = 0; i < num_children; i++) {

            if (child[i] instanceof JComponent) {
                ((JComponent) child[i]).setEnabled(enabled);
            }

            if (child[i] instanceof Container) {
                setChildrenEnabled((Container) child[i], enabled);
            }
        }
    }

    /**
     * Set the enabled state of a JMenu and all its children.
     *
     * @param menu     the JMenu of the children
     * @param enabled  the enabled flag
     */
    public static final void setChildrenEnabled(JMenu menu, boolean enabled) {

        Component child[] = menu.getMenuComponents();
        int num_children = child.length;

        Component me = menu.getComponent();
        if (me instanceof JComponent) {
            ((JComponent) me).setEnabled(enabled);
        }

        for (int i = 0; i < num_children; i++) {

            if (child[i] instanceof JComponent) {
                ((JComponent) child[i]).setEnabled(enabled);
            }

            if (child[i] instanceof Container) {
                setChildrenEnabled((Container) child[i], enabled);
            }
        }
    }

    /**
     * Sets the enabled property on a Component.
     *
     * @param  c  the Component to set 
     * @param  flag  the enabled flag
     */
    public static final void setEnabled(Component c, boolean flag) {

        if (c != null) {
            c.setEnabled(flag);
        }
    }
}

Related

  1. limitMenuSize(JMenu menu, String name, int size)
  2. makeMenu(JMenu menu, List menuItems)
  3. menuItem(JMenu parent, String label, Object... attrs)
  4. removeAllSeparators(JMenu menu)
  5. removeTopAndBottomSeparators(JMenu menu)
  6. tearDownMenu(JMenu menu)