How to get the top level container of a JComponent?

扶醉桌前 提交于 2021-02-07 06:41:02

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!