Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing the level of hierarchy in JTree based on a condition 1

Status
Not open for further replies.

pjeypal

Programmer
Apr 17, 2006
36
US
hi,

Hi,

I have a dynamic JTree built from records in an oracle database.I created the tree with two fields location and description displayed side by side on the tree, something like this :

Root
|
location :: description
.
.
location4000 :: description4000

There is a third field parent in the table.For a parent value there are several child records.When the parent value changes a new level should be created in the hierarchy. Something like this

Root
|
location ::description parent 1
|
location1::description1 parent 1
|
location3::description3 parent 2
|
location4::description4 parent3

Any suggestions on which class to use in this case.

Priya
 
Do the childs change in Database only, by an user, editing them, or both?

From your design, I read the Nodes are sorted, and 'parent x' means the parent is Node in Row x.

Let's have a bigger tree:
Code:
 r 0
|
r 1 p 0
|
r 2 p 0
      |
      r 3 p 2
             |
             r 4 p 3
|
r 5 p 0
|
r 6 p 0

Now I change r3p2 to have parent r5p0
reordered without renaming:
Code:
 r 0
|
r 1 p 0
|
r 2 p 0
|
r 5 p 0
      |
      r 3 p 2
             |
             r 4 p 3
|
r 6 p 0
Well - renaming it:
Code:
 r 0
|
r 1 p 0
|
r 2 p 0
|
r 3 p 0
      |
      r 4 p 3
             |
             r 5 p 4
|
r 6 p 0
The usual code looks something like this:
Code:
 public class Node
{
	private Node parent; // null if root
	private String description;
	private String location;
	public Node (Node parent, String description /*more stuff */)
	{
		this.parent = parent;
		/*more stuff */
	}
	/*more stuff */
}

seeking a job as java-programmer in Berlin:
 
Hi,

Thanks for the tips.Thi thing looks bit complicated to me.I've not worked much with JTree. The structure of backend table is like this (just an example)

location description parent
A1 xyz null
A2 abc C2
B5 axy C2
C4 pqr C2
A7 out C4
A8 iot C4
S8 hto C4
C2 abz C6
C6 ttt C9

In the above example i need to display all locations without parent directly under main root.A2 and B5 have the same parent C2, so they are grouped together but at the same time C2's parent is C6.so A2,B5 and C2 are under C6.Again C6 has parent C9.So again all these records belong to that group.There are about 4000 records and this level of hierarchy can go upto 9-10 levels deep.

If you want i can send the XLS sheet for the table. I dont have an idea on how to design the Jtree for this hierarchy.

Thanks for all your help

Peiya
 
Did you work with trees at all?

Doesn't oracle provide a way to get all nodes in order?

If not, my first idea is to store all values (loc., descr. par.) in a collection col-1.
We know: root has no parent.
We search for an object which has parent==null, make it the root, and remove the object from the collection col-1.
We search all objects who's parent is root and add it to the root node, remove them from the collection col-1.
For every new node we search matching childs which we remove here and add there until col-1 is empty.

If the speed is too low you could add the objects and nodes to HashMaps for faster access, but I don't think that's neccessary, for just 4000 records.
If 3 childs is an average number of child-nodes, we calculate: 3^8=6561 - 8 itertations to find every child/ the Tree is about 8 levels deep.

I weakly remember something about big-trees; I guess while refreshing the gui - search for that, if you get performance problems.

To simulate such an JTree I could read the directory-structure of my harddrive, shuffle the entries and try to display them in order.
If I would like to get the data, I would prefer proper sql-insert-statements, perhaps a csv-file, surely not a xls-File.

--
P.S.: "...order by parent" should at least be possible.

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top