I am trying to construct a JTree to show the following data. The program should he able to read any data in this format.

ChildNode--------Parent Node
Employee--------Root
Visitors----------Root
Staff------------Employee
Students--------Employee
Graduates-------Students
Undergrads------Students
Interns----------Employee,Visitors

I have this two columns of info stored in two linked list. LinkedList A and LinkedList B. A has all the child nodes and B has all the parent nodes

ChipsNcoke

Recommended Answers

All 3 Replies

Here is a working example of what you need. Basically it takes commands like 'add node' with name "Child1" to node with name "Parent1". The root must always be referred to as "root".

Run the code and read the comments. Should be easy to use/modify.

import javax.swing.*;
import javax.swing.tree.*;
import java.util.*;

class JTreeReader extends JTree
{
	//create map of names to node objects
	private HashMap namesToNodes = new HashMap();
	
	public JTreeReader()
	{
		//create root of tree
		DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
		this.setModel(new DefaultTreeModel(root));
		
		//add to map
		namesToNodes.put("root", root);
	}
	
	//get node object with name
	private DefaultMutableTreeNode getNode(String name)
	{
		//make lowercase
		name = name.toLowerCase();
		
		//get node from map
		DefaultMutableTreeNode node = (DefaultMutableTreeNode)(namesToNodes.get(name));
		
		//if not defined
		if(node == null)
		{
			//create new node
			DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(name);	
			
			//add to map
			namesToNodes.put(name, newNode);
			
			//return new node
			return newNode;
		}
		else
		{
			//return old node
			return node;
		}
	}
	
	public void expandRoot()
	{
		//expand path to root
		expandPath(new TreePath(getNode("root")));
	}
	
	//add first node to second node
	public void addNodeTo(String childNodeName, String parentNodeName)
	{
		//get two nodes
		DefaultMutableTreeNode parentNode = getNode(parentNodeName);
		DefaultMutableTreeNode childNode = getNode(childNodeName);
	
		//add child to parent at the end of child list
		((DefaultTreeModel)getModel()).insertNodeInto(childNode, parentNode, parentNode.getChildCount());	
		
		
	}
	
	public static void main(String[] args)
	{
		//create jframe
		JFrame jf = new JFrame();
		
		//create jtree
		JTreeReader tree = new JTreeReader();
		tree.addNodeTo("Item1","root");
		tree.addNodeTo("Sub Item 1","Item1");
		tree.addNodeTo("Sub Item 2","Item1");
		tree.addNodeTo("Item2","root");
		
		tree.expandRoot();
		
		//show
		jf.getContentPane().add(tree);
		jf.setBounds(0,0,500,500);
		
		jf.setVisible(true);
	}

}

;) For more help, www.NeedProgrammingHelp.com

Just what i needed....Thanks so much !!!!!!!!!!!

ChipsNcoke

I want two ParentNodes to have the same child node....The program reads only one...ie

tree.addNodeTo("Item1","root");
tree.addNodeTo("Item2","root");
[B]tree.addNodeTo("Sub Item 1","Item1");[/B]
tree.addNodeTo("Sub Item 2","Item1");
[B]  tree.addNodeTo("Sub Item 1","Item2");[/B]

Chipsncoke

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.