JTree oddity...

D

dushkin

Hi,
I uses JTree in my code.
From time to time I remove a node and insert a new node instead of
it.
I use the object ID to follow the changes while debugging.

For example, before I make the changes the nodes IDs are as follows:

Root
Node 1 (7cb)
Node 2 (7db)
Node 3 (7eb)
Node 4 (7fb)

Now, The scenrio was is as follows:
Click Node1 --> Click Node2 --> Click Node1 --> Click Node2

After removing and inserting the first 2 nodes (after those clicks)
the tree looks like this:
Root
Node 1 (80b)
Node 2 (8ab)
Node 3 (7eb)
Node 4 (7fb)

and here comes the oditty...

After each click which fires the remove and insertin of nodes, I have
the following code:

//**********************************************************************************
DefaultMutableTreeNode mapMutNode = null;
TreePath selPath =
xTreeScm.getPathForLocation(e.getX(), e.getY());
if (selPath.getLastPathComponent() == null) {
return;
} else {

printlnTree();

DefaultMutableTreeNode mapMutNode =
selPath.getLastPathComponent();
int i =
xTreeScm.getModel().getIndexOfChild(xTreeScm.getRootNode(),
mapMutNode);
//**********************************************************************************

mapMutNode returns me the replaced 7cb node instead of the new 80b node
!!!
Moreover, the following getIndexOfChild() return -1 !


Can someone advise please? Thanks
 
T

Thomas Hawtin

dushkin said:
DefaultMutableTreeNode mapMutNode =
selPath.getLastPathComponent();
int i =
xTreeScm.getModel().getIndexOfChild(xTreeScm.getRootNode(),
mapMutNode);

mapMutNode returns me the replaced 7cb node instead of the new 80b node
!!!
Moreover, the following getIndexOfChild() return -1 !

It's difficult to make out what you are doing without a complete example.

At a guess it's because you are taking the TreePath before altering the
tree. TreePath represents a sequence of 'nodes'. So if you take a copy
of a sequence of tree nodes, then alter the original tree, nothing
happens to the sequence copy. TreeModel does not depend upon TreeNode.
Perhaps you are mixing up nodes with MutableTreeNode's concept of
user-objects. The lack of static types in TreeModel doesn't help.

Tom Hawtin
 
D

dushkin

Thanks Tom for your kind answer.

Let me focus a little bit more on the problem description:

DefaultMutableTreeNode mapMutNode = null;
TreePath selPath = xTreeScm.getPathForLocation(e.getX(), e.getY());
mapMutNode = selPath.getLastPathComponent();
int i = xTreeScm.getModel().getIndexOfChild(xTreeScm.getRootNode(),
mapMutNode);


mapMutNode return me a node that I removed using DefaultMutableTreeNode
"public void remove(MutableTreeNode aChild)" method, but the following
i equals -1 !

How can it be???
 
T

Thomas Hawtin

dushkin said:
mapMutNode return me a node that I removed using DefaultMutableTreeNode
"public void remove(MutableTreeNode aChild)" method, but the following
i equals -1 !

If you changed a node but didn't inform the tree model, anything could
happen.

Assuming you are using DefaultTreeModel, I suggest using the likes of
insertNodeInto and removeNodeFromParent.

http://download.java.net/jdk6/docs/...sertNodeInto(javax.swing.tree.MutableTreeNode,
javax.swing.tree.MutableTreeNode, int)
http://download.java.net/jdk6/docs/...eFromParent(javax.swing.tree.MutableTreeNode)

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top