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.