• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting All The componts in a JFrame

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there's an example in this thread of using getComponents recursively

https://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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic