> 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