// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;

public class PanelWalk extends JFrame implements ActionListener {
    public PanelWalk() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new BorderLayout());
        setName("My frame");
        JPanel p1 = new JPanel();
        p1.setName("My panel #1");
        p1.setLayout(new BorderLayout());
        JPanel p2 = new JPanel();
        p2.setName("My panel #2");
        p2.setLayout(new BorderLayout());
        JButton btn = new JButton("Check");
        btn.setName("My button");
        btn.addActionListener(this);
        p2.add(btn, BorderLayout.CENTER);
        p1.add(p2, BorderLayout.CENTER);
        getContentPane().add(p1, BorderLayout.CENTER);
        pack();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton btn = (JButton)e.getSource();
        JPanel p2 = (JPanel)btn.getParent();
        JPanel p1 = (JPanel)p2.getParent();
        JPanel cp = (JPanel)p1.getParent();
        JLayeredPane lp = (JLayeredPane)cp.getParent();
        JRootPane rp = (JRootPane)lp.getParent();
        JFrame f = (JFrame)rp.getParent();
        String stack = btn.getClass().getName() + " : " + btn.getName() + "\r\n" +
                       p2.getClass().getName() + " : " + p2.getName() + "\r\n" +
                       p1.getClass().getName() + " : " + p1.getName() + "\r\n" +
                       cp.getClass().getName() + " : " + cp.getName() + "\r\n" +
                       lp.getClass().getName() + " : " + lp.getName() + "\r\n" +
                       rp.getClass().getName() + " : " + rp.getName() + "\r\n" +
                       f.getClass().getName() + " : " + f.getName();
        JOptionPane.showMessageDialog(null, stack);
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new PanelWalk();
                f.setVisible(true);
            }
        });
    }
}
