This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

URGENT :- Creating JTree

 
Kinu Kanwar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some data and I want to show it as Tree Structure ... My code is

DefaultMutableTreeNode root = new DefaultMutableTreeNode("ROOT") ;
JTree tree = new JTree(root , true);
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Child 1") ;
node.add( child1 );
DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Child 2") ;
node.add( child2 );
DefaultMutableTreeNode child3 = new DefaultMutableTreeNode("Child 3") ;
node.add( child3 );


But the above code just shows the ROOT ....

My Structure for Tree would be like that

ROOT
Child-1
Child-2
Child-3
Child-3-2
Child-3-2
Child-3-2-1
Child-4

and so one ....

I have simplified my above code so that you can grip my logic in a second ... Please help me ..Its urgent matter

Kinu
 
Kinu Kanwar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the problem..... First you need to create nodes and the last step should be creating JTree

Kinu
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I don't know if that is the problem exactly, but in the above example you are using node.add(), but node is not defined!
 
Kinu Kanwar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct code is

DefaultMutableTreeNode root = new DefaultMutableTreeNode("ROOT") ;
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Child 1") ;
node.add( child1 );
DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Child 2") ;
node.add( child2 );
DefaultMutableTreeNode child3 = new DefaultMutableTreeNode("Child 3") ;
node.add( child3 );


JTree tree = new JTree(root , true); //CAll JTREE CONSTRUCTOIR AT END

Kinu
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well there must be some code missing because there is no variable called 'node' in that code either. But is the problem solved now?
 
Kinu Kanwar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, see the difference b/w code of 1s post and 2nd post. All variables were defined correctly.

Any wayz thanks for your help.

Kinu
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While I see the difference between the two pieces (the constructor called is moved, I know), the reason for the difference is not apparent from the code because as I have said, the use and type of the node variable is still not clear. I see you are calling node.add(), but I still don't see where else it is used in this code.

Anyway, glad the problem is solved.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic