Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate through all objects in Jframe

I have a simple question. I have a project made with javax.swing.JFrame. I would like to iterate through all the objects that i Have added in the Jframe. Is that possible, how can I do it?

like image 349
Borut Flis Avatar asked Dec 20 '22 22:12

Borut Flis


1 Answers

this will iterate through all components inside your JFrame's contentPane and print them to the console:

public void listAllComponentsIn(Container parent)
{
    for (Component c : parent.getComponents())
    {
        System.out.println(c.toString());

        if (c instanceof Container)
            listAllComponentsIn((Container)c);
    }
}

public static void main(String[] args)
{
    JFrame jframe = new JFrame();

    /* ... */

    listAllComponentsIn(jframe.getContentPane());
}
like image 106
Dan O Avatar answered Jan 31 '23 06:01

Dan O