wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JTree expand node and highlight Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTree expand node and highlight" Watch "JTree expand node and highlight" New topic
Author

JTree expand node and highlight

Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
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

Joined: Sep 16, 2005
Posts: 1780


There is no emoticon for what I am feeling!
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
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

Joined: Sep 16, 2005
Posts: 1780
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

Joined: May 18, 2004
Posts: 434
Oh ok, I get it now. The explanation makes it much clearer. Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTree expand node and highlight
 
Similar Threads
selecting a JTree node programmatically
collapsing JTree
JTree
JTree, adding and removing nodes contained in a master tree
Need help on JTree