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 / General / November 2006

Tip: Looking for answers? Try searching our database.

JTree oddity...

Thread view: 
dushkin - 19 Nov 2006 14:33 GMT
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
Andrew Thompson - 19 Nov 2006 14:47 GMT
...
(snip snippets)

> Can someone advise please?

I advise you to post an SSCCE*, to c.l.j.gui, rather than
that mess of code snippets, to c.l.j.programmer.

* <http://www.physci.org/codes/sscce/>

> ...Thanks

No worries.

Andrew T.
Thomas Hawtin - 19 Nov 2006 15:10 GMT
>                     DefaultMutableTreeNode  mapMutNode =
> selPath.getLastPathComponent();
[quoted text clipped - 5 lines]
> !!!
> 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
dushkin - 19 Nov 2006 17:15 GMT
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???
Thomas Hawtin - 19 Nov 2006 17:42 GMT
> 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/api/javax/swing/tree/DefaultTreeModel.html#in
sertNodeInto(javax.swing.tree.MutableTreeNode
,
javax.swing.tree.MutableTreeNode, int)
http://download.java.net/jdk6/docs/api/javax/swing/tree/DefaultTreeModel.html#re
moveNodeFromParent(javax.swing.tree.MutableTreeNode
)

Tom Hawtin
dushkin - 20 Nov 2006 08:07 GMT
Thanks Tom! It really helped me!


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.