Re: can JTree's have multiple roots and models ?



Oliver Wong wrote:
"Thomas Hawtin" <usenet@xxxxxxxxxxxxxxxxx> wrote in message news:43617af8$0$6529$ed2619ec@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx

When layering models like this, you need to be careful with listeners. You should make sure when the model nearest the component has no listeners attached to it, it has no listeners attached to the underlying model(s). If you don't, you might find yourself with a small (or otherwise) memory leak.


I'm guessing the easy fix for this would be to have your "super TreeModel" register listeners on all of its sub TreeModels right away, and then never fiddle with the listeners from there on. Then, the super TreeModel receives an event from one of its sub TreeModels, it just forwards these events to its own registered listeners.

Would there be any problems with this implementation?

The problem is you have (strong) references set up as below (the upper arrows represent the references via listeners typically implemented as inner classes of some description).


              <------                 <------
        JTree         Composite model         Underlying model
              ------>                 ------>

The JTree and composite model are all about a single display. Suppose the underlying model is longer lived. Removing the composite model from the JTree should free up all the required memory, but you end up with:

                                      <------
        JTree         Composite model         Underlying model
                                      ------>


The composite model (and anything it refers to) is not collectible. However if you composite model only adds listeners when it has listeners applied to itself, then you get:



JTree Composite model Underlying model ------>

The composite model is now collectible as it should be.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
.



Relevant Pages

  • Re: can JTrees have multiple roots and models ?
    ... with the super TreeModel mainly dealing with creating the illusion that all of those sub-models are part of a bigger tree. ... You should make sure when the model nearest the component has no listeners attached to it, it has no listeners attached to the underlying model. ...
    (comp.lang.java.gui)
  • Re: can JTrees have multiple roots and models ?
    ... You should make sure when the model nearest the component has no listeners attached to it, it has no listeners attached to the underlying model. ... The observed object, simply needs a list of references to all items subscribing to it; when no one is subscribing to it, that list is empty. ...
    (comp.lang.java.gui)