File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Jtree with its Modules Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Jtree with its Modules" Watch "Jtree with its Modules" New topic

Jtree with its Modules

seema chhetri
Ranch Hand

Joined: Feb 17, 2009
Posts: 43
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.

Rob Spoor

Joined: Oct 27, 2005
Posts: 20188

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):

How To Ask Questions How To Answer Questions
seema chhetri
Ranch Hand

Joined: Feb 17, 2009
Posts: 43

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.

I agree. Here's the link:
subject: Jtree with its Modules
It's not a secret anymore!