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

Jtree with its Modules

 
seema chhetri
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,

I have made one Jtree in which its have 26 modules around when i click on any of them again all 26 modules is coming from that node please see my code and let me know what to do.


Thanks
seema
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I couldn't compile your code because of several missing classes, but your TreeModel code looks really bogus. There are always 2 children (getChildCount), but if the JTree tries to get these it always gets a completely new object. If it then try to get the index of an object, 0 is always returned. And nothing is a leaf, ever.

The following are usual implementations:
- each object has an associated List (or array); let's call it children
- getChildCount(o) returns children.size() (children.length for the array)
- isLeaf(o) returns children.isEmpty() (children.length == 0 for the array)
- getIndexOfChild(o, child) returns children.indexOf(child) (a loop to retrieve the index for the array)
- getChild(o, index) returns children.get(index) (children[index] for the array)

Usually this List (or array) is stored as part of the object (e.g. DefaultMutableTreeNode) or can be constructed using the object.

For instance, a naive (very slow) File tree model (missing a few methods):
 
seema chhetri
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,


thanks for repplying .but the main problem is that i dont want any leaf. I only want root then after this modules no child of these modules.

please tell me what can be possible for this code.


Thanks
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic