Java JMenu tearDownMenu(JMenu menu)

Here you can find the source of tearDownMenu(JMenu menu)

Description

Totally rip a menu apart, recursively.

License

BSD License

Declaration

public static void tearDownMenu(JMenu menu) 

Method Source Code

//package com.java2s;
/***//from  w w  w  .j ava  2s  . co  m
 * Copyright (C) 2010 Johan Henriksson
 * This code is under the Endrov / BSD license. See www.endrov.net
 * for the full text and how to cite.
 */

import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.*;

public class Main {
    /**
     * Totally rip a menu apart, recursively. Action listeners are removed in a
     * safe way which guarantees GC can proceed
     */
    public static void tearDownMenu(JMenu menu) {
        Vector<JMenuItem> componentsToRemove = new Vector<JMenuItem>();
        for (int i = 0; i < menu.getItemCount(); i++)
            componentsToRemove.add(menu.getItem(i));
        for (JMenuItem c : componentsToRemove)
            if (c == null)
                ;// Separator
            else if (c instanceof JMenu)
                tearDownMenu((JMenu) c);
            else
                for (ActionListener l : c.getActionListeners())
                    c.removeActionListener(l);
        menu.removeAll();
    }
}

Related

  1. makeMenu(JMenu menu, List menuItems)
  2. menuItem(JMenu parent, String label, Object... attrs)
  3. removeAllSeparators(JMenu menu)
  4. removeTopAndBottomSeparators(JMenu menu)
  5. setChildrenEnabled(JMenu menu, boolean enabled)