JTree: show only subtree

Posted on 2007-08-03
Last Modified: 2008-01-09
I have an already populated JTree, such as:

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

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

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?
Question by:Ugi
    LVL 92

    Expert Comment

    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

    Author Comment

    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?
    LVL 92

    Expert Comment

    you'd be creating a new model but it would still be referencing the same nodes.

    Author Comment

    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)
    LVL 92

    Accepted Solution

    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

    Author Comment

    > 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.
    LVL 92

    Expert Comment

    let me know if u get stuck :)
    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now