Sort JTree alphabetical

Hello everyone! I am trying to sort a JTree alphabetical. For the moment i made only the roots (partitions) to sort. I use the JTree to view all the partitions and view all the files. I override the interface TreeNode for my needs...and i made a class for cell renderer...
public class FileTreeNode implements TreeNode
     public File file;
     private File[] children;     
     private  TreeNode parent;     
     public boolean isFileSystemRoot;     
     public FileTreeNode(File file, boolean isFileSystemRoot, TreeNode parent)
          this.file = file;
          this.isFileSystemRoot = isFileSystemRoot;
          this.parent = parent;
          this.children = this.file.listFiles();
          if (this.children == null)
               this.children = new File[0];
     public FileTreeNode(File[] children)
          this.file = null;
          this.parent = null;
          this.children = children;               
     @Override
     public Enumeration<?> children()
          final int elementCount = this.children.length;
          return new Enumeration<File>()
               int count = 0;
               public boolean hasMoreElements()
                    return this.count < elementCount;
               public File nextElement()
                    if (this.count < elementCount)
                         return FileTreeNode.this.children[this.count++];
                    throw new NoSuchElementException("Vector Enumeration");
     @Override
     public boolean getAllowsChildren()
          return true;
     @Override
     public TreeNode getChildAt(int childIndex)
          return new FileTreeNode(this.children[childIndex],this.parent == null, this);
     @Override
     public int getChildCount()
          return this.children.length;
     @Override
     public int getIndex(TreeNode node)
          FileTreeNode ftn = (FileTreeNode) node;
          for (int i = 0; i < this.children.length; i++)
               if (ftn.file.equals(this.children))
                    return i;                    
          return -1;
     @Override
     public TreeNode getParent()
          return this.parent;
     @Override
     public boolean isLeaf()
          return (this.getChildCount() == 0);
     public void sortRoots()
          List<File> list = Arrays.asList(this.children);
          Collections.sort(list);
          System.out.println("Partitions: " + list);
and the other class....public class FileTreeCellRenderer extends DefaultTreeCellRenderer
     private static final long serialVersionUID = 1L;
     protected static FileSystemView fsv = FileSystemView.getFileSystemView();
     private Map<String, Icon> iconCache = new HashMap<String, Icon>();          
     private Map<File, String> rootNameCache = new HashMap<File, String>();     
     public Component getTreeCellRendererComponent(JTree tree, Object value,
               boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)
          FileTreeNode ftn = (FileTreeNode) value;
          File file = ftn.file;
          String filename = "";
          if (file != null)
               if (ftn.isFileSystemRoot)
                    filename = this.rootNameCache.get(file);
                    if (filename == null)
                         filename = fsv.getSystemDisplayName(file);                         
                         this.rootNameCache.put(file, filename);
               else
                    filename = file.getName();
          JLabel result = (JLabel) super.getTreeCellRendererComponent(tree,
                    filename, selected, expanded, leaf, row, hasFocus);
          if (file != null)
               Icon icon = this.iconCache.get(filename);
               if (icon == null)
                    icon = fsv.getSystemIcon(file);
                    this.iconCache.put(filename, icon);
               result.setIcon(icon);
          return result;
and i run the application...File[] roots = File.listRoots();
FileTreeNode rootTreeNode = new FileTreeNode(roots);
tree = new JTree(rootTreeNode);
tree.setCellRenderer(new FileTreeCellRenderer());
can anyone please help me to sort alphabetical all the files from all the nodes...any help will be appreciated.
Thanks.
Calin                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

might be easier if you were to add a small pictorial
e.g. if you have a no-arg constructor JTree, you get this
JTree
      colors
               blue
               violet
               red
               yellow
      sports
               basketball
               soccer
               football
               hockey
      food
               hot dogs
               pizza
               ravioli
               bananasnow put it how you would like it sorted

Similar Messages

  • How to sort jTree Nodes

    Hi,
    I am interested in whats the easiest way to sort jTree Nodes alphabetically. Is there any Method to do this directly or may i use something like quicksort?
    Thanks for help
    Mirco

    Thanks Hamed,
    Well, I work in your code, and I post here.
      public static DefaultMutableTreeNode sortTree(DefaultMutableTreeNode root) {
                     for (int i =0; i<root.getChildCount()-1; i++) {
                              DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(i);
                            String nt = node.getUserObject().toString();
                            for (int j=i+1;j<=root.getChildCount()-1;j++)
                                     DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) root.getChildAt(j);
                                        String np = prevNode.getUserObject().toString();
                                System.out.println(nt+" "+np);
                                if (nt.compareToIgnoreCase(np)>0) {
                                               root.insert(node, j);
                                             root.insert(prevNode, i);
                            if (node.getChildCount() > 0) {
                        node = sortTree(node);
                     return root;
        }and this code I used after include in jtree
      public  void jTreeSortingBegin()
              dn = (DefaultMutableTreeNode)treeModel.getRoot();
              treeModel.reload();
              dn = sortTree(dn);
              treeModel.reload(dn);
        }for exemple
      addObject(jTextField1.getText());
            jTreeSortingBegin();

  • Payment Proposals list, needs to be sorted by alphabetically

    Every report needs to be sorted differently, but Payment Proposals list, needs to be sorted by alphabetically  listing
    pls provide solution

    Every report needs to be sorted differently, but Payment Proposals list, needs to be sorted by alphabetically  listing
    pls provide solution

  • Sorting in alphabetical order

    How can I sort paragraphs alphabetically in Pages 09?

    Inside the WordService 2.7 download folder is a file called ReadMe.rtf double click it and it should open in Text Edit and says:
    "Installation:
    +Copy WordService into the directory "Services" (located in the Library folder of your home directory), then log out and log in again. If there's no such folder, just create one using the Finder."+
    In the side bar on the lefthand side of every open Finder window should be a little house icon probably with your name next to it. This is your computer account.
    Click on it and find the folder labeled Library which should contain another folder called Services. If you don't have a Services folder create one:
    +Menu > New Folder > click on it and name it "Services"+
    Drag WordService.service from your downloaded WordService folder into the above folder. +Do not+ drag the whole folder in.
    Now you will find the contained services under every application (including Finder) menu (eg the Pages menu) in a submenu called Services. There are many services in the menu. The specific services you want are +Format > Sort Lines Ascending/Descending.+
    Peter

  • Parameters to be sorted in alphabetical order

    Hi,
    Can you please let me know whether the parameters values are by default sorted in alphabetical order...refering to discoverer viewer/plus...
    Thanks in advance
    Sri

    If you are referring to the item classes (list of values), they would be sorted in alphabetical order. The parameter values wud appear in the same order in which the lovs appear in admin.
    If you want to customize the sorting (reorder), you can do so using alternate sort tab in edit item class.
    you can refer to the admin users guide.

  • My Recently Added is now sorted by Alphabetical not Date added

    Hi there,
    So i just plugged my iphone in to update my music and i went to click recently added to listen to some but for some reason it was sorted in alphabetical rather than date added, i really need the function of date added to i can get all the newest music i put on.
    If anyone can help that would be great

    I have just redone my recentl added playlist and ordered it by date. When i sync to my iPhone it displays in the same date order
    Try recreating the recently added playlist
    Here is my Smart Playlist. Looking at it again if you see i have the limit items selected by most recently added. Maybe that is what is different

  • HT4089 Can I sort documents alphabetically in IOS Pages?

    Can I sort documents alphabetically in IOS Pages?

    You can do it. Open Pages and, in the window that allows you to open a document, scroll up and you will see a button to order your documents by name

  • Is there anyway to sort faces Alphabetically in "Photos"

    Is there any way to Sort Faces alphabetically in "Photos"

    No, only by dragging the faces manually into the alphabetical order on the Faces tile. The are sorted by the most frequent use.

  • Sort Hierarchy Alphabetically

    How can I sort a hierarchy alphabetically? The hierarchy structure is extracted from 3rd party source system and the records in same level sorted according to nodename. In the reports I can sort alphabetically according to text but when I create a Bex variable to choose the hierarchy, the hierarchy structure in the selection screen is sorted according to nodename and I don't see any option to sort it alphabetically according to its medium description.

    Did you solved it?

  • How can I sort Notes, alphabetically, on iPhone 4s with IOS 8?

    How can I sort Notes, alphabetically, on Phone 4s with IOS 8?
    I just got iCloud setup and am synching between icloud and iphone.  I have loaded some notes to convert over from blackberry.
    So how can I sort Notes alphabetically on iPhone??
    Was easy to do on blackberry and but I can't figure out how to do that on iPHone
    What am I missing?

    Notes are sorted by the time on the iPhone. There is no way to re-sort them.
    Cheers,
    GB

  • I have Firefox 3.6.12. How do I alphabetize bookmarks. I go to sort, alphabetize, and then when I open bookmarks, they are not sorted, not alphabetized.

    My question is the same as above. I "sort" by bookmarks in alphabetical order, then go to open all my bookmarks and they are not in alphabetical order. Some bookmarks are single; some are in folders.
    I have a long list of bookmarks and it's taking forever to find a bookmark because they're not in order. You can email me at <email removed - c>
    Thanks

    See http://kb.mozillazine.org/Sorting_and_rearranging_bookmarks_-_Firefox
    The [https://addons.mozilla.org/firefox/addon/9275/ SortPlaces] add-on may be of interest to you, it gives greater control over sorting bookmarks.

  • How to sort Jtree node?

    Now, I want to sort tree node by increasing.
    for example there are three child node:
    [20 -40][
    10 - 20]
    [30- 60].
    they are added to JTree node by different order.but are shown in increasingorder.
    like this:
    [10 - 20]
    [20 -40][
    [30- 60].
    how to control tree node.

    Implement the Comparable interface in t he userobject class and possibly the toString(0 as well.
    Ex.
    public class UserObject implements java.lang.Comparable{
    public int compareTo(Object obj) throws ClassCastException
    public String toString()

  • How can I sort memos alphabetically?

    I have over 300 memos and want them to be sorted alphabetically. This way, it is easier for me to find a particilar memo or note. How can I set Memo this way?

    rmpete wrote:
    But how do you sort the items within a single memo?
    Post relates to: Visor Prism
    You can't.
    Sounds like you need to create a spreadsheet with Docs To Go to enable sorting line by line...
    WyreNut
    Post relates to: Centro (AT&T)
    I am a Volunteer here, not employed by HP.
    You too can become an HP Expert! Details HERE!
    If my post has helped you, click the Kudos Thumbs up!
    If it solved your issue, Click the "Accept as Solution" button so others can benefit from the question you asked!

  • Playlist sorting not alphabetical?

    Can someone explain to me the method the iPod uses to sort a list of playlists? I can't make any sense of it, and I'm trying to name them so they show in a particular order.
    For instance, here's the list of playlists, and how they are sorted in iTunes:
    Apparently some characters in my playlist names won't appear in the apple forums(some asterisks), so here's a screenshot of the rest:
    http://homepage.mac.com/ltcarter47/images/playlistsorting.png
    Message was edited by: Sean C. Carter

    As I found, playlists are sorted in their creation order within iTunes. I use iTunes on Mac, though my iPod is Windows formatted. I'm adding a new folder within another (existing) folder. Then I create subfolders and playlists within these newly created subfolders.
    As a result, new folder in iTunes is positioned alphabetically within other peer-nested folders, but in my iPod (Classic 160G) this newly created folder appears as last. As for inner subfolders they are positioned just as in iTunes!
    So, I can conclude, that the wrong positioning appears ONLY when there are EXISTING (and already alphabetically sorted) folders, and we add A NEW folder to the same nesting level.
    I see the only possible reason -- the iTunes software has a bug -- while synching it should INSERT a new folder BETWEEN other existing folders but it only ADDS it to the folders list end.
    As a solution I'll try to uncheck a root-level folder during synchronisation, then synch, then check it back and synch again and then look at the results. I yet not tried to do it, but I'll try today. I think it should resolve the issue.
    If I'll have success then what wee need are two things:
    -- uncheck all checks at the level, where a new folder has been added besides the first (in alphabetic order) folder, and synch (so we get a folder with the only "first" subfolder) , then check them back as you need and synch again. Now all other folders should be added in proper alphabetic order.
    -- wait for a iTunes update, resolving this pretty obvious bug.
    The only question appears -- do Apple coders themself use iPods or not??? I beleave they use iRiver :)))

  • Sort in Alphabetical order

    Hi everyone,
    I am new for Forum. I have an doubt. How can we sort a list of text in alphabetical order in Indesign. Actually in TOC there is an option but it is not correct like word or Excel. Any other good option.

    What are you trying to sort? And what is wrong with the sorting you get?

Maybe you are looking for