1

this question was asked : Disposing JFrame by clicking from an inner JPanel

and people provided answers that i didn't quite understand so the question is what does this actually do :

((Window) getRootPane().getParent()).dispose();

could some one explain it to me?

Community
  • 1
  • 1
M.Helmy
  • 13
  • 7
  • 1
    Is this a problem of programming? IMO it is a problem of understanding. I think there are documentations for the programming language. Read the fine manuals. – jogo Dec 12 '15 at 11:36
  • 1
    The best way to find a JComponent's window is with its [getTopLevelAncestor](https://docs.oracle.com/javase/8/docs/api/javax/swing/JComponent.html#getTopLevelAncestor--) method. The second best way is with [SwingUtilities.getWindowAncestor](https://docs.oracle.com/javase/8/docs/api/javax/swing/SwingUtilities.html#getWindowAncestor-java.awt.Component-). A JRootPane is not always a direct child of the window. – VGR Dec 12 '15 at 18:10

1 Answers1

2

It gets the JPanel in which the JButton is on.
Then it gets the parent of the JPanel, which is the JFrame.
Finally it disposes the JFrame using dispose().

See Root Pane Container for more information

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
Dan
  • 7,286
  • 6
  • 49
  • 114