Solved

JTree using userObject(). Fetching data from the database

Posted on 2002-07-08
7
592 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Accepted Solution

by:
heyhey_ earned 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 84
java. non-English characters encoding problem. intellij idea 3 73
Requested array size exceeds VM limit 3 84
servlet example issue 6 29
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

919 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now