Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTree expand node and highlight

 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

The user selects a node on a JTree and then clicks a button to create a new node underneath this node as shown here...

Root
|- Node User Clicks On
|- Another Node

Root
|- Node User Clicked On
|- New Node Here
|- Another Node

Now, the code that I have creates the node and adds it to the tree and programmatically selects the new node. I can see that the new node is programmatically selected by doing printouts. However, my code makes the tree look like the first example above, you can't actually see the new node yet (you still have to double click on "Node User Clicked On" to open to the new node).



If there is anyone who can tell me how to make the new node visible and highlighted like a selected node should be programmatically I would be very greatful.

Many kind regards,
Rachel
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! It's not exactly what I need but I'm going to see what I can learn from it. Will have a shot at it and let you know how it goes.

Thanks again!
Rachel
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should have added an explanation:

When the JTree GUI seems to get out of synch, it's often because one changed
the nodes directly, without involving the model.

If you look at the standard node types (TreeNode, MutableTreeNode,
DefaultMutableTreeNode) you'll see nodes don't hold references
to their tree -- they don't (can't) inform the tree when things change,
like when a child node is added or removed.

One of the basic jobs of the TreeModel (usually implemented by
DefaultTreeModel) is to notify TreeModelListeners when the model
changes. JTree itself registers a listener with its model so that it
knows when and how to repaint.

So, if you are going to change the data in a tree, be sure to call
a model method like insertNodeInto, rather than a node method like
insert, to keep listeners in synch.
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh ok, I get it now. The explanation makes it much clearer. Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic