There are two direct, different methods in SwingUtilities that provide the same functionality (as stated in their Javadoc). They return java.awt.Window , but if you added your panel to the JFrame , you can safely refer it to the JFrame .
Two simple and easy ways:
JFrame f1 = (JFrame) SwingUtilities.windowForComponent(comp); JFrame f2 = (JFrame) SwingUtilities.getWindowAncestor(comp);
For completeness, some other ways:
JFrame f3 = (JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, comp); JFrame f4 = (JFrame) SwingUtilities.getRoot(comp); JFrame f5 = (JFrame) SwingUtilities.getRootPane(comp).getParent();
icza Aug 05 2018-11-14T00: 00Z
source share