How to get JComponent top-level container?

Is there a way to get the top level container of a component? For example, I have a JToolbar, and I want to know at one point that the top level container of this JToolbar is my JFrame or its own JDialog window.

+7
java swing
source share
2 answers
SwingUtilities.windowForComponent(...); 
+14
source share

If the component is added to the hierarchy, you can find the top-level container by recursively calling getParent :

 Container c = toolbar; while ( c.getParent() != null ) { c = c.getParent(); } if ( c instanceof JFrame ) { //... } 
+6
source share

All Articles