Search code examples
javaswingjframejpaneldispose

jDialog disposed but still in jFrame.getOwnedWindows


I have a JFrame (EC_GUI.main) and then I create several JDialogs to which I add a JPanel, passing the JFrame as owner.

I am now in the process of detecting if any of these JDialogs are opened through the jFrame.getOwnedWindows(), since I can't get them through one of this

EC_GUI.main.getComponentCount() 1
javax.swing.JRootPane[,0,0,755x1005,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=]

EC_GUI.main.getRootPane().getComponentCount() 2
javax.swing.JPanel[null.glassPane,0,0,755x1005,hidden,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777217,maximumSize=,minimumSize=,preferredSize=]
javax.swing.JLayeredPane[null.layeredPane,0,0,755x1005,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=,minimumSize=,preferredSize=,optimizedDrawingPossible=true]

EC_GUI.main.getContentPane().getComponentCount() 1
ec.gui.dialogs.template.EC_BorderPanel[,0,0,755x1005,layout=java.awt.BorderLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.EmptyBorder@44e6097c,flags=9,maximumSize=,minimumSize=,preferredSize=]

But I do get them if I query the ownedWindows

EC_GUI.main.getOwnedWindows().length 1
ec.gui.dialogs.visivilityfield.EC_VrPresetDialog$1[dialog0,385,254,502x511,invalid,hidden,layout=java.awt.BorderLayout,MODELESS,title=Virtual Reality Presets,defaultCloseOperation=DO_NOTHING_ON_CLOSE,rootPane=javax.swing.JRootPane[,0,0,502x511,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]

I noticed that even if I call jDialog.dispose() I still have a reference so if I open and close, let's say 3 times, one of my JDialogs, I get

EC_GUI.main.getOwnedWindows().length 3
ec.gui.dialogs.visivilityfield.EC_VrPresetDialog$1[dialog1,385,254,502x511,invalid,hidden,layout=java.awt.BorderLayout,MODELESS,title=Virtual Reality Presets,defaultCloseOperation=DO_NOTHING_ON_CLOSE,rootPane=javax.swing.JRootPane[,0,0,502x511,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]
ec.gui.dialogs.visivilityfield.EC_VrPresetDialog$1[dialog2,385,254,502x511,invalid,hidden,layout=java.awt.BorderLayout,MODELESS,title=Virtual Reality Presets,defaultCloseOperation=DO_NOTHING_ON_CLOSE,rootPane=javax.swing.JRootPane[,0,0,502x511,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]
ec.gui.dialogs.visivilityfield.EC_VrPresetDialog$1[dialog3,385,254,502x511,invalid,hidden,layout=java.awt.BorderLayout,MODELESS,title=Virtual Reality Presets,defaultCloseOperation=DO_NOTHING_ON_CLOSE,rootPane=javax.swing.JRootPane[,0,0,502x511,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]

Is this normal?


Solution

  • You read through the JavaDocs you will not that, when configured correctly, dispose will release the native resources that the window is using, this does not "dispose" of the object, you can re-show the window by calling setVisible.

    You could check the isDisplayable (which will let you know if the window still has a reference to it's native peer) or the more reliable isVisible option...

    If the dialog has no other strong references, it will, eventually be garbage collected.