How can I get a JTree to only listen to its TreeModel while it is actually visible to the user or at least to have it unregister itself as soon as the corresponding JFrame is disposed?
As far as I can see the only case a JTree unregisters itself from its model is if you pass it a new model (using setModel(…)).
This causes the tree not being garbage collected if the model is referenced from somewhere else. Example: I implemented a TreeModel using a WatchService to have an always up to date model of a file system tree. Even a single listener on the model requires me to keep the WatchService informing the model about file system changes, so it cannot be garbage collected. So even if the JTree is not visible anymore it is still kept in memory by the model which still needs to get updates from the WatchService, although none of this is necessary any longer.
I guess the best way would be to create a new class extending JTree that does the registering to and unregistering from the model. If so, which methods are called when the component is shown or disposed? Probably addNotify() and removeNotify() are good candidates?