问题
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.
回答1:
SwingUtilities.windowForComponent(...);
回答2:
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 )
{
//...
}
来源:https://stackoverflow.com/questions/2660943/how-to-get-the-top-level-container-of-a-jcomponent