2

Is it possible to close parent element of a component .. probably a JFrame/JInternalFrame from a JPanel? if yes how it can be done?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
c.pramod
  • 606
  • 1
  • 8
  • 22

2 Answers2

4

This will help you:

SwingUtilities.windowForComponent(panel).dispose();

or

SwingUtilities.windowForComponent(panel).setVisible(false);
Dan D.
  • 32,246
  • 5
  • 63
  • 79
2

Modified version of this answer

public class CustomPanel extends JPanel {

  private JFrame parent; //Need to pass this reference somehow, constructor or otherwise

  public void closeJFrame() {
    WindowEvent winEvent = new WindowEvent(parent, WindowEvent.WINDOW_CLOSING);
    Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winEvent );
  }
}
Community
  • 1
  • 1
Grambot
  • 4,370
  • 5
  • 28
  • 43