JTree sorting problem, how to sort

here is my code:
DefaultMutableTreeNode root=new DefaultMutableTreeNode("root");
                         DefaultMutableTreeNode childone=new DefaultMutableTreeNode("id");
                         DefaultMutableTreeNode person1=new DefaultMutableTreeNode("david");
                         DefaultMutableTreeNode p11=new DefaultMutableTreeNode("java programming");
                         DefaultMutableTreeNode p12=new DefaultMutableTreeNode("maths");
                         DefaultMutableTreeNode p13=new DefaultMutableTreeNode("c programming");
                         person1.add(p11);
                         person1.add(p12);
                         person1.add(p13);
                         childone.add(person1);
                         DefaultMutableTreeNode person3=new DefaultMutableTreeNode("ann");
                         DefaultMutableTreeNode p31=new DefaultMutableTreeNode("georgian");
                         DefaultMutableTreeNode p32=new DefaultMutableTreeNode("english");
                         DefaultMutableTreeNode p33=new DefaultMutableTreeNode("russian");
                         person3.add(p31);
                         person3.add(p32);
                         person3.add(p33);
                         childone.add(person3);
                         DefaultMutableTreeNode person2=new DefaultMutableTreeNode("merab");
                         DefaultMutableTreeNode p21=new DefaultMutableTreeNode("veterinar");
                         DefaultMutableTreeNode p22=new DefaultMutableTreeNode("building");
                         person2.add(p21);
                         person2.add(p22);
                         childone.add(person2);
                         DefaultMutableTreeNode person4=new DefaultMutableTreeNode("maia");
                         DefaultMutableTreeNode p41=new DefaultMutableTreeNode("medicine");
                         DefaultMutableTreeNode p42=new DefaultMutableTreeNode("pediatri");
                         DefaultMutableTreeNode p43=new DefaultMutableTreeNode("aaaa");
                         p41.add(p42);
                         p41.add(p43);
                         person4.add(p41);
                         childone.add(person4);
                         DefaultMutableTreeNode childtwo=new DefaultMutableTreeNode("fullname");
                         DefaultMutableTreeNode c1=new DefaultMutableTreeNode("merab");
                         DefaultMutableTreeNode c2=new DefaultMutableTreeNode("maia");
                         DefaultMutableTreeNode c3=new DefaultMutableTreeNode("ann");
                         DefaultMutableTreeNode c4=new DefaultMutableTreeNode("david");
                         childtwo.add(c1);
                         childtwo.add(c2);
                         childtwo.add(c3);
                         childtwo.add(c4);
                         DefaultMutableTreeNode childthree=new DefaultMutableTreeNode("email");
                         DefaultMutableTreeNode d1=new DefaultMutableTreeNode("[email protected]");
                         DefaultMutableTreeNode d2=new DefaultMutableTreeNode("[email protected]");
                         DefaultMutableTreeNode d3=new DefaultMutableTreeNode("[email protected]");
                         childthree.add(d1);
                         childthree.add(d2);
                         childthree.add(d3);
                         root.add(childthree);
                         root.add(childone);
                         root.add(childthree);
                         jTree1 = new JTree(root);
                         jScrollPane1.setViewportView(jTree1);
                     //   DefaultTreeModel ddd=(DefaultTreeModel)jTree1.getModel();
                        Comparator<DefaultMutableTreeNode> comp=new Comparator<DefaultMutableTreeNode>() {
                             public int compare(DefaultMutableTreeNode o1,DefaultMutableTreeNode o2) {
                                  return o1.toString().compareTo(o2.toString());
                        ArrayList<DefaultMutableTreeNode> arr=Collections.list(root.children());
                         Collections.sort(arr,comp);
                         root.removeAllChildren();
                        for(int i=0;i<arr.size();i++)
                             root.add(arr.get(i));now i want to sort all nodes and leafs that contains this tree , how should i do that? can anyone correct this code so that it sorts all data in this jtree?

We may extend DefaultMutableTreeNode to implement Comparable
and override insert so that the relevant nodes are sorted after each insert:
* TreeSort.java
import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;
public class TreeSort extends JFrame {
    private JTree jTree1;
    private JScrollPane jScrollPane1;
    public TreeSort() {
        super("TreeSort");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(400,300);
        setLocationRelativeTo(null);
        MyNode root = new MyNode("root");
        MyNode childone = new MyNode("id");
        MyNode person1 = new MyNode("david");
        MyNode p11 = new MyNode("java programming");
        MyNode p12 = new MyNode("maths");
        MyNode p13 = new MyNode("c programming");
        person1.add(p11);
        person1.add(p12);
        person1.add(p13);
        childone.add(person1);
        MyNode person3 = new MyNode("ann");
        MyNode p31 = new MyNode("georgian");
        MyNode p32 = new MyNode("english");
        MyNode p33 = new MyNode("russian");
        person3.add(p31);
        person3.add(p32);
        person3.add(p33);
        childone.add(person3);
        MyNode person2 = new MyNode("merab");
        MyNode p21 = new MyNode("veterinar");
        MyNode p22 = new MyNode("building");
        person2.add(p21);
        person2.add(p22);
        childone.add(person2);
        MyNode person4 = new MyNode("maia");
        MyNode p41 = new MyNode("medicine");
        MyNode p42 = new MyNode("pediatri");
        MyNode p43 = new MyNode("aaaa");
        p41.add(p42);
        p41.add(p43);
        person4.add(p41);
        childone.add(person4);
        MyNode childtwo = new MyNode("fullname");
        MyNode c1 = new MyNode("merab");
        MyNode c2 = new MyNode("maia");
        MyNode c3 = new MyNode("ann");
        MyNode c4 = new MyNode("david");
        childtwo.add(c1);
        childtwo.add(c2);
        childtwo.add(c3);
        childtwo.add(c4);
        MyNode childthree = new MyNode("email");
        MyNode d1 = new MyNode("[email protected]");
        MyNode d2 = new MyNode("[email protected]");
        MyNode d3 = new MyNode("[email protected]");
        childthree.add(d1);
        childthree.add(d2);
        childthree.add(d3);
        root.add(childtwo);
        root.add(childone);
        root.add(childthree);
        jTree1 = new JTree(root);
        jScrollPane1 = new JScrollPane();
        jScrollPane1.setViewportView(jTree1);
        add(jScrollPane1);
    public static void main(final String[] args) {
        Runnable gui = new Runnable() {
            public void run() {
                new TreeSort().setVisible(true);
        //GUI must start on EventDispatchThread:
        SwingUtilities.invokeLater(gui);
class MyNode extends DefaultMutableTreeNode implements Comparable {
    public MyNode(String name) {
        super(name);
    @Override
    public void insert(final MutableTreeNode newChild, final int childIndex) {
        super.insert(newChild, childIndex);
        Collections.sort(this.children);
    public int compareTo(final Object o) {
        return this.toString().compareToIgnoreCase(o.toString());
}Edited by: Andre_Uhres on Nov 22, 2008 3:47 PM
There seems to be a small bug in your original code: childthree is added twice and childtwo isn't added at all.

Similar Messages

  • I just updated my mac and when i went to open iphoto it says it "can not open because of a problem" how do i fix this?

    i just updated my mac and when i went to open iphoto it says it "can not open because of a problem" how do i fix this?

    To re-install iPhoto
    1. Put the iPhoto.app in the trash (Drag it from your Applications Folder to the trash)
    2a: On 10.5:  Go to HD/Library/Receipts and remove any pkg file there with iPhoto in the name.
    2b: On 10.6: Those receipts may be found as follows:  In the Finder use the Go menu and select Go To Folder. In the resulting window type
    /var/db/receipts/
    2c: on 10.7 they're at
    /private/var/db/receipts
    A Finder Window will open at that location and you can remove the iPhoto pkg files.
    3. Re-install.
    If you purchased an iLife Disk, then iPhoto is on it.
    If iPhoto was installed on your Mac when you go it then it’s on the System Restore disks that came with your Mac. Insert the first one and opt to ‘Install Bundled Applications Only.
    If you purchased it on the App Store you can find it in your Purchases List.

  • How do I find out what program / file is using 100 GB of space on my Hard drive?  Once I find the problem  how do I delete the specific files to increase GB of space?

    How do I find out what program / file is using 100 GB of space on my Hard drive?  Once I find the problem  how do I delete the specific files to increase GB of space?

    Use OmniDiskSweeper
    Once you find file, it can be deleted from within OmniDiskSweeper
    Allan

  • I have sent my iPod back for servicing because it is cracked, with some dispute with Apple I have managed to get a brand new iPod Touch. Just wondering if anyone has had this problem how long did it take from "Replacement Product Pending" to get bck to UK

    I have sent my iPod back for servicing because it is cracked, with some dispute with Apple I have managed to get a brand new iPod Touch. Just wondering if anyone has had this problem how long did it take from "Replacement Product Pending" to get back to the UK

    Not really. Is say "pending"
    pend·ing 
    /ˈpendiNG/
    Adjective 
    Awaiting decision or settlement.
    Preposition
    Until (something) happens or takes place: "they were released on bail pending an appeal".
    Synonyms
    adjective. 
    pendent - undecided - unsettled - outstanding - pendant
    preposition. 
    during - until - till - to

  • Hello guys I have a question,or rather a problem. how can I detach yourself iPhone operator Verizon USA? I mean as to call and what to say? I live in Ukraine, and the opportunity came up for to buy cheap 4S 16GB. but on Verizon. when I insert the original

    Hello guys I have a question,or rather a problem. how can I detach yourself iPhone operator Verizon USA? I mean as to call and what to say? I live in Ukraine, and the opportunity came up for to buy cheap 4S 16GB. but on Verizon. when I insert the original SIM card Verizon and I try to activate, says: (error occurred activation, if this problem happen again please call at +1................). this is the hour to use it through the R-SIM 9 pro. everything works нормальнои calls and Internet and SMS, but I want to officially unlock. beforehand thank you

    Well, you managed to find this support forum didn't you and you have an internet connection don't you?  Then if you have all these things, go and find the number by going to the Verizon website and it should not be hard to find the number.
    As for speaking English, this is not really our problem and it is surely something that you should have thought about before buying the phone in the first place.
    You also need to know that Verizon in the US is a cdma operator and if you purchased an iPhone 4s, then it should not be locked.  The Verizon 4s comes with a sim card tray that should be unlocked out of the box for use internatinoally on gsm networks, so all you should need to do is open the sim card tray and insert a sim card and you should pick up the local gsm network.
    A cdma phone does not actually use a sim card, so there would be no original sim card with the phone because when it is used in the US it will pick up the Verizon cdma network without a sim card.  The sim card is only for use on a gsm network which will not work in the US, but is designed to work outside the US.  Open the sim card tray and insert your own local sim card and the phone should work and should not be locked.
    Other than this, if you purchased the phone in the first place, it is the responsibility of the previous owner to cancel the Verizon contract.  This is nothing to do with you, so contact the seller and ask them.
    Other wise, go to the Verizon website and look up the number and call them.  As they are the phone company, ultimately, they are the only ones who can help you.

  • My phone has been losing calls and according to the apple store i need to reset it due to a software fault - i have done this and still have the same problems. how do i reset the phone without reinstalling the soft ware fault by way of icloud backup

    my phone has been losing calls and according to the apple store i need to reset it due to a software fault - i have done this and still have the same problems. how do i reset the phone without reinstalling the soft ware fault by way of icloud backup

    Well, it appears that your backup is corrupt, thus causing your issue. So, you'll have to restore as a new device, & not from backup. Follow this by syncing your content back to your phone:
    http://support.apple.com/kb/ht1414
    Do not restore from backup.

  • I want to install my photoshop 5.0 limited edition but the pc displays i should check if it is a 32 or 64 bit application. i changed my windows os  from xp to win7 64 bit (i think that is the problem) how can i run my photoshop on this system?

    i want to install my photoshop 5.0 limited edition but the pc displays i should check if it is a 32 or 64 bit application. i changed my windows os  from xp to win7 64 bit (i think that is the problem) how can i install and run my photoshop 5.0 le on this system?

    Kglad Creative Suite 2 is only applicable to individuals affected by the activation server shut down.  A complete list of affected software titles can be found at Activation server shut down for Creative Suite 2, Acrobat 7, and Macromedia products.

  • Help : After upgrade to IOS5, I only can use my iphone 3gs in Spore instead of Msia. Any1 know what is the problem & how to solve it?!

    I bought iphone 3gs in singapore last year,I had no problem to use it in Singapore & Malaysia but after i upgrade to ios5,i can't use it in malaysia.It will automatic restart my phone itself. Is it because of IOS5 problem?!Because when I register my itune a/c, I selected the country is Singapore. Anyone know what is the problem & how can I solve it?!

    If it automatically restarts the phone firmware is corrupt. Restore the phone with iTunes and set up as a new phone, do not restore your backup, which is likely where the corruption is. If it then works you can try restoring just the backup. If that breaks it again you know where the problem is; you will have to start over and manually reinstall your content.
    To restore just the backup, right click on the phone's name in iTunes and select "restore from backup"

  • I have iphone 4s  and I got wifi problem how i can fix it

    I have iphone 4s  and I got wifi problem how i can fix it

    Tap Settings App > General > Reset > Reset Network Settings. See if fixed. If still problem, Restore iPhone with iTunes 10.7 on computer. See if fixed. If still problem and there is Warranty or AppleCare make Genius reservation and take iPhone to Apple for resolution.

  • On ipad 2 ps touch just keeps crashing it appears from reviews this is a common problem, how does one access a refund

    On ipad 2 ps touch just keeps crashing it appears from reviews this is a common problem, how does one access a refund

    As stated, completely exit pst (don't just leave the app) and then activate airplane mode. Then open PST and change your cloud sync option to "off". Once you deactivate airplane mode, you should be able to open PST again without the crash. Iirc, I had to work at opening PST and accessing my settings very quickly or it would still crash before opening the settings page. Unfortunately, we have to completely forget about cloud sync until the bug is fixed. Save any image files back to the ios camera roll to share with other apps or upload elsewhere.

  • Help my hard drive is being taken up by about 200gb of "other" memory, i am running time machine on an imac does does cause the problem how can i get rid of all "other" memory

    my problem is that most of my imac hard drive is being taken up by 200gb of "other' memory
    i am running time machine does this contribute
    how can i get rid of all "other" memory
    thanks

    JKOL96 wrote:
    i am running time machine does this contributex
    Some, possibly, but shouldn't be a problem.
    how can i get rid of all "other" memory
    Much of it is OSX and other things needed to run your Mac.
    See The Storage Display for an explanation, and don't miss the link to Where did my Disk Space go?

  • It's an audio problem, how can i fixed it?

    hey, why my imac 10.7.4 suddenly don't give a sound. the volume icon cannot be click, it's an audio problem, how can i fixed it?

    this should only happen if you have a digital toslink connected to your audio jack.  is there a red light coming out of it?
    have you tried restarting the computer?
    does your system see your audio device in about this mac more info audio?

  • My bookmarks are lost after turning off the computer unexpectedly (power problem),how can it recovers?

    My bookmarks are lost after turning off the computer unexpectedly (power problem),how can it recovers? Thanks a lot !!!!
    我的书签在电脑突然被断电之后全部丢失了,我该怎么办呢??谢谢啦!!~~~~

    This can also be a problem with the file places.sqlite that stores the bookmarks and the history.
    * http://kb.mozillazine.org/Locked_or_damaged_places.sqlite
    * http://kb.mozillazine.org/Lost_bookmarks

  • Need help: Flex  record webcam problem, how to setting Brightness,Contrast,Saturation and Sharpness

    Hello, dear all
    Please help me!
    Flex  record webcam problem, how to setting Brightness,Contrast,Saturation and Sharpness?
    nsOutGoing=new NetStream(nc);
    nsOutGoing.attachCamera(m_camera);
    nsOutGoing.publish(filename, "record");
    I want to control the Brightness,Contrast,Saturation and Sharpness for the recorded flv file.
    At present, I only can control the videodisplay object, but I can not able to control Camera.
    Thanks very much!!
    kimi
    MSN: [email protected]

    Can I change a Video object to to Camera object, If yes, How do??
    nsOutGoing.attachCamera(video as Camera);// it does not work rightly
    thanks

  • JDialog Problem: How to make a simple "About" dialog box?

    Hi, anyone can try me out the problem, how to make a simple "About" dialog box? I try to click on the MenuBar item, then request it display a JDialog, how? Following is my example code, what wrong code inside?
    ** Main.java**
    ============================
    publc class Main extends JFrame{
    public Main() {
              super();
              setJMenuBar();
              initialize();
    public void setJMenuBar()
         JMenuBar menubar = new JMenuBar();
            setJMenuBar(menubar);
            JMenu menu1 = new JMenu("File");      
            JMenuItem item = new JMenuItem("About");      
            item.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                  // About about = new About(this);
               // about.show();
            menu1.add(item);
         menubar.add(menu1);
    public static void main(String args[]){
         Main main = new Main();
    }** About.java**
    =============================
    import java.awt.*;
    import javax.swing.*;
    public class About extends JDialog{
         JPanel jp_top, jp_center, jp_bottom;
         public About(JFrame owner){
              super(owner);
              setDefaultCloseOperation( DISPOSE_ON_CLOSE );
              Image img = Toolkit.getDefaultToolkit().getImage(DateChooser.class.getResource("Duke.gif"));          
              setIconImage( img );
              setSize(500,800);
              Container contentPane = getContentPane();
             contentPane.setLayout(new BorderLayout());           
             contentPane.add(getTop(), BorderLayout.NORTH);
              contentPane.add(getCenter(), BorderLayout.CENTER);
              contentPane.add(getBottom(), BorderLayout.SOUTH);
              setResizable(false);
               pack();            
               setVisible(true);
         public JPanel getTop(){
              jp_top = new JPanel();
              return jp_top;
         public JPanel getCenter(){
              jp_center = new JPanel();
              return jp_center;
         public JPanel getBottom(){
              jp_bottom = new JPanel();
              jp_bottom.setLayout(new BoxLayout(jp_bottom, BoxLayout.X_AXIS));
              JButton jb = new JButton("OK");
              jp_bottom.add(jb);
              return jp_bottom;
    }

    Code looks reasonable except
    a) the code in the actionPerformed is commment out
    b) the owner of the dialog should be the frame
    If you need further help then you need to create a "Short, Self Contained, Compilable and Executable, Example Program (SSCCE)",
    see http://homepage1.nifty.com/algafield/sscce.html,
    that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

  • I have a problem how can I solve it I want give permissions to groupA to edit the people picker and I want to restrict groupB to edit the people picker what is the solution boss.. in InfoPath form is it possible..

     i have  a problem how can I solve it I want give permissions to groupA to edit the people picker and I want to restrict groupB to edit the people picker what is the solution boss.. in InfoPath form is it possible..

    Hi,
    To hide/disable controls based on user group in an InfoPath form, a solution is that we can call User Profile Service to check the group of current user, then hide/disable
    specific controls by setting some rules in form.
    Here is a demo with steps in details would be helpful to you:
    http://blog.symprogress.com/2011/05/infopath-list-form-hidedisable-fields-based-on-sharepoint-group-membership/
    More information about checking if a user is a member in a SharePoint group within web InfoPath 2010 forms:
    http://www.hishamqaddomi.ca/spg/index.php/sharepoint-2010/infopath-2010/65-checking-if-a-user-is-a-member-in-a-sharepoint-group-within-web-infopath-2010-forms
    Thanks 
    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact
    [email protected]

Maybe you are looking for

  • How do I remove an old - unworking computer from my home sharing  account

    I recently had a PC expire.   It was on my list of computers that made up my home sharing account. I need to remove that old computer from that account so that I can add the new computer. The old computer does not work anymore so I cannot access the

  • External table in Oracle 10g and Windows server 2003 problem

    I'm having a problem accessing an external table in an Oracle 10g and Windows 2003 server environment. The disk is remote (mapped) to the server. It's the usual access errors, kup-04001 or kup-04063. I have the [seemingly] exact same setup in an Orac

  • Satellite P70-A-120 - Win 8.1 booting problem

    Hello. I have a problem with the boot of my Satellite P70. The first time that I push the power button, it appears the screen with the Toshiba logo but Windows 8.1 doesnt start. If I power off the pc with the power button and push the power button an

  • Bookmark within a page

    I need some helps here... What I want to do is put some bookmarks on a portal page which combines portlets and items. For example, in a page, you have the "Back to Top" on each section break and it will take you back to top of the page. I know how to

  • Time Slicing in Enterprise Beans

    Hi All, I am using Weblogic 6.1 as App Server with SQL Server 7.0 as backend. I want to make use of Time Slicing concepts while executing any beans involved in my application? I also use JDBC Transactions in my code. Say I have around 80 Session Bean