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

Dani AI

Generated

Brief expert note tied to the thread: a Swing tree node instance can only live in one place in a true tree. In 's example a single map keeps one DefaultMutableTreeNode per name, so calling the add routine a second time with the same name simply reuses (and thus re-parents) that one node — which explains 's observation that "Sub Item 1" only appears once.

Two practical approaches depending on intent:

  • Treat the UI as a tree (best for JTree): create a distinct DefaultMutableTreeNode for each parent-child appearance, but point both nodes at the same domain object as the node’s userObject. That preserves a single logical object while letting it show under multiple parents.
  • Treat the data as a graph/DAG (same child truly has many parents): JTree is the wrong control for a native DAG. Either show cross-references in the tree (duplicate nodes that reference the same model), or build a custom view that renders graph relations.

Minimal pattern to apply when parsing "Child -> Parent1,Parent2":

MyItem item = new MyItem("Interns");            // domain object
DefaultMutableTreeNode n1 = new DefaultMutableTreeNode(item);
DefaultMutableTreeNode n2 = new DefaultMutableTreeNode(item);
DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
model.insertNodeInto(n1, parentNode1, parentNode1.getChildCount());
model.insertNodeInto(n2, parentNode2, parentNode2.getChildCount());

Implementation tips: split parent strings on commas and trim whitespace; keep a map of parentName -> parentNode for fast lookup; keep childName -> List<DefaultMutableTreeNode> if later updates/removals are needed; perform all tree mutations on the Swing EDT; preserve display case while using a case-insensitive lookup key if desired. These changes keep the UI responsive and allow the same logical item to appear under multiple parents without "moving" the node.

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,

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.