• 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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: 24082
54
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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!