Solved

JTree using userObject(). Fetching data from the database

Posted on 2002-07-08
7
588 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 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

11 Experts available now in Live!

Get 1:1 Help Now