Java Utililty Methods JComponent Container

List of utility methods to do JComponent Container

Description

The list of methods to do JComponent Container are organized into topic(s).

Method

voidgetAllJComponents(Container container, Collection collection)
get All J Components
if (container instanceof JComponent) {
    JComponent c = (JComponent) container;
    collection.add(c);
Component[] children = container.getComponents();
if (children != null) {
    for (int i = 0; i < children.length; i++) {
        Component c = children[i];
...
TgetAncestorOfType(JComponent component, Class type)
get Ancestor Of Type
Container ancestor = component.getParent();
while (ancestor != null) {
    System.out.println("Check  parent : " + ancestor);
    if (type.isInstance(ancestor))
        return (T) ancestor;
    ancestor = ancestor.getParent();
return null;
...
Object[]getAncestorsOfClass(JComponent start, Class theClass)
search of the hierarcy of components for an instance of a given class
List holder = new ArrayList();
Container parent = start.getParent();
while (parent != null) {
    if (theClass.isInstance(parent))
        holder.add(parent);
    parent = parent.getParent();
Object[] ret = (Object[]) Array.newInstance(theClass, holder.size());
...
booleangetBooleanClientProperty(JComponent c, Object property)
get Boolean Client Property
Boolean value = (Boolean) c.getClientProperty(property);
return (value == null) ? false : value;
ListgetChildJComponents(Container container)
get Child J Components
List result = new ArrayList();
Component[] children = container.getComponents();
for (int i = 0; i < children.length; i++) {
    if (children[i] instanceof JComponent) {
        result.add(children[i]);
        result.addAll(getChildJComponents((JComponent) children[i]));
return result;
ClipboardgetClipboard(JComponent c)
Returns the clipboard to use for cut/copy/paste.
return Toolkit.getDefaultToolkit().getSystemClipboard();
JComponentgetComponent(JComponent container, String name)
get Component
assert name != null;
List<Component> componentQueue = new LinkedList<Component>();
componentQueue.add(container);
while (!componentQueue.isEmpty()) {
    Component c = componentQueue.remove(0);
    if (name.equals(c.getName())) {
        return (JComponent) c;
    if (c instanceof Container) {
        for (Component child : ((Container) c).getComponents()) {
            componentQueue.add(child);
return null;
JComponentgetComponent(JComponent container, String name)
get Component
List<Component> componentQueue = new LinkedList<Component>();
componentQueue.add(container);
while (componentQueue.size() > 0) {
    Component c = componentQueue.remove(0);
    String cname = c.getName();
    if (cname != null && cname.equals(name)) {
        return (JComponent) c;
    if (c instanceof Container) {
        for (Component child : ((Container) c).getComponents()) {
            componentQueue.add(child);
return null;
JComponentgetComponentByName(boolean enableDeepSearch, JComponent rootComponent, String name)
get Component By Name
for (int i = 0; i < rootComponent.getComponentCount(); i++) {
    if (handleNull(rootComponent.getComponent(i).getName()).equalsIgnoreCase(name))
        return (JComponent) rootComponent.getComponent(i);
    else {
        if (getComponentByName(enableDeepSearch, (JComponent) rootComponent.getComponent(i), name) == null)
            continue;
        else
            return getComponentByName(enableDeepSearch, (JComponent) rootComponent.getComponent(i), name);
...
JComponent[]getComponentsOfType(JComponent base, Class type)
gather all components which are instances of a specific class
List<JComponent> holder = new ArrayList<JComponent>();
accumulateComponentsOfType(base, type, holder);
JComponent[] ret = (JComponent[]) Array.newInstance(type, holder.size());
holder.toArray(ret);
return ret;