Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 *    Geotoolkit - An Open Source Java GIS Toolkit
 *    http://www.geotoolkit.org
 *
 *    (C) 2013 Geomatys
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This 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
 *    Lesser General Public License for more details.
 */

import java.awt.Component;

import java.beans.PropertyChangeListener;
import javax.swing.JComponent;

public class Main {
    /**
     * Remove all children from a JCompontent and remove all
     * PropertyChangeListeners for all children recursively.
     *
     * @param container
     */
    public static void safeRemoveAll(JComponent container) {
        //remove all children PropertyChangeListeners
        if (container.getComponentCount() > 0) {
            for (Component child : container.getComponents()) {
                removeAllPropertyChangeListeners((JComponent) child);
            }
        }
        //remove all children
        container.removeAll();
    }

    /**
     * Recursively remove all PropertyChangeListeners from a component
     * @param container
     */
    public static void removeAllPropertyChangeListeners(JComponent component) {
        if (component.getComponentCount() > 0) {
            for (Component child : component.getComponents()) {
                if (child instanceof JComponent) {
                    removeAllPropertyChangeListeners((JComponent) child);
                }
            }
        }

        PropertyChangeListener[] tmpList = component.getPropertyChangeListeners();
        for (PropertyChangeListener listener : tmpList) {
            component.removePropertyChangeListener(listener);
        }
    }
}