Results 1 to 4 of 4
  1. #1
    xlomo is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

    Default How to Create Independent copies of a TreeModel

    I have a Jtree component and i need to create a copy of it's model so that i can edit it individually, So I use the following code :

    TreeModel PreviewModel = this.XMLtree.getModel();

    Where XMLtree a Jtree component.

    My problem is that, when a change is made to the PreviewModel, the same change is applied to XMLtree model as well, It's like i created a pointer to the XMLtree model and not a new TreeModel.

    So, can anyone explain to me how to create an independent exact copy of the TreeModel ?

    I'm in desperate need of.... HEEEEEEELPPPPPPPP !

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default

    Does the concrete class for your TreeModel have either a copy constructor or a copy() method?

  3. #3
    xlomo is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

    Default

    None I can find.

  4. #4
    xlomo is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

    Default

    Since I started this topic and now i have it's solution i think i should post it.

    Java Code:
        private DefaultMutableTreeNode CopyNode(DefaultMutableTreeNode OriginNode){
            DefaultMutableTreeNode Copy = new DefaultMutableTreeNode(OriginNode.toString());
            if(OriginNode.isLeaf()){
                return Copy;
            }else{
                int cc = OriginNode.getChildCount();
                for(int i=0;i<cc;i++){
                    Copy.add(CopyNode((DefaultMutableTreeNode)OriginNode.getChildAt(i)));
                }
                return Copy;
            }
        }
    I made this function that creates an exact copy of the given node and it's children. So Copy the root of the model you want and create a new model with the copied root.
    Last edited by Fubarable; 05-22-2011 at 06:56 PM. Reason: code tags added

Similar Threads

  1. Independent Study at College
    By Sno in forum Forum Lobby
    Replies: 2
    Last Post: 10-13-2010, 05:21 PM
  2. Replies: 4
    Last Post: 09-10-2009, 01:40 PM
  3. Why and how Java is a Platform Independent?
    By gopivista in forum New To Java
    Replies: 7
    Last Post: 09-01-2009, 04:34 PM
  4. PS Prinitng Issue - Copies not working
    By nvngyl in forum Advanced Java
    Replies: 1
    Last Post: 08-12-2009, 07:30 PM
  5. Preventing local copies of fields
    By Steve11235 in forum Threads and Synchronization
    Replies: 2
    Last Post: 08-05-2009, 05:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •