remove sub components of container which matches regular expression - Java Swing

Java examples for Swing:JComponent

Description

remove sub components of container which matches regular expression

Demo Code


//package com.java2s;

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

public class Main {
    /**/*from   w  ww .  j av a2 s .  co  m*/
     * remove sub components of container which matches regular expression
     * @param parent
     * @param nameRegex
     */
    public static void removeComponentsByName(Container parent,
            String nameRegex) {
        Component[] components = parent.getComponents();
        for (Component comp : components) {
            if (comp.getName() != null && comp.getName().matches(nameRegex))
                parent.remove(comp);
        }
    }
}

Related Tutorials