Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTree Question  RSS feed

 
John Danek
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.

 
Paul Clapham
Sheriff
Posts: 22374
42
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!