Search code examples
javaswingframes

How to close parent frame from JInternalFrame in java?


I want to close the parent frame on the click of a button in JInternalFrame.


Solution

  • Parent frame as the JInternalFrame the button is in or the JFrame or window it is contained in? Use either:

    SwingUtilities.getWindowAncestor(button).dispose()
    

    or

    SwingUtilities.getAncestorOfClass(JInternalFrame.class, button).dispose();
    

    or similar.

    Javadoc: https://docs.oracle.com/javase/8/docs/api/javax/swing/SwingUtilities.html