Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting All The componts in a JFrame

 
Renjith mohanan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2539
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic