?
Solved

JTree: show only subtree

Posted on 2007-08-03
9
Medium Priority
?
620 Views
Last Modified: 2008-01-09
I have an already populated JTree, such as:

root
+child1
| +child1.1
| +child1.2
+child2
| +child2.1
| +child2.2

and then i need a tree where the root is child1, which look like:
child1
+child1.1
+child1.2

This means i just want to hide all other nodes. Also the nodes which are the parents of my new defined root node. (And of course i need to do this on any level)
And later on i need the whole tree again; this is why i don't want to just set a new tree-strucure to the model.

To make it more clear: i have a JTree and 2 Buttons called: "Show full tree" and "Show selected subtree".

Is there a good way to do this, or do i have to set a new model (with model data) to the tree?
0
Comment
Question by:Ugi
  • 4
  • 3
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 19623426
Depends on what your using for your TreeModel
If you're using TreeNode's then try:

tree.setModel(new DefaultTreeModel(childnode));

Basically yuou need to change the model to a new one rooted at the appropriate node
0
 

Author Comment

by:Ugi
ID: 19624274
I subclassed the DefaultTreeModel.
Yes, I could set a new Model, but for my needs it would be better to use the same model and just set a flag which node is the current root.

There are some Methods in the DefaultTreeModel like
getRoot(), getChild(), getChildCount()

Is it enough to change the return values of them? or does the JTree use other functions to create the structure?
And what must getPathToRoot() return? Can i return the path to the total root, or must i return the path to the current root element?
0
 
LVL 92

Expert Comment

by:objects
ID: 19624308
you'd be creating a new model but it would still be referencing the same nodes.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Ugi
ID: 19624734
I must often change (automatically; the 2 buttons were just to describe it better) between the full tree and different subtrees. Because of this i think that it needs less computation time and less RAM if the model is able to this.
And a nice side effect would be that i do not need to cache the whole tree (some thousand entries).

Is it enought to just overwrite the functions
  getRoot(), getChild(), getChildCount()

Or give me a link to a good tutorial about TreeModels. (Because the tree tutorial of sun is not good in the point of what happens if the root of the model changes during runtime - does this cause problems, or can this be done without any difficulty; are there some important point to take care of)
0
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 19624831
you'll need to implement your own model then

> Because the tree tutorial of sun is not good in the point of what happens if the root of the model changes during runtime

basically you have to change thew way your model behaves.
ie. return a different root
0
 

Author Comment

by:Ugi
ID: 19624917
> basically you have to change thew way your model behaves.
> ie. return a different root
This means that i do not get in trouble because i changed the root.

I'll implement this next week and give you the points afterwards.
0
 
LVL 92

Expert Comment

by:objects
ID: 19624932
let me know if u get stuck :)
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20213323
Forced accept.

Computer101
EE Admin
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question