• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

JTree Question

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 27211
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If tomatoes are a fruit, then ketchup must be a jam. Taste this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic