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 SubTree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTree SubTree" Watch "JTree SubTree" New topic

JTree SubTree

Pamela Ng

Joined: Dec 20, 2008
Posts: 3

Is there a way to just show part of a tree, as in a subtree. I use a JTree to create for eg:

- Map
|___- mapID
- simulationID
- mapDesc
- mapLength
- mapWidth
- mapScale
- JunctionList
|______- Junction
|______- junctionID
|______- R10
|______- junctionType
|______- jConnectedTo
|______- BS5

The JTree is read from an XML document. And it can change dynamically depending on the XML document read, the JTree will show accordingly. There are somemore subnodes for Junction element, but for example, i want to stop at junctionType node. How do i do that? I have read several examples from other forums and tried but to no avail.

Another question: After i display this subtree, i want to link and display the remaining subtree by clicking a button, again display a small section of the subtree, by a count of 10 nodes by a refresh of the JTree. IThe count of nodes i mean by counting the main nodes, those nodes with + or -, Is it possible to do so? How do i do it? The examples i read online only give me the solution for displaying the whole tree and not part of a tree.

Help very much appreciated.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10826

Welcome to the Ranch Pamela, we are happy to have you here

I have read several examples from other forums and tried but to no avail.
What problem did you run into when you tried it?

As you probably know, the JTree is just the "view" and is not aware of the data at all. All the data is encapsulated inside the TreeModel.
If you carefully go through the API for the TreeModel, you will find lots of convenient methods like getChildCount() and getChild() etc. If you haven't done so already, please do take out time to read this.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
I agree. Here's the link:
subject: JTree SubTree
jQuery in Action, 3rd edition