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 / March 2006

Tip: Looking for answers? Try searching our database.

need to insert a parent in a jtree

Thread view: 
MikeB - 21 Mar 2006 15:26 GMT
I find lots of material on appending items to the end of a jtree but
how do I insert a node and make every thing to the left of it it's
children.

DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Child 1");
root.add(child1);
DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Child 2");
root.add(child2);

Let's say that the above code created my tree, but later on I want to
insert a new node after child1 and make child2 the child of the newly
inserted node. So the before and after will look like the below
example.

before:
root
+ child1
+ child2

after:
root
+ child1
+ newNode
  + child2
Thomas Weidenfeller - 21 Mar 2006 16:35 GMT
> I find lots of material on appending items to the end of a jtree

Surprising, because this is really not a a case which is much more
common than others.

But before going into details, you are actually not appending anything
to a JTree. A JTree is just a visual representation of you model. You
change the model, and let the JTree pick up the changes and display the
new data. The distinction is important, as I will point out in a second.

> Let's say that the above code created my tree, but later on I want to
> insert a new node after child1 and make child2 the child of the newly
[quoted text clipped - 11 lines]
> + newNode
>    + child2

In three steps:

    Remove child2 from its parent
    Add child2 as child to newNode
    Insert newNode at the desired position

Other sequences are also possible (e.g. first adding newNode, then
removing child2, than adding child2 to newNode).

The important thing is that the JTree is properly informed about the
changes in the model once the changes are done. You can either

* perform all operations via the TreeModel. Which, if correctly
implemented, will inform the JTree, or you can

* rearrange the nodes and notify the JTree afterwards.

Rearranging the nodes and notifying the JTree afterwards has the
advantage that you can limit the number of notifications which are fired
to the JTree. If you go via the model, you probably trigger two events
(for the remove and the insert).

So a code sequence which just fires one event would look something like

    DefaultMutableTreeNode parent, child1, child2, newNode;
    DefaultTreeModel tm;

    int ndx = parent.getIndex(child2); // remember where to insert
    parent.remove(pos);
    newNode.insert(child2, 0);
    parent.insert(newNode, pos); // reinsert at old pos. of child2

    // let the model inform the JTree about changes below parent
    tm.nodeStructureChanged(parent);

/Thomas
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

MikeB - 21 Mar 2006 19:04 GMT
Thanks Thomas for the quick response. I need some clarification about
your example, parent.remove(pos); is pos = ndx?
MikeB - 22 Mar 2006 16:59 GMT
disregard the last comment I got it to work
MikeB - 22 Mar 2006 16:59 GMT
disregard the last comment I got it to work
MikeB - 22 Mar 2006 16:59 GMT
disregard the last comment I got it to work


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.