Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 605
  • Last Modified:

JTree using userObject(). Fetching data from the database

I am trying to make a JTree fetching my userObejct(a Java Bean ) from the database. I have around 3850 records consisting of following variables

  nodeName
  nodeID
  nodeParentID

I have wrapped this and sent this to the client in a hashmap.

Currently i am making my JTree for 2 levels and on subsequent clicks loading data as required.

Now I have a serious concern, I want to use the breathFirst of teh root node. In my current scenario it returns only three nodes which are basically its children.
But I want it to give the entire fetch I have made from
the database.
How can I built the tree in the beginning of loading in one shot. That is all the instances of DefaultMutableTreeNode class are built in one shot in the begging.

Thanx in advance. If I am not clear do ask me for the same.

Regards,

dumdum420
0
dumdum420
Asked:
dumdum420
  • 4
  • 3
1 Solution
 
heyhey_Commented:
1. load all db records and create DefaultMutableTreeNode for each of them. put all id<->node pairs in a Hashtable.
2. Enumerate all elements from Hashtable, for each node, get it's parent from Hashtable and use
.add(MutableTreeNode newChild)
to add it as child.
3. create new DefaultTreeModel with root node.
0
 
dumdum420Author Commented:
Thanx Hey hey,

I got your point put here is a pilot just tell me how to do it programatically :

This Bean construciton is basically a loop and following two variables telling the parent child relationship are set to relevant fields.

HashMap m = new hashMap();

// This is the root node in the hashmap
Bean b = new Bean();
b.setID("1");
b.setParentID(null);
m.add(b);

Bean b = new Bean();
b.setID("2");
b.setParentID("1");

Bean b = new Bean();
b.setID("3");
b.setParentID("1");

Bean b = new Bean();
b.setID("4");
b.setParentID("1");

Bean b = new Bean();
b.setID("5");
b.setParentID("2");

Bean b = new Bean();
b.setID("6");
b.setParentID("2");

Bean b = new Bean();
b.setID("7");
b.setParentID("3");

Bean b = new Bean();
b.setID("8");
b.setParentID("3");


Bean b = new Bean();
b.setID("9");
b.setParentID("5");

Thus you can assume a long map consisting of various beans. Now I want to constrcut my tree from this Map in one shot using DefaultMutableTreeNode class so that if I do a beathFirst on the root node I can get all nodes enumeration.

If I am still not clear please do let me know.

Right now I am using two level construction approcach which basically on every expansion iterates the Map fetches all the nodes which have parent of the node on which exapnsion is done and instance of DefaultMutableTreeNode are constructed.

Regards,

dumdum


Bean b = new Bean();
b.setID();
b.setParentID();



0
 
heyhey_Commented:
i will the needed code later today
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
heyhey_Commented:
1. create Hashtable for nodes,

Hashtable nodes = new Hashtable();


2. for each Bean create a node and add it to hashtable

nodes.add(b.getID(), new DefaultMutableTreeNode(b));

3. build the three structure
 
Enumeration e = hash.elements();
while (e.hasMoreElements())
{
  DefaultMutableTreeNode node = (DefaultMutableTreeNode)e.nextElement();
  Bean b = (Bean)node.getUserObject();
  if (b.getParentID())
  {
    DefaultMutableTreeNode parent = (DefaultMutableTreeNode)hash.get(b.getParentID());
    if (parent == null)
    {
      System.err.println("Parent not found (id: " + b.getID() + ", parent id: " + b.getParentID() + ") !");
    }
    else
    {
      parent.add(node);
    }
  }
}

4. create tree
DefaultMutableTreeNode root = (DefaultMutableTreeNode)hash.get("1");
TreeModel model = new DefaultTreeModel(root);  
JTree tree = new JTree(model);
0
 
dumdum420Author Commented:
This sounds great I am going to try this..

One thing I am spectical about is where exactly and when should I call this loop. I mean to say is that after making the root node or where exactly.

Feed back will really help.

Thanx once again.

dumdum420
0
 
heyhey_Commented:
> One thing I am spectical about is where exactly and when should I call this loop

I do not understand the question.
in point 2. you create TreeNodes with relevant data
in point 3. you build Tree structure from these nodes.
in point 4. you get the root tree node (created in point 2, connecting to other nodes in point 3) and create a tree that uses it.

of course if you already have JTree created you will have to tweak the code from point 4.
0
 
dumdum420Author Commented:
well I have another question which goes exactly in same line with this question infact it is an extension to this question so heyhey how should I post this!!!

It involves eventually sorting in alphbatical order the DefaultMutableTreeNode constructed. Have a look at the seprate posting and suggest me a opossible solution since you only guided me to construct the tree.

Thanx in advance.

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now