Java tutorial
//package com.java2s; /* * This file is part of WebLookAndFeel library. * * WebLookAndFeel library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * WebLookAndFeel library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with WebLookAndFeel library. If not, see <http://www.gnu.org/licenses/>. */ import java.awt.Component; import java.awt.Container; import java.awt.event.ComponentListener; import java.awt.event.ContainerListener; import java.awt.event.KeyListener; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.event.MouseWheelListener; import java.util.ArrayList; import java.util.List; import javax.swing.JComponent; import javax.swing.event.AncestorListener; public class Main { /** * Destroys container by destroying its childs structure and removing all * listeners. * * @param container * container to destroy */ public static void destroyContainer(final Container container) { for (final Container toDestroy : collectAllContainers(container)) { toDestroy.removeAll(); toDestroy.setLayout(null); for (final MouseListener listener : toDestroy.getMouseListeners()) { toDestroy.removeMouseListener(listener); } for (final MouseMotionListener listener : toDestroy.getMouseMotionListeners()) { toDestroy.removeMouseMotionListener(listener); } for (final MouseWheelListener listener : toDestroy.getMouseWheelListeners()) { toDestroy.removeMouseWheelListener(listener); } for (final KeyListener listener : toDestroy.getKeyListeners()) { toDestroy.removeKeyListener(listener); } for (final ComponentListener listener : toDestroy.getComponentListeners()) { toDestroy.removeComponentListener(listener); } for (final ContainerListener listener : toDestroy.getContainerListeners()) { toDestroy.removeContainerListener(listener); } if (toDestroy instanceof JComponent) { final JComponent jComponent = (JComponent) toDestroy; for (final AncestorListener listener : jComponent.getAncestorListeners()) { jComponent.removeAncestorListener(listener); } } } } /** * Returns list of all sub-containers for this container. * * @param container * container to process * @return list of all sub-containers */ public static List<Container> collectAllContainers(final Container container) { return collectAllContainers(container, new ArrayList<Container>()); } /** * Returns list of all sub-containers for this container. * * @param container * container to process * @param containers * list to collect sub-containers into * @return list of all sub-containers */ public static List<Container> collectAllContainers(final Container container, final List<Container> containers) { containers.add(container); for (final Component component : container.getComponents()) { if (component instanceof Container) { collectAllContainers((Container) component, containers); } } return containers; } }