This discussion is archived
7 Replies Latest reply: Apr 3, 2008 8:15 AM by 807601 RSS

DefaultTreeModel..clone()?

807601 Newbie
Currently Being Moderated
Hey all,

is it possible to make a clone of a DefaultTreeModel?
i need this because i want to keep track of older DefaultTreeModels with the old data.

thanks in advanced

Greetz

Grad_
  • 1. Re: DefaultTreeModel..clone()?
    807601 Newbie
    Currently Being Moderated
    You should learn how to use the API
    -Google "java 6 defaulttreemodel" (assuming you're using java 6, use 1.5 or 1.4 or whatever if necessary)
    -Click first result
    -Search the page for 'clone'
  • 2. Re: DefaultTreeModel..clone()?
    807601 Newbie
    Currently Being Moderated
    Already did that,

    but isn't there an easier way than:
    DefaultTreeModel myDefaultTreeModel = new DefaultTreeModel();

    //
    add some nodes
    //

    Object o = myDefaultTreeModel;
    DefaultTreeModel newDefaultTreeModel = (DefaultTreeModel)o.clone();

    greetz
  • 3. Re: DefaultTreeModel..clone()?
    807601 Newbie
    Currently Being Moderated
    If you have a DefaultTreeModel object, just call clone() on it. What's so complicated about that?
  • 4. Re: DefaultTreeModel..clone()?
    807601 Newbie
    Currently Being Moderated
    because Eclipse says:

    The method clone() from the type Object is not visible

    when i call:
    DefaultTreeModel model = new DefaultTreeModel();
    DefaultTreeModel model2 = (DefaultTreeModel)model.clone();
  • 5. Re: DefaultTreeModel..clone()?
    807601 Newbie
    Currently Being Moderated
    newark wrote:
    If you have a DefaultTreeModel object, just call clone() on it. What's so complicated about that?
    Am I missing something? DefaultTreeModel just has the default implementation of clone inherited from Object. A quick scan through DefaultTreeModel's methods doesn't show any copying methods that I can see. I think the answer may be that you have to roll up your sleeves and write your own tree copying code. Shouldn't be hard.
  • 6. Re: DefaultTreeModel..clone()?
    807601 Newbie
    Currently Being Moderated
    well,
    i wrote my own clone method, but i find it still very odd, since i was taught that everything inherits from Object, but i can't call the DefaultTreeModel.clone().
  • 7. Re: DefaultTreeModel..clone()?
    807601 Newbie
    Currently Being Moderated
    Grad_ wrote:
    well,
    i wrote my own clone method, but i find it still very odd, since i was taught that everything inherits from Object, but i can't call the DefaultTreeModel.clone().
    1. I think you need to review how to clone:

    [http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf]

    2. I don't think you should be subclassing DefaultTreeModel! Just write a routine that copies a tree.