Home arrow static arrow Java Programming [Archive] - Cast a TreeNode to a DefaultMutableTreeNode
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Cast a TreeNode to a DefaultMutableTreeNode
This topic has 8 replies on 1 page.

Posts:254
Registered: 4/15/04
Cast a TreeNode to a DefaultMutableTreeNode  
Jun 16, 2004 7:34 AM



 
I can't find out how to do this anywhere. Maybe it could be done a more efficient way?

Below is my current algorithm.

I have a dummy node with no children. When a user clicks on that node, a Servlet call is made, and that dummy node should be replaced with a real node (that contains children). So, I am trying to remove the old node that was clicked on, and replace it with the new node( which has child nodes attached to it).

I should mention that I chose to replace the original node instead of attaching child subnodes to the original node one at a time. This is because there could be 1000's of nodes returned, and attaching them one by one seemed inefficient. Creating a branch on the Servlet side and attaching that branch in one call seemed much more efficient.

To complete my code, I need to be able to convert a TreeNode into a DefaultMutableTreeNode. That way I can attach the new node to the old nodes parent, effectively replacing the old node.

   public void populateNode( )   {      // Get currently Selected node      TreePath currentPath = tree.getSelectionPath();      DefaultMutableTreeNode currentNode = (currentPath ==null ) ? rootNode         : (( DefaultMutableTreeNode ) currentPath.getLastPathComponent());       // Get parent of currently selected node      TreeNode tn = currentNode.getParent();       // Get back structure with root node matching the currently selected node      DefaultMutableTreeNode dmtn = MyServlet.callServlet( currentNode.toString() );       // remove selected node      removeCurrentNode();       // insert new root node into tree at location      addObject( tn, dmtn, true ); // NEED TO CONVERT TREENODE TO DEFAULTMUTABLETREENODE   }    /**    * Add child to the currently selected node.    * @param parent The parent node to attach the child to.    * @param child The child node to add.    * @param shouldBeVisible Set to true if tree should scroll to the new node when opened.    * @return The DefaultMutableTree node.    */   public DefaultMutableTreeNode addObject( DefaultMutableTreeNode parent, Object child, boolean shouldBeVisible )   {      DefaultMutableTreeNode childNode = ( DefaultMutableTreeNode ) child;       if ( parent == null )      {         parent = rootNode;      }       ( ( DefaultTreeModel ) tree.getModel() ).insertNodeInto( childNode, parent, parent.getChildCount() );       // Make sure the user can see the lovely new node.      if ( shouldBeVisible )      {         tree.scrollPathToVisible( new TreePath( childNode.getPath() ) );      }      return childNode;   }    /**    * Remove the currently selected node.    */   public void removeCurrentNode()   {      TreePath currentSelection = tree.getSelectionPath();      if ( currentSelection != null )      {         DefaultMutableTreeNode currentNode = ( DefaultMutableTreeNode ) currentSelection.getLastPathComponent();         MutableTreeNode parent = ( MutableTreeNode ) currentNode.getParent();         if ( parent != null )         {            treeModel.removeNodeFromParent( currentNode );            return;         }      }   }
 

Posts:31,095
Registered: 4/30/99
Re: Cast a TreeNode to a DefaultMutableTreeNode  
Jun 16, 2004 8:15 AM (reply 1 of 8)



 
I should mention that I chose to replace the original
node instead of attaching child subnodes to the
original node one at a time. This is because there
could be 1000's of nodes returned, and attaching them
one by one seemed inefficient. Creating a branch on
the Servlet side and attaching that branch in one call
seemed much more efficient.

It does seem that way, doesn't it? Presumably there's something in some other place that is adding the 1000 children one at a time, but it's somebody else's responsibility.

To complete my code, I need to be able to convert a
TreeNode into a DefaultMutableTreeNode. That way I
can attach the new node to the old nodes parent,
effectively replacing the old node.

Whoever is producing the TreeNode needs to produce a DefaultMutableTreeNode (or a subclass of that). If it did, then you don't have a problem, just cast it. If it didn't, then you'll have to create your own DefaultMutableTreeNode and copy the user object and the 1000 children over from the TreeNode object. (So much for efficiency.)

PC²
 

Posts:254
Registered: 4/15/04
Re: Cast a TreeNode to a DefaultMutableTreeNode  
Jun 16, 2004 8:31 AM (reply 2 of 8)



 

It does seem that way, doesn't it? Presumably there's
something in some other place that is adding the 1000
children one at a time, but it's somebody else's
responsibility.

What I think I meant to say was:

If I attach them one by one to the existing node then the treeModel would be updated each time a node was added.

If I attach a DefaultMutableTreeNode once then the treeModel would be updated once. When the DefaultMutableTreeNode structure is built on the Servlet side, it doesn't use the treeModel. It builds a branch by using the myDefaultMutableTreeNode.add( myOtherDefaultMutableTreeNode ); method call.


To complete my code, I need to be able to convert a
TreeNode into a DefaultMutableTreeNode. That way I
can attach the new node to the old nodes parent,
effectively replacing the old node.

Whoever is producing the TreeNode needs to produce a
DefaultMutableTreeNode (or a subclass of that). If it
did, then you don't have a problem, just cast it. If
it didn't, then you'll have to create your own
DefaultMutableTreeNode and copy the user object and
the 1000 children over from the TreeNode object. (So
much for efficiency.)

PC�
I'm not sure I understand this. I am just using built-in function calls. Do you have an example of what you're talking about?

Thanks
 

Posts:31,095
Registered: 4/30/99
Re: Cast a TreeNode to a DefaultMutableTreeNode  
Jun 16, 2004 8:36 AM (reply 3 of 8)



 
addObject( tn, dmtn, true ); // NEED TO CONVERT TREENODE TO DEFAULTMUTABLETREENODE
Were you just asking about this line of code?
addObject( (DefaultMutableTreeNode)tn, dmtn, true ); // JUST CAST IT
PC²
 

Posts:254
Registered: 4/15/04
Re: Cast a TreeNode to a DefaultMutableTreeNode  
Jun 16, 2004 8:43 AM (reply 4 of 8)



 
It worked! I swear I tried casting that before I asked about it in this forum.

So, am I correct in thinking that it is better to create a branch of DefaultMutableTreeNodes in the Servlet? Or is it better to just pass an ArrayList of DFMT nodes back to the Applet, and use the addObject method to add them one by one via the tree model?

Thanks for your your help

 

Posts:31,095
Registered: 4/30/99
Re: Cast a TreeNode to a DefaultMutableTreeNode  
Jun 16, 2004 9:02 AM (reply 5 of 8)



 
So, am I correct in thinking that it is better to
create a branch of DefaultMutableTreeNodes in the
Servlet? Or is it better to just pass an ArrayList of
DFMT nodes back to the Applet, and use the addObject
method to add them one by one via the tree model?

Neither is better. If you want, try both versions and see which one comes closer to whatever you mean by "better" in this context.
 

Posts:254
Registered: 4/15/04
Re: Cast a TreeNode to a DefaultMutableTreeNode  
Jun 16, 2004 9:13 AM (reply 6 of 8)



 
By "Neither is better" do you mean both are equivalent, or there is a better way to do it in general?

I was just trying to think ahead as to which way has the least amount of reloads, refreshes, repaints, etc...
 

Posts:31,095
Registered: 4/30/99
Re: Cast a TreeNode to a DefaultMutableTreeNode  
Jun 16, 2004 10:59 AM (reply 7 of 8)



 
By "Neither is better" do you mean both are
equivalent, or there is a better way to do it in
general?

No, I mean that "better" is an undefined term.

I was just trying to think ahead as to which way has
the least amount of reloads, refreshes, repaints,
etc...

Why bother with that when actually testing them will give you the answer much more reliably? Your guess at those things is likely to be wrong, irrelevant, or both. All you are interested in is... well, I don't know, but if you wanted to know whether a lump of steel would fall faster through water if it were a sphere or a cube, you could do a whole bunch of complicated calculations or you could drop the two lumps and see what happened.
 

Posts:10,972
Registered: 10/23/03
Re: Cast a TreeNode to a DefaultMutableTreeNode  
Jun 16, 2004 11:23 AM (reply 8 of 8)



 
On a barely related note: why does MutableTreeNode introduce method setUserObject, but it isn't
until DefaultMutableTreeNode that getUserObject pops up. Riddle me this.
 
This topic has 8 replies on 1 page.