Changing JTree at runtime
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   andrea_loleo
Posted On:   Thursday, July 26, 2001 12:40 AM

I have an already populated JTree. At Runtime I add one or more node to JTree, using something like parentNode.add(newNode).... If I call a repaint method, the new node isn't visible, I have to reload model using DefaultTreeModel dtm = ((DefaultTreeModel)myTree.getModel()); TreeNode rootNode = (TreeNode) dtm.getRoot(); dtm.reload(rootNode); Unfortunately this method cause the lost of focus on the currently selected node and the collapse of all JTree around its root node; If I change an existing node (for example changing its text or icon) I must reload the model too!!! Does exist an other more "soft" method to change the aspect of a JTree w   More>>

I have an already populated JTree. At Runtime I add one or more node to JTree, using something like

parentNode.add(newNode)....

If I call a repaint method, the new node isn't visible, I have to reload model using

			
DefaultTreeModel dtm = ((DefaultTreeModel)myTree.getModel());
TreeNode rootNode = (TreeNode) dtm.getRoot();
dtm.reload(rootNode);



Unfortunately this method cause the lost of focus on the currently selected node and the collapse of all JTree around its root node; If I change an existing node (for example changing its text or icon) I must reload the model too!!!

Does exist an other more "soft" method to change the aspect of a JTree without reload the entire model?

   <<Less

Re: Changing JTree at runtime

Posted By:   Scott_Stanchfield  
Posted On:   Tuesday, August 14, 2001 02:57 AM

You need to make sure you fire the appropriate events from your tree model after updating the tree.

The JTree will wait for update notification before changing its display. This allows you to add several nodes in one batch before it repaints.

Depending on the tree model you're using you'll probably need to call a method like


model.fireTreeStructureChanged()


If you're using the default tree model, see the Javadocs; if you're using a custom tree model, make sure you provide a way to fire the events. (In general, you shouldn't automatically fire the events when new nodes are added, unless the model is really meant to force updates to the view for each node. This can really slow things down and cause a lot of "flicker" if you add several nodes at once)

Re: Changing JTree at runtime

Posted By:   Anonymous  
Posted On:   Friday, July 27, 2001 06:46 AM

If myTree it's your tree and rootNode is the root Node you can use the following code instead of your repaint method:


myTree.setModel(new DefaultTreeModel(rootNode));


About changing nodes, if you add an object to the tree, when you change his name the tree is automatic updated (at least with me it worked).

About | Sitemap | Contact