This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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
Joined: Sep 16, 2005
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.
Joined: May 18, 2004
Oh ok, I get it now. The explanation makes it much clearer. Thanks!