Is it possible to close parent element of a component .. probably a JFrame
/JInternalFrame
from a JPanel? if yes how it can be done?
Asked
Active
Viewed 1,251 times
2

Andrew Thompson
- 168,117
- 40
- 217
- 433

c.pramod
- 606
- 1
- 8
- 22
2 Answers
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 );
}
}