Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2005

Tip: Looking for answers? Try searching our database.

Removeing tree node from unselected Jtree

Thread view: 
IchBin - 26 Feb 2005 02:38 GMT
I need to remove a node from a JTree object (node may or may not be
selected, does not matter). I can scan the tree nodes. Once I find the
node and it satisfies my if, I need to remove it from the tree.  Can't
seem to have the right objects so I can remove it. Any one have any
suggestions?

static public void testTree(JTree tree){

        scanNodes(tree);
}
static public void scanNodes(JTree tree) {
  DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
  TreeNode root   = (TreeNode)tree.getModel().getRoot();
  scanNodes(model, root);
}
static public void scanNodes(TreeModel model, TreeNode innode) {
 String target = "[ 93 ]";

 if (innode.getChildCount() >= 0) {
    for (Enumeration e = innode.children(); e.hasMoreElements();) {

         TreeNode node = (TreeNode)e.nextElement();
         if (node.toString().contains(target)){
              System.out.println(node);
              ========> remove node
 //model.removeNodeFromParent(node);
 //tree.removeCurrentNode();
          }
          scanNodes(model, node);
      }
   }
}
Signature


Thanks in Advance...
IchBin
__________________________________________________________________________

'The meeting of two personalities is like the contact of two chemical
substances:
 if there is any reaction, both are transformed.'
-  Carl Gustav Jung,  (1875-1961),  psychiatrist and psychologist

Babu Kalakrishnan - 26 Feb 2005 08:45 GMT
> I need to remove a node from a JTree object (node may or may not be
> selected, does not matter). I can scan the tree nodes. Once I find the
> node and it satisfies my if, I need to remove it from the tree.  Can't
> seem to have the right objects so I can remove it. Any one have any
> suggestions?

I am not sure if I understand your problem correctly - but, there are
some problems in the code below - (See below)

> static public void testTree(JTree tree){
>
[quoted text clipped - 21 lines]
>    }
> }

When you create an Enumeration (innode.children()), the enumeration
object would become invalid if you remove something from it. (Yes - the
TreeNode interface doesn't specify such behaviour, but then you cannot
assume that it is safe to do so. Also the default implementation
DefaultMutableTreeNode does specify this). So any further access of the
enumeration can throw an Exception.

I'd suggest using a loop like :

for (int i=innode.getChildCount()-1; i >=0; i--)
{
   TreeNode node = innode.getChildAt(i);
   // Test child node and remove if required      
   // else scanNodes(model,node);   
}

Note two points here. The iteration is performed backwards because
otherwise removal of a node can result in the indices of nodes changing.
(Of course you can correct your index for that, but this looks simpler).
Secondly, the recursive call is made only if the node is not removed.
This is because once you remove a node, it as well as any of its
children cease to be a part of the model, and so i'd guess their removal
is unnecessary.

Wonder why all your methods have a static tag ?? Typically it is a sign
of trying to use Java in a non-OO manner.

BK
IchBin - 27 Feb 2005 20:56 GMT
>> I need to remove a node from a JTree object (node may or may not be
>> selected, does not matter). I can scan the tree nodes. Once I find the
[quoted text clipped - 59 lines]
>
> BK

Thank you so much for your direction. I understand your logic now. I
have implemented the FOR LOOP and not the enumeration.

The question I had was, now that I have parsed thru the tree nodes and
want to delete the child node. At this point I do not have the model nor
the LastSelectedPathComponent to issue the removeNodeFromParent.

I do have code where I have this information and I can issue:
"CurrentTreeModel.removeNodeFromParent(selectedTreeNode)"

Not sure how to acquire this information at the point where I have the
child node and want to remove it.

The static is just old habits I am trying to get out of.

Signature

Thanks in Advance...
IchBin
__________________________________________________________________________

'The meeting of two personalities is like the contact of two chemical
substances:
 if there is any reaction, both are transformed.'
-  Carl Gustav Jung,  (1875-1961),  psychiatrist and psychologist

Babu Kalakrishnan - 28 Feb 2005 12:59 GMT
>>> I need to remove a node from a JTree object (node may or may not be
>>> selected, does not matter). I can scan the tree nodes. Once I find
[quoted text clipped - 13 lines]
> Not sure how to acquire this information at the point where I have the
> child node and want to remove it.

If you mean that the "removeNodeFromParent" method doesn't exist in the
TreeModel interface, the answer is that you have to cast "model" to a
DefaultTreeModel before using the method. Similarly the "node" has to
be typecast as a MutableTreeNode in order to call the method. This
assumes that you're using a DefaultTreeModel as your model, and a
DefualyMutableTreeNode as the nodes in it (as is the case for a default
JTree)

((DefaultTreeModel)model).removeNodeFromParent((MutableTreeNode)node)

should do the job.

I don't really understand the relevance of LastSelectedPathComponent in
all this since you're not dealing with selection at all.

BK

> The static is just old habits I am trying to get out of.

eEtter to get rid of those at the earliest and program in real Java :-)

BK
IchBin - 01 Mar 2005 04:05 GMT
>>>> I need to remove a node from a JTree object (node may or may not be
>>>> selected, does not matter). I can scan the tree nodes. Once I find
[quoted text clipped - 37 lines]
>
> BK
Thanks Babu..

Have reviewed my code and:

All my tree nodes are defined as DefaultMutableTreeNode.
All my tree models are defined as DefaultTreeModel.

Then later in the code I try the following. It runs but no nodes are
removed and no error messages?

    public void scanNodes(JTree tree)
    {
        scanNodes (( DefaultTreeModel)tree.getModel(),
                   ( TreeNode)tree.getModel().getRoot() );
    }

     public void scanNodes(DefaultTreeModel model, TreeNode innode)
     {
       for (int i=innode.getChildCount()-1; i >=0; i--)
       {
         MutableTreeNode node = (MutableTreeNode)innode.getChildAt(i);

         model.removeNodeFromParent(node);

         scanNodes(model, node);
       }
     }

Signature

Thanks in Advance...
IchBin
__________________________________________________________________________

'The meeting of two personalities is like the contact of two chemical
substances:
 if there is any reaction, both are transformed.'
-  Carl Gustav Jung,  (1875-1961),  psychiatrist and psychologist

Babu Kalakrishnan - 01 Mar 2005 11:32 GMT
>>>>> I need to remove a node from a JTree object (node may or may not be
>>>>> selected, does not matter). I can scan the tree nodes. Once I find
[quoted text clipped - 66 lines]
>        }
>      }

The above code should remove all nodes below root and leave you with a
JTree that has a single root node (even though the code itself is
somewhat buggy - see the last para of my response).

If the scanNodes() call isn't made from the EDT, there is a possiblility
that the view may not reflect the real state of the tree. This can be
solved by wrapping the removeNodeFromParent call within a
SwingUtilities.invokeLater wrapper.

If it still doesn't work, this points to some other bug in your code.
Please post a compilable example that shows this behaviour.

I had mentioned this earlier, but here again you seem to be calling
scanNodes recursively with a node, even after the node has been removed
from the model. This does not make any sense, because the node and its
children are no longer part of the model, and it doesn't make any sense
to make that call on the model. (I think the DefaultTreeModel will
ignore this, but there are no guarantees that it will not throw an
exception)

BK
IchBin - 01 Mar 2005 15:05 GMT
> The above code should remove all nodes below root and leave you with a
> JTree that has a single root node (even though the code itself is
[quoted text clipped - 17 lines]
>
> BK

My tree structure is [Root, Parent, Child]. I should only be deleting
the child node under their parents. In the present logic if I do not
recursively call scanNodes I only see the parent nodes. Presently, since
the remove is not working I assume this is why I am not getting the
exception. I need to pull back and look at this again.

Signature

Thanks in Advance...
IchBin
__________________________________________________________________________

'The meeting of two personalities is like the contact of two chemical
substances:
 if there is any reaction, both are transformed.'
-  Carl Gustav Jung,  (1875-1961),  psychiatrist and psychologist

Thomas Weidenfeller - 28 Feb 2005 10:08 GMT
> I need to remove a node from a JTree object (node may or may not be
> selected, does not matter). I can scan the tree nodes. Once I find the
> node and it satisfies my if, I need to remove it from the tree.

DeafultMutableTreeModel.removeNodeFromParent();

/Thomas

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.