• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTree: strange appearance / broken tree  RSS feed

 
Ron Katz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
Strange things happen from time to time to my JTree.
Sometimes, when I open my application, I see my JTree with gaps or broken links.
Sometimes, the nodes appear in the wrong order (!) - i.e. suppose A is the root, B is its childe and C is B's child, the tree may "decide" to appear in the midle of it's panel instead of appearing, as usual in my application, on the top of the panel, and strange of all, when you open the branch of B, instead of C appearing after it, C appears... above A...
Did anyone see such a strange thing?
Anyone have any idea or a clue of what the problem might be?
Please give a hand, good people...
 
Brian Cole
Author
Ranch Hand
Posts: 936
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ron Katz:
Hello all,
Strange things happen from time to time to my JTree.
Sometimes, when I open my application, I see my JTree with gaps or broken links.


It's hard to know what to suggest without more details. For instance, are you using DefaultMutableTreeNodes or some other class?

One thing you can try: If you're using DefaultTreeModel make sure you are calling yourModel.insertNodeInto() and yourModel.removeNodeFromParent() and that you are not calling someNode.insert() or someNode.remove().

You can also make sure the adding/removing of nodes happens on the event tread.
 
Ron Katz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Brian and thank you very much for your response.
I'm using a node wich expands DefaultMutableTreeNodes, and you were correct: I was using insert / remove (and also add(...)) instead of using the model's insertNodeInto & removeNodeFromParent methods.
I changed my code to use the model's methods instead of the node's methods.
I saw in the java code that it sais that :
"insertNodeInto is the preferred way to add children as it will create the appropriate event" - but I couldn't figure out why is it better to use this method then using the others. I will be happy to learn why is that...

Since this problem does not occure consistently, I can't tell you right now if your suggestion solved the problem, however I will let you know (in a few days I guess) if the problem was solved or not...

Thank you again!

Originally posted by Brian Cole:


It's hard to know what to suggest without more details. For instance, are you using DefaultMutableTreeNodes or some other class?

One thing you can try: If you're using DefaultTreeModel make sure you are calling yourModel.insertNodeInto() and yourModel.removeNodeFromParent() and that you are not calling someNode.insert() or someNode.remove().

You can also make sure the adding/removing of nodes happens on the event tread.
 
Brian Cole
Author
Ranch Hand
Posts: 936
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ron Katz:
I saw in the java code that it sais that :
"insertNodeInto is the preferred way to add children as it will create the appropriate event" - but I couldn't figure out why is it better to use this method then using the others.


What insertNodeInto(newChild, parent, index) does is call parent.insert(newChild, index) and fire an event.

If you call parent.insert() then no events are fired, so the tree will not be notified that there have been changes to the tree model.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!