• Post Reply Bookmark Topic Watch Topic
  • New Topic

Editing JTree Custom Model

 
raphael Bereh
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am implementing TreeModel to buil a custom model to a JTree.
My problem is when a Edit the Model (add,remove,change value of a Node ) my Tree does no refresh to present the accurate data. I have seen lots of example on the web, but each time people are using DefaultMutableTree .. to perform editing operation. I am writing an xml Editor that uses a DOM parser to read the data into a DOM that I display using a JTree. Then I sue a SAX 2 parser to validate the data against a schema and display errors in a different rendering. Now I need to enable the user to edit the xml DOM Tree in order to correct the file.
Itried implementing a TreeModelListener class, but I have no idea of what kind of code should go into each method of the interface to get my tree updated.
I really need help quicklly and would be greatefull of example with code samples:I read a lot about the theory but still not getting there
Thanks.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Swing forum.
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, DefaultMutableTreeNode is a long long word and I hate it too, but the class itself is really smart, because you can give this class your own object (setUserObject()). There you can take whatever you want, you simply have to implement the toString method in order to show it right.
Of course, you can do all by your own by implementing TreeModel, but in my opinion it is better to use existing stuff like DefaultTreeModel and extending from that, because they will fire all events. I think, this could be your problem. After you are changing the model, maybe you forget to tell the tree, what happened?
 
Justin Chu
Ranch Hand
Posts: 209
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you extend DefaultTreeModel, it requires the hierarchical data structure to be stored INSIDE the DefaultTreeModel, which is not preferred in my case.

I have still yet to figure out how to refresh the tree yet. One way is to look at the source of DefaultTreeModel and implement firing the event, which is a pain to implement.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!