• Post Reply Bookmark Topic Watch Topic
  • New Topic

Display all elements of vector in JTree as separate node

 
Manjula Rao
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to display all elements of vector in JTree.
i have files as well as directories in a vector.
when i use DefaultMutableTreeNode, i get all the
elements of vector in one node itself.
But i want that each element (file)should be shown as separate node
and all elements of directory should be shown as child nodes of parent
node(directory).
 
eric moon
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Boy aren't you lucky I've just been studying this same subject!
You could subclass DefaultMutableTreeNode, (give it a shorter name, while you're at it!! Like FileNode.) and give it a constructor that takes a vector. This constructor will have to call the .addChildren and/or addNode methods appropriately. You'll want to add a node for each directory or file. You can use addChildren to add a bunch of nodes to parent node. You can then use one of your nodes to creat your treemodel:
tree.getModel().setRoot(new FileNode());
(or similar.... maybe it's getDefaultModel(), I'm not sure)
As an alternative--how did your files end up in a vector?? Maybe there is a way to avoid putting that heirarchical structure into a linear collection in the first place!
Hope this helps!
e
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!