user3603473
user3603473

Reputation:

Get All jcomponents and their location on a jframe

I want a jframe on which i am adding jcomponents dynamically and on a button click i want to know about all the available components and their location(or sequence). I am using this method

Component[] components=getContentPane().getComponents();
            components.toString();

but i am getting a string which contain too much info about components I only need their names(not must variable name their class name means JLabel lbl; so I want JLabel ) and their sequence.

It don't know how to extract this info from the string.

Upvotes: 1

Views: 315

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347324

All components have a getLocation method, simply take your array of components and loop over them...

for (Component comp : getContentPane().getComponents()) {
    System.out.println(comp.getLocation());
}

The getLocation method will return a java.awt.Point which contains the x/y coordinates of the component.

Remember, getComponents will only return the components within the immediate container, it does not do a recursive search on it's own...

Upvotes: 1

Related Questions