简体   繁体   中英

Creating JTree using a HashMap

I'm attempting to create a JTree to display in my application that will display the internal data structure of my application. I use a HashTable to initialize all my objects that utilizes each objects index that you can retrieve using getIndex(). I'm trying to create a JTree using just the names of each objects to be displayed

I cant figure out exactly how to get adding Nodes correct. I create a new node but I cannot reference it as a parent Node.

Here I create my JTree and root Node:

JTree                                           tree;
    DefaultMutableTreeNode                          treeRoot = new DefaultMutableTreeNode( "Cave" );
    tree                                            = new JTree( treeRoot );

Here I create a node representing a Party object that is a new child of the root node:

        //DefaultMutableTreeNode newParty.getIndex();  //!Does not work
        //DefaultMutableTreeNode newParty.getIndex() = new DefaultMutableTreeNode( newParty.getName() , treeRoot ); //does not work
        addNode( newParty.getName(), treeRoot);

Bellow is the addNode Method:

static DefaultMutableTreeNode addNode ( String Asset, DefaultMutableTreeNode parent ){
    DefaultMutableTreeNode newNode = new DefaultMutableTreeNode( Asset );
    parent.add( newNode );
    return newNode;
}

And here I attempt to add a Creature node that is a child of a specific Party Node:

findParty =             ( newCreature.getParty() );// == index of parent Node in HashMap
                    if (findParty == 0 ) {
                        SorcerersCave.theCave.addCreature( newCreature );
                        addNode ( newCreature.getName(), treeRoot );
                    }else {
                        ((Party)gameAssets.get( findParty )).addMember( newCreature );
                        gameAssets.put(  newCreature .getIndex(), newCreature );
                        addNode ( newCreature.getName(), ((Party)gameAssets.get( findParty ).getName() ) );

Ideally I would like to create nodes represented by each objects int index, then I could add that node to the tree with the correct text displayed and parent. As you can see I try to do this several times but nothing seems to really stick.

Was thinking about it all wrong!

Instead of creating a JTree in the method that is instantiating all of my classes is the wrong way to go. You should create your JTree using the internal methods and constructors of each object.

Here in the "Cave" object I create the root of the Node. (The cave gets constructed first and will contain the 4 "Party" objects in an ArrayList)

class Cave {
    public ArrayList< Party >                   parties = new ArrayList < Party > ();
    ...
    public JTree                                theTree;
    public DefaultMutableTreeNode               treeRoot = new DefaultMutableTreeNode( "Cave" );
    ...

    public Cave (){//Constructs Cave Object
        theTree                                            = new JTree( treeRoot );
    }
}//End Cave

Then when creating the "Object" I construct it and set the parent node as the root node "treeRoot":

class Party extends Assets implements SearchableByName {
    ArrayList < Creature >                  members = new ArrayList < Creature > ();
    DefaultMutableTreeNode                  pNode;
    public void addMember ( Creature newMember ) {
    members.add ( newMember );
        newMember.cNode = addNode( newMember.getName(), this.pNode );
        newMember.tNode = addNode( "Treasures", newMember.cNode );//Each Creature hold Treasures
        newMember.aNode = addNode( "Artifacts", newMember.cNode );//Each Creature holds Artifacts
    }
    public Party ( String[] x ){//Party Consstructor
        index =                             Integer.parseInt( x[ 0 ] );
        name =                              x[ 1 ];
        pNode =                             addNode( this.getName(), SorcerersCave.theCave.treeRoot );//Creates new Parent node with treeNode as its parent
    }
}//End Party

And finally I add the treasure and artifacts to their respective parent node when I add them to the ArrayLists belonging to the creatures that hold them.

public void addItem ( Treasure newItem ) {
    inventory.add ( newItem );
    addNode ( newItem.getType(), tNode );//Add Treasure parent Node labeled "Treasures"
}
public void addArtifact ( Artifact newItem ) {
    artifacts.add ( newItem );
    addNode ( newItem.getName(), aNode );//Add Artifact Node to parent Node labeled "Artifacts"
}

Lesson learned: When it comes down to it where each node places in my object hierarchy is an attribute of the object it self and should be described / established within each respective class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM