| Author |
JTree making a node act as parent
|
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
|
|
Hi,
I want to make a node to reflect as a Parent node containing children (Means i need the "+" sign associated with that node when shown in JTree). Currently I am using the TreeSelectionListener interface to do some operation when a node is selected in the JTree. But I would like to do some operation only when the node is expanded using the TreeExpansionListener interface. At this point there is only a Root and say some 10 children under the root. I want all the 10 children to behave like a collapsed node (pretend they have some children within them).
Is there a way I can do this? If not how to use the TreeExpansionListener for a leaf node(bit awkward)?
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
|
|
|
I did it by adding a dummy node and then removing it in the TreeWillExpandListener interface when a node is expanded.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Wouldn't it be easier to use a new TreeModel that returns false for isLeaf?
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
|
|
Hey Rob!!! Thanks! it worked when the isLeaf() is overridden...
Thanks Again!!!
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
You're welcome.
And the good thing is, this isn't even a trick or workaround - it's documented:
Returns true if node is a leaf. It is possible for this method to return false even if node has no children. A directory in a filesystem, for example, may contain no files; the node representing the directory is not a leaf, but it also has no children.
|
 |
 |
|
|
subject: JTree making a node act as parent
|
|
|