Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

import javax.swing.AbstractButton;

import javax.swing.JMenu;

public class Main {
    public static void printChildHierarchy(Component parent) {
        printChildHierarchy(parent, 0);
    }

    private static void printChildHierarchy(Component parent, int level) {
        StringBuilder prefix = new StringBuilder("");
        for (int i = 0; i <= level; i++) {
            prefix.append(" ");
        }
        if (parent instanceof Container) {
            Component[] children = (parent instanceof JMenu) ? ((JMenu) parent).getMenuComponents()
                    : ((Container) parent).getComponents();

            for (int i = 0; i < children.length; ++i) {
                Component child = children[i];
                System.out.println(prefix + " " + child.getName()
                        + (child instanceof AbstractButton ? " A:" + ((AbstractButton) child).getActionCommand()
                                : "")
                        + " = " + child);
                printChildHierarchy(child, level + 1);
            }
        }
    }
}