Here you can find the source of tearDownMenu(JMenu menu)
public static void tearDownMenu(JMenu menu)
//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(); } }