How to get parent for JDialog from JPanel
java, jdialog, jframe, swing
Solution
Every JComponent supports the Method getParent(). As the name of the method says, it returns you a reference to the parent of this component. Since JDialog, JPanel, JFrame etc. are Subclasses of JComponent, you can use it in your case. But be aware that you have to do a type cast, e.g. :
`JFrame parentFrame = (JFrame) myContenPane.getParent()`
And depending on your layout, you may have to call getParent() multiple times, which is quite ugly.
Hope this helps.
Problem
In my Swing app. I have a `JFrame` with few `JPanels`. One of it I use for placing another panels. And one of these - another panel - calls a `JDialog`. Constructor of dialog accepts `Frame`, `String` and `Boolean` as parameters. My problem is how to get parent (which is frame) from this panel? `SwingUtilities.windowForComponent(...)` and `SwingUtilities.getWindowAncestor(...)` do not work in my case. Constructor with no parameters is not an option.