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 / GUI / February 2005

Tip: Looking for answers? Try searching our database.

Jtree nodes alphabetically

Thread view: 
Ike - 10 Feb 2005 14:37 GMT
Does anyone know where I can find example source of sorting nodes in a JTree
alphabetically? That is, I want to lexicographically sort all nodes at a
given level on a branch in a JTree. This seems like a common need - is there
source or example anyone is aware of of this? Thanks, Ike
Thomas Weidenfeller - 10 Feb 2005 15:32 GMT
> Does anyone know where I can find example source of sorting nodes in a JTree
> alphabetically?

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

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq

Ike - 10 Feb 2005 17:47 GMT
private void addNodeInSortedOrder(DefaultMutableTreeNode parent,
DefaultMutableTreeNode child){
       int n = parent.getChildCount();
       if(n==0){
           parent.add(child);
           return;
       }
       DefaultMutableTreeNode node=null;
       for(int i=0;i<n;i++){
           node = (DefaultMutableTreeNode)parent.getChildAt(i);
           if(node.toString().compareTo(child.toString())>0){
               parent.insert(child, i);
               return;
           }
       }
       parent.add(child);
       return;
   }


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.