Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the top level container of a JComponent?

Tags:

java

swing

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 monent the top level container of that JToolbar is my JFrame or is its own window, a JDialog.

like image 710
5YrsLaterDBA Avatar asked Apr 18 '10 02:04

5YrsLaterDBA


2 Answers

SwingUtilities.windowForComponent(...);
like image 108
camickr Avatar answered Sep 19 '22 03:09

camickr


If the component has been added to the hierarchy, you can look up the top-level container by recursively calling getParent:

Container c = toolbar;
while ( c.getParent() != null )
{
  c = c.getParent();
}

if ( c instanceof JFrame )
{
  //...
}
like image 39
Ash Avatar answered Sep 21 '22 03:09

Ash