Hiding some nodes in jtree

S

Srikanth

How can i hide and show some nodes in the tree based on some criteria?
Right now i done it this way(given below). But its not working...

1) I have extended the DefaultMutableTreeNode into CustomTreeNode which
contains a variable called visible and contains methods setVisible()
and isVisible()
2) I have overridden the DefaultTreeModel.
a) getChildCount() gives the count of the visible node
b) getChildIndex() gives the index of the nodes among the visible
nodes
c) getChild() gives that node.
 
T

Thomas Weidenfeller

Srikanth said:
How can i hide and show some nodes in the tree based on some criteria?
Right now i done it this way(given below). But its not working...

1) I have extended the DefaultMutableTreeNode into CustomTreeNode which
contains a variable called visible and contains methods setVisible()
and isVisible()
2) I have overridden the DefaultTreeModel.
a) getChildCount() gives the count of the visible node
b) getChildIndex() gives the index of the nodes among the visible
nodes
c) getChild() gives that node.

That's going in the right direction. What one typically does is to
implement a wrapper model. It wraps the original model, provides the
same API (TreeModel), but does some filtering on the original model data
before passing it through.

In your case such a wrapper model would take your isVisible() flag in
the tree nodes into account when responding to getChild...() method
invocations. Very similar to what you did in your subclassed model
implementation.

There is some example for a JList or JTable in Sun's GUI tutorial to
sort list data before it is displayed. The structure is the same for a
Tree Model.

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Extra Row in JTree 6
Filtered JTree 0
JTree model update problem 3
Programmatically removing nodes from a JTree 2
change icon in jtree 0
JTable within JTree 1
JTree navigation 1
JTree node partially visible 1

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top