Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JTree using userObject(). Fetching data from the database

Posted on 2002-07-08
7
Medium Priority
?
600 Views
Last Modified: 2011-09-20
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
Comment
Question by:dumdum420
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 7140018
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
 

Author Comment

by:dumdum420
ID: 7141284
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 7142784
i will the needed code later today
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 16

Accepted Solution

by:
heyhey_ earned 400 total points
ID: 7142793
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
 

Author Comment

by:dumdum420
ID: 7145105
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 7145349
> 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
 

Author Comment

by:dumdum420
ID: 7246758
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question