// 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.Component;
import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class LabelFun extends JFrame {
    public LabelFun() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new GridLayout(2, 2));
        for(int i = 1; i <= 4; i++) {
            JLabel lbl = new JLabel();
            lbl.setName("Label" + i);
            lbl.setText("This is Label #" + i);
            getContentPane().add(lbl);
        }
        pack();
        System.out.println(find(getContentPane(), JLabel.class, "Label2").getText());
    }
    @SuppressWarnings("unchecked")
    public static <T extends Component> T find(Container parent, Class<T> typ, String nam) {
        for(Component c : parent.getComponents()) {
            if(c.getClass().equals(typ) && c.getName().equals(nam)) {
                return (T)c;
            }
        }
        return null;
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new LabelFun();
                f.setVisible(true);
            }
        });
    }
}
