• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Number of child in root node of JTree

 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I am not very clear how the child nodes are handled in JTree. I have a JTree and i obtain the tree model using JTree.getModel() method. TreeModel in turns get me the root node of the Tree using TreeModel.getRoot() method. I do a getChildCount to obtain number of child nodes. If i want to delete all child nodes, i am not able to use a for loop to do that because everytime i use TreeModel.getChild(rootNode, index) and removeNodeFromParent() to get and remove a child from root node, the array of child nodes shrinks automatically, like you are pulling a book at the bottom of a pile of books. So if i want to delete all child nodes, i have to getChild at index=0 for as many times as the child count. Is this the typical way to delete all child nodes?
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
there is of course another way to remove all children. You can cast the root node to a DefaultMutableTreeNode. with this you can do it.
TreeModel model = tree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
root.removeAllChildren();
Bye
Rene
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic