I made a JFrame (call it J1
) that includes a JButton. When the button is clicked, a new JFrame (call it J2
) opens.
J1
also has an ArrayList that contains J2
, and all other Jframes that were opened.
J2
is set to DISPOSE_ON_CLOSED
I set for J2
a windowClosed() method. For testing, inside the JFrame I looped threw the ArrayList
, until I get to the current J2
that was just closed, and surprisingly when I do J2.setVisisble(true)
the J2
window returns!
I also checked threw the Task Manager, and saw the though opening a new J2
, will make the overall program consume more RAM, closing each J2
doesn't show much difference on the Task Manager, it doesn't look like any memory was freed. It looks like that the memory consumption is going back to "normal" after a few seconds, so i doubt that is has anything to do directly with the J2
.
I tried printing (System.out.print
) all the ArrayList
content every time a new J2
is initiated, and after opening a window, closing it, and opening a new one, I get the following messgae:
home.ATMmachine[frame0,252,198,620x420,invalid,hidden,layout=java.awt.BorderLayout,title=ATM Machine No.1,resizable,normal,defaultCloseOperation=DISPOSE_ON_CLOSE,rootPane=javax.swing.JRootPane[,9,38,602x373,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]
This means that the J2
wasn't completely deleted!
The JFrame dispose()
method is suppose to
Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be marked as undisplayable.
So am I just wrong, and the frame is being deleted sum time later.
If not, then how can i delete the frame in such a way that referencing to it would be like referencing to null?