aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTree making a node act as parent 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 making a node act as parent" Watch "JTree making a node act as parent" New topic
Author

JTree making a node act as parent

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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: 1776
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: 19790
    
  20

Wouldn't it be easier to use a new TreeModel that returns false for isLeaf?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Hey Rob!!! Thanks! it worked when the isLeaf() is overridden...



Thanks Again!!!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTree making a node act as parent