• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

URGENT :- Creating JTree

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the problem..... First you need to create nodes and the last step should be creating JTree

Kinu
 
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic