• 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

Editing JTree Custom Model  RSS feed

 
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.
 
High Plains Drifter
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Swing forum.
 
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?
 
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.
 
Goodbye moon men. Hello tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!