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().
available on the web at:
optional Replicator mirror
Please email your feedback for publication, letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : . If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, especially when sending an ad-hominem attack, a rant composed mainly of obscenities or a death threat, please quote the offending passage and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. I can’t very well fix erroneous or ambiguous text if I can’t find it.
Your face IP:[220.127.116.11]
|Feedback||You are visitor number 63,655.|