James Riden
James Riden

Reputation: 101

How to populate JTree dynamically?

I am working on a project that requires the use of a tree data structure. Having done some researches I found that Java JTree would be of great use to my project, however I stumbled upon a problem that I spent a week fixing but to no avail.

Here's the problem, in order to create a new node, a DefaultMutableTreeNode has to be instantiated and I'm not sure how that can be done in a loop. Normally when we want to create a new node in JTree, we would firstly declare the nodes in the following way:

DefaultMutableTreeNode parent = new DefaultMutableTreeNode("This is parent node.");
DefaultMutableTreeNode child = new DefaultMutableTreeNode("This is child node.");

Then, in order to add/link child node to parent node, we would do the following:

parent.add(child);

I have two arraylists containing the parent nodes and child nodes, they correspond to each other in a parent-child relationship, meaning that arraylistParent.get(x) will always be the parent of arraylistChild.get(x).

I was thinking that by using a for loop, I could do:

for (int x = 0; x < arraylistParent.size(); x++){
    parent.add(new DefaultMutableTreeNode(arraylistChild.get(x)));
}

This could only work in a flat hierarchy tree, which obviously isn't the case for me. I will have different parent nodes in arrayListParent which I need to check before adding the child nodes, but once again not all child nodes have the same, single parent node. My arraylists probably contain something like this:

arraylistParent = [root, p1, p2, p2, p3, p1]
arraylistChild = [p1, p2, p5, p3, p4, p5]

and I want to generate the tree structure like this:

root
  ..p1
    ..p2
      ..p5          
      ..p3
        ..p4
    ..p5

Obviously I can throw a few if loops inside to check whether parent.getUserObject() is the same as arraylistParent.get(x) but only those that match the string "This is parent node" will be checked and added.

If there's a different, non-existent parent found in the loop, a new parent needs to be created and this is the tricky part I don't know how to solve, as I have no idea how to have the loop automatically create a new instance of parent for the child node.

Upvotes: 1

Views: 2440

Answers (1)

tucuxi
tucuxi

Reputation: 17935

Let us assume your node-datums are Strings (that is, the contents of your ArrayLists are Strings). The explanation will work with any other Java object, though. We will call these contents 'nodes', as distinct from 'tree nodes'.

You can create a HashMap<String, DefautMutableTreeNode> m, so that m.get(node) will return the corresponding TreeNode.

Now, you have to iterate over the parents' arraylist. At each position, you will have a parent and a child. Look them up in the map. If the parent TreeNode does not exist, you will first have to create it. If the child TreeNode does not exist, the same will apply. You should make sure that the map is updated after creating any TreeNodes. Finally, you will mark the child as a child of the parent.

At the end of this algorithm, you should look through all the nodes in the child array. The one without any parents is the root. This is the root of your tree.

Upvotes: 3

Related Questions