Java/Swing: Obtain Window/JFrame from inside a JPanel

前端 未结 4 985
不知归路
不知归路 2020-11-30 00:59

How can I get the JFrame in which a JPanel is living?

My current solution is to ask the panel for it\'s parent (and so on) until I find a Window:

Con         


        
4条回答
  •  迷失自我
    2020-11-30 01:50

    You could use SwingUtilities.getWindowAncestor(...) method that will return a Window that you could cast to your top level type.

    JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this);
    

提交回复
热议问题