• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

JTree Question  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My program is importing a DefaultTreeModel from another program. In my program, I would like to create an identical DefaultTreeModel, but instead of each node being a DefaultMutableTreeNode, I would like each to be a simContainerNode (which is a class I wrote that extends DefaultTreeModel that has the additional field of int frequency).

Does anyone have a good way of doing this? Or even any ideas? I am absolutely stumped.

 
Sheriff
Posts: 23714
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just write some code which goes through the source tree recursively and produces the target tree as you go along. Basically you'd have a method "private simContainerNode copyNode(DefaultMutableTreeNode node)" which creates the new node from the old one and then appends copies of the old node's children (created by that method) to the new node.

(Instead of extending DefaultMutableTreeNode you might consider the fact that you can put any kind of object at all into it as the user object.)
 
John Danek
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help. I got it working by basically doing exactly what you said, and "swallowing" the old tree's nodes into the new ones by storing the old nodes as the user object in the new ones.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!