No example source, but this is easy and straight-forward to program. I
would guess it can be done in 10 to 25 lines of code. E.g. roughly like
it follows:
== Assuming the level of the tree is initially unsorted ==
Get the tree node above the level you want to sort:
MutableTreeNode.getParent()
Get all children of that tree node (these are the nodes forming the
particular level): MutableTreeNode.children() or
getChildCount()/getChildrenAt().
Place the found tree nodes in some collection or array, and sort like
you would sort any other set of data. See
http://java.sun.com/docs/books/tutorial/collections/algorithms/index.html
for details.
Then remove the children from the parent node and re-add them, now in
the order of the result of the sort.
Finally, fire the necessary change event from the model to the JTree.
== Assuming your level is initially sorted and you want to add a node ==
Get all the siblings of the new node, as described above. Run a binary
search on them to find the insertion point (the Collection framework
provides binary search methods). From the found insertion point in the
collection/array calculate the child number and insert the new node at
that position at the parent node. If you do the insertion via the model
(and the model is programmed to fire the necessary events), then you
don't need to trigger the change event manually. If not, send the
necessary change event from the model to the JTree manually.
/Thomas
--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq