Home arrow static arrow Java Programming [Archive] - Get name of edited node in JTree
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Get name of edited node in JTree
This topic has 13 replies on 1 page.

Posts:87
Registered: 8/5/02
Get name of edited node in JTree  
Jul 26, 2004 5:09 AM



 
I am using a TreeModelListener on my JTree and need ot get the name of the node that was just edited. I can get the new name of the node, but I need the old name. Here is what I have so far:

    public void treeNodesChanged( TreeModelEvent e ) {         DefaultMutableTreeNode node = (DefaultMutableTreeNode)                                        e.getTreePath().getLastPathComponent();         System.out.println( "Old value: " + node.getUserObject() );         int index = e.getChildIndices()[ 0 ];         node = (DefaultMutableTreeNode) node.getChildAt( index );         System.out.println( "New value: " + node.getUserObject() );     } // end treeNodesChanged


The old value is just the parent...the new value is correct. Any ideas?

Thanks!
 

Posts:1,695
Registered: 1/13/04
Re: Get name of edited node in JTree  
Jul 26, 2004 5:52 AM (reply 1 of 13)



 
You are mistaken: TreeModelListener listens to model events. Your need doesn't concern the model events, but selection events.
See the TreeSelectionListener interface for details concerning your problem.

Hope this helped,
Regards.
 

Posts:87
Registered: 8/5/02
Re: Get name of edited node in JTree  
Jul 26, 2004 6:11 AM (reply 2 of 13)



 
The TreeSelectionListener only listens for selections though - that doens't help me. I need to know the old value of a node that has been edited.

I am able to do this with the
treeNodesRemoved
method (determining which nodes have been removed) by doing:

    public void treeNodesRemoved( TreeModelEvent e ) {         Object [] children = e.getChildren();         // print out all nodes that have been removed        for ( int i=0; i<children.length; i++ ) {            System.out.println( children[ i ] );        } // end for     } // end treeNodesRemoved


I tried doing something similar in the
treeNodesChanged
method, but no go.
 

Posts:31,095
Registered: 4/30/99
Re: Get name of edited node in JTree  
Jul 26, 2004 8:31 AM (reply 3 of 13)



 
I need to know the old value of a node that has been edited.

Perhaps you could get this by implementing a custom TreeCellEditor (I think that's what it's called). And perhaps you'd get better answers in the Swing forum.
 

Posts:1,695
Registered: 1/13/04
Re: Get name of edited node in JTree  
Jul 27, 2004 12:34 AM (reply 4 of 13)



 
With the TreeSelectionListener, you can store the name/value of the node as soon as it is selected, so before it can be edited.
After the editing, compare the new value to the old and do what you need with this.
The same principle can apply to a whole node and its childs, and even the whole tree itself.
The problem is that without a working sample code to demonstrate your problem, it's hard to guess the needed help.
 

Posts:87
Registered: 8/5/02
Re: Get name of edited node in JTree  
Jul 27, 2004 3:56 AM (reply 5 of 13)



 
The code I have posted is an overridden method in my TreeModelListener; that is what I'm using to try and do what I want.

I did think about using the TreeSelecitonListener to keep track of the last selected node so that when one is edited, I can assume that the last selected node was the edited node. I'm sure that will work, but it sounds like a bit of a hack and I was wondering if there was any better way. I was just thinking that if I can do it with the treeNodesRemoved method in my TreeModelListener, I should be able to do it in another overridden method there.
 

Posts:1,695
Registered: 1/13/04
Re: Get name of edited node in JTree  
Jul 27, 2004 6:30 AM (reply 6 of 13)



 
Sorry but I still don't see the link between treeNodesRemoved and node editing...
Again I must repeat myself: post a wider sample of your code, and perhaps someone will understand what you mean and/or find something which could help you solve the problem.
 

Posts:31,095
Registered: 4/30/99
Re: Get name of edited node in JTree  
Jul 27, 2004 7:57 AM (reply 7 of 13)



 
I will repeat myself too, except that I do understand what you are asking. The TreeModelListener tells you that a node has changed in some way; you can examine the node and see what its user object (which you confusingly call its "name") IS. But I don't see where it says you can see what its user object WAS. And I don't think you can see that. All you get is a link to the nodes as they are after the change. That's why I think your cell editor has to be used; after all, it's the thing doing the changing, so it knows at some point in time what are the old and new values.
 

Posts:87
Registered: 8/5/02
Re: Get name of edited node in JTree  
Jul 27, 2004 9:07 AM (reply 8 of 13)



 
Franck_Lefevre:

I have posted all of the code I am using - I was thinking that I can get the previous name (or user object) of the node that has been edited in my treeNodesInserted method because of how I can get the nodes from the treeNodesRemoved method I am overriding. But, I see now that that is not possible...I was misinterpreting what was going on there.
 

Posts:87
Registered: 8/5/02
Re: Get name of edited node in JTree  
Jul 27, 2004 9:12 AM (reply 9 of 13)



 
DrClap:

Sorry, when I say "name" I do mean user object. My post I jsut made before this hopefully explains why I thought I could get what the node WAS after editing because I mistakingly assumed that the treeNodesRemoved method was doing somethign similar. But, I guess that treeNodesRemoved is actually storing all of the removed nodes inside the TreeModelEvent object - and I can get them by looking at the children of this object (as I have in my code).

It now makes sense to me that I cannot use the TreeModelListener and probably should use the DefaultTreeCellEditor. I didn't know that there was something like that - I should have assumed that though.

I'll give that a shot and see what happens.

Thanks!
 

Posts:1,695
Registered: 1/13/04
Re: Get name of edited node in JTree  
Jul 27, 2004 11:57 PM (reply 10 of 13)



 
Franck_Lefevre:

I have posted all of the code I am using.

No, you haven't... see below

I was thinking that I can get the previous name (or user object) of the node that has been edited in my
treeNodesInserted method because of how I can get the nodes from the treeNodesRemoved method.

treeNodesInserted is an example of the code you have not posted.

Anyway, you seem to have solved your problem (or, at least, found the answers to your questions).
So, good luck to you now...

 

Posts:87
Registered: 8/5/02
Re: Get name of edited node in JTree  
Jul 28, 2004 4:13 AM (reply 11 of 13)



 
Sorry...regarding treeNodesInserted - that is not what I wanted to say. I mean treeNodesChanged - that is where I tried grabbing the edited value (after the node is changed...much like in treeNodesRemoved, where I grab the values removed). That code is posted... sorry for the confusion.

Anyway, I don't think I have quite solved the problem...I suspect I need to use some type of cell editor listener, but I can't find the correct hing to use. I am currently trying to use CellEditorListener, but the two methods that it implements (editingStopped and editingCanceled) don't seem to give me the info I want.

DefaultTreeCellEditor seems more along hte lines of what I want, but I don't see how to use that...
 

Posts:87
Registered: 8/5/02
Re: Get name of edited node in JTree  
Jul 28, 2004 8:38 AM (reply 12 of 13)



 
I figured out how to get it to work. I am using a TreeCellEditor and using the inHitRegion method to store the name of a node that is being edited. I can then refer back to the value I got from there in order to tell what the name of the node used to be.

Thanks for all of your help!
 

Posts:1,695
Registered: 1/13/04
Re: Get name of edited node in JTree  
Aug 2, 2004 12:00 AM (reply 13 of 13)



 
My pleasure...

;) Have fun!
 
This topic has 13 replies on 1 page.