File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Getting All The componts in a JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Getting All The componts in a JFrame" Watch "Getting All The componts in a JFrame" New topic
Author

Getting All The componts in a JFrame

Renjith mohanan
Ranch Hand

Joined: Jun 19, 2009
Posts: 52
Hi All,
I need to get all the components in a JFrame.

I will explain the issue in a proper way...

" I have a JFrame and added many JPanels in the frame. And the JPanel may contain other JPanel or JButton or JLabel or JTable. I need to get all the components in the frame recursively." (Because I needed to add Listener interface to all the components in the JFrame).

I used the method getComponents() on the frame and then apply this method again on each element of Component [] (after down casting the Component to Container), but this did not work correctly. Is there any way to get all the components from the top level container.

Please Hepl me to fix this..

Thanks In Advance.

Renjith M


Personal E-mail: kalliyoorrenjith@gmail.com
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I used the method getComponents() on the frame and then apply this method again on each element of Component [] (after down casting the Component to Container), but this did not work correctly.

> Please Hepl me to fix this..

can't fix what we can't see
Renjith mohanan
Ranch Hand

Joined: Jun 19, 2009
Posts: 52
Hi,
This is my code




In the above code I am getting all the components in the frame in Component[], components1 and then I use the method getComponents() on each element of the components1 array and then I downcast it to Container and then set MouseListenr to each element.
But the problem is I need to get all the components from the Top Container(that is the frame).
Is there any way to get all elements using any recursive method or such.


Thanks In Advance

Renjith M
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
there's an example in this thread of using getComponents recursively

http://www.coderanch.com/t/344419/Swing-AWT-SWT-JFace/java/deactivate-close-minimise-resizable-from#1993655

it is looking for buttons, but should be easy to modify to suit your needs
Renjith mohanan
Ranch Hand

Joined: Jun 19, 2009
Posts: 52
Thanks Michael,

I got the code and I got all the components of a JFrame.
And my code is given below..



And I called the method setListener(this.getContentPane()) from the constructor of the calss that extends JFrame class

Thanks.
Renjith M
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
Because I needed to add Listener interface to all the components in the JFrame.


No need to add a listener to every component, just use a global listener:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting All The componts in a JFrame