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.
SwingUtilities.windowForComponent(...);
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 )
{
//...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With