This week's book giveaway is in the Android forum.
We're giving away four copies of Learning Java by Building Android Games and have Jean-Marcel Belmont on-line!
See this thread for details.
Win a copy of Learning Java by Building Android Games this week in the Android forum!
  • 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
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

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
Sheriff
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!