You most commonly use dispose in a windowClosing event handler for a JFrame or Frame.
Java can automatically garbage collect stranded objects created by Java, but it needs this help arranging to recycle resources inside the brain-damaged native GUI. This does not delete the current Container itself, e. g. dispose for a Panel would remove the panel from the screen, and would discard all the paraphernalia needed to put the panel on the screen, but all the panel fields would still be intact.
It is wise to arrange to dispose of any unneeded Graphics objects you obtained with getGraphics.
If you just hide frames rather than disposing of them, they can be made visible again more quickly. On the other hand, if you are truly finished with a frame, the associated GUI peer resources the invisible frame is holding onto won’t be recycled. Rule of thumb, use dispose, and only if you need to fine tune, sometimes use setVisible( false ) instead for free standing Frames, JFrames and Windows. use setVisible( false ) for everything else.
dispose cannot be counted on to do built-in setVisible( false ). Do your own setVisible( false ) first. I have not experimented, but I doubt that dispose removes children. When disposing Frames, you must disconnect any off-frame Listeners and any references to the Frame, if you want the Frame itself to be garbage collected.
Image.flush() should really be called Image.dispose().
This page is posted
Optional Replicator mirror
Your face IP:[22.214.171.124]
You are visitor number|