• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

sorting jtree

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
pavan in
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic