27.04.2013 Views

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Graphics, AWT, Swing I part<br />

Q: Need I to remove all the listeners on all of the components in the Frame when<br />

I close window of the Frame?<br />

I've got a Frame, which has in it a bunch of components, some of which themselves<br />

may be containers. Many of the components in this frame have listeners.<br />

When somebody closes the window of the Frame, I want to completely get rid of the<br />

Frame, so that the garbage collector will later clean it up, freeing it's memory.<br />

However, I haven't yet figured out a way to do this without tracking every single<br />

component by myself. Just calling dispose() on the main Frame doesn't seem to be<br />

good enough. Calling "RemoveAll()", even recursively, doesn't seem to be good<br />

enough.<br />

I *suspect* that the problem may be that I have to remove all the listeners on all of<br />

the components in the Frame. Is there a way to get a list of the listeners out of a<br />

given component? I really don't want to have to track every single button I add to<br />

every single Frame, and every single Listener I add to every single component. If I<br />

have to keep track of all of that, it sort of defeats a lot of the advantages of a<br />

well-defined object oriented system.<br />

Answer: I think you're slightly confused here. When another object registers as a<br />

listener for a component within the frame, it's the component within the frame that is<br />

holding a reference to the external object in its listeners list - not vice versa.<br />

Which means you don't really need to explicitly remove every listener that any other<br />

object registered on components in the frame.<br />

What you need to worry about is however the listeners that the frame itself registered<br />

with some other components that are still alive. If the frame gets disposed without<br />

removing these, the objects that were being listened to will retain a reference to the<br />

frame and this can cause the frame to stay around as long as these objects which<br />

hold these references stay alive.<br />

So look for all Listeners that your frame registered itself as on components external<br />

to itself (should be fairly easy to see since you normally do it in your constructor) and<br />

remove those before disposing off the frame.<br />

--<br />

Kala<br />

Pick your topics, and we'll send you great deals, free<br />

information, and special offers by email from Focalex.<br />

IT Professional General Computer Design and Graphics Linux Freeware/Shareware<br />

Games Intranet Computer Hardware Web Design Computer Software<br />

UNIX Web Software Windows Software Personal Finance Programming Software<br />

<strong>Server</strong>s C/C++ Powerbuilder Perl XML<br />

<strong>Java</strong> SQL Design/Graphics Utilities Handhelds<br />

file:///F|/350_t/350_tips/graphics-I.htm (5 of 6) [2002-02-27 21:18:07]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!