This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes sorting jtree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "sorting jtree" Watch "sorting jtree" New topic
Author

sorting jtree

pavan in
Ranch Hand

Joined: Oct 22, 2002
Posts: 64
I have a jtree which is created dynamically. i.e, initially jtree has root and it's children. When the user expands the child, it's children are added after getting data from server. I am using TreeWillExpand listener for doing this task.
My problem is, if the node has lot of children(example -> 300,000 nodes), then the jtree is taking lot of time(10 min!!..) to add and show to user. Any one has sorting algorithm which will reduce the time?
I am using -
((DefaultTreeModel)jtree.getModel).insertNodeInto(childNode,parentNode,index);
to add children.
where index is obtained by looping through each of its siblings and matching the alphabet of current node with other nodes alphabet. (I think this is taking looot of time because it has to compare each and every node before adding the node at the specific place)
Thanks.
Chris Shepherd
Ranch Hand

Joined: Jun 27, 2000
Posts: 286
You say you are getting this data from a server. Is it actually coming out of a database? If its a database, you should be able to include an ORDER BY in your SQL statement.
If its not a database, whats the data coming from?
pavan in
Ranch Hand

Joined: Oct 22, 2002
Posts: 64
Even though the data is being retrieved from, the data contains three information. One is the display id, one is sequence number and parent id (like 10`1`1^20`2`1 etc). I add the node to its parent based on the parent id. So, the data is ordered by display id and I want the nodes to be ordered by sequence number. I can not change the data retrieval as the sql queries is provided by different users (and hence can not be changed)
Any other thought?
Thanks.
Chris Shepherd
Ranch Hand

Joined: Jun 27, 2000
Posts: 286
Are your sequence numbers unique or is ot possible to have duplicates?
You might also try to time your sql requests to see how much of that time you mentioned is required just to process the requests.
[ February 24, 2003: Message edited by: Chris Shepherd ]
pavan in
Ranch Hand

Joined: Oct 22, 2002
Posts: 64
The sequence numbers are unique.
The database query took 56sec. while the applet took approx 10min to build. It should not take that much time, isnt it? I think the time taken is while finding the index.
Ashish Mahajan
Ranch Hand

Joined: Feb 19, 2003
Posts: 77
Hi Pravin,
Seems that u r currently facing with 2 probs :-
1. Faster sorting model
2. Performance issues (i.e. even if u don't use Sorting it is taking time)
For prob.no. 2, take a look at the foll. link :- http://developer.java.sun.com/developer/community/chat/JavaLive/2003/jl0121.html
Actually prob.no. 1 and 2 can be solved if we write a custom MutableTreeNode that internally uses SortedSet for storing children. Java's DefaultMutableTreeNode uses Vector as internal collection. The reason is that Vector and Hashtable were released (i think) with JDK1.1 and the Collection Framework was released with JDK1.2. Since Collection framework was not available during development of swing library they heavily used Vector and Hashtable. U can see the probs arises with JList, JComBox, JTable etc.. Needless to say Vector is very poor performance wise.
Recently we faced similar probs with JList. The list would have to be populated with millions of data. When we started with list's default Vector it was taking terrible time even for standalone version. We thought of providing progress bar. But later we wrote our custom SortedListModel (not a big stuff) but improved the performance so amazingly that now there was no need of progress bar and the millions of records were displayed within few sec. Note that this is a standalone application
Hope this helps


The best teams have no specialists, only general contributors with special skills
pavan in
Ranch Hand

Joined: Oct 22, 2002
Posts: 64
Thanks Ashish.
I have class myDefaultMutableTreeNode which extends DefaultMutableTreeNode. Will it help if I override insertNpde() method as suggested in -
http://forum.java.sun.com/thread.jsp?forum=57&thread=189640
I just could not think of way to use the insertNode(MutableTreeNode m, int index) because, what should be the index specfied in my program while adding the node?
Ashish Mahajan
Ranch Hand

Joined: Feb 19, 2003
Posts: 77
Hi Pavan,
It is always best to create custom MutableTreeNode which uses SortedSet as internal collection. Moreover ur program is Applet and hence will run on network thus won't be fast. Hence it is highly recommended to write SortedMutableTreeNode. But in worst case if that is not possible then use the foll code posted in the link u provided :-
----------------------------------------------
Posts: 44 | Registered: Oct 2002 | IP: Logged

Re: Sorting Elements in Tree alphabetically
Author: PRoCeDuR
In Reply To: Re: Sorting Elements in Tree alphabetically Feb 28, 2002 3:16 AM
Reply 9 of 13


It's better to overwrite the insert(...) Method. Add is only calling the insert Method so it's not necessary to overwrite it:
public void insert(MutableTreeNode newChild, int childIndex)
{
super.insert(newChild, childIndex);
Collections.sort(this.children, nodeComparator);
}
It also looks better if you're ignoring case sensitivity:
protected static Comparator nodeComparator = new Comparator ()
{
public int compare(Object o1, Object o2)
{
return o1.toString().compareToIgnoreCase(o2.toString());
}
public boolean equals(Object obj)
{
return false;
}
}
;
----------------------------------------
What the code actually doing is to insert the passed in object into the Vector by calling super's version and then simply sorting the collection with the help of utility class Collections.
Hope this helps
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: sorting jtree
 
Similar Threads
JTree
Searching a JTree
Expand Lazy Loaded JTree
Lazy fetching JTree
JTree, adding and removing nodes contained in a master tree