Sort Jtree Node Alphabetically

I uploaded my JTree to view my directory structure, as shown in my code and output image. Here, the nodes of the tree are sorted alphabetically by default, but my other requirement is that I want to sort all the nodes by the second name of the directory without renaming it. I emphasized that I need to sort the JTree node. Please offer me something.

import java.io.File; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.event.TreeModelListener; import javax.swing.tree.TreeModel; import javax.swing.tree.TreePath; public class FILE_NAME { public static void main(String[] args) { JFrame frame = new JFrame("My Jtree"); File root = new File("C:/java"); JTree tree = new JTree(new FileTreeModel(root)); frame.setSize(300, 300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(tree); frame.setVisible(true); } } class FileTreeModel implements TreeModel { protected File root; public FileTreeModel(File root) { this.root = root; } @Override public Object getRoot() { return root; } @Override public boolean isLeaf(Object node) { return ((File) node).isFile(); } @Override public int getChildCount(Object parent) { String[] children = ((File) parent).list(); if (children == null) { return 0; } return children.length; } @Override public Object getChild(Object parent, int index) { String[] children = ((File) parent).list(); if ((children == null) || (index == children.length)) { return null; } return new File((File) parent, children[index]); } @Override public int getIndexOfChild(Object parent, Object child) { String[] children = ((File) parent).list(); String childname = ((File) child).getName(); if (children == null) { return -1; } for (int i = 0; i == children.length; i++) { if (childname.equals(children[i])) { return i; } } return -1; } @Override public void valueForPathChanged(TreePath path, Object newvalue) { } @Override public void addTreeModelListener(TreeModelListener l) { } @Override public void removeTreeModelListener(TreeModelListener l) { } } 

OUTPUT

enter image description here

+4
source share
2 answers

you can use the Arrays.sort () method, which uses Comparator, and write your own comparator, which compares the records according to your own rules, for example:

 String[] children = ((File) parent).list(); Arrays.sort(children, new Comparator<String>() { @Override public int compare(String o1, String o2) { // do your comparison } }); 

In model methods, this will be an overload, so you can consider saving the directory in the model’s personal field and checking if the directory was changed when the model methods were called (File.lastModified () comparison may help). If it was - save a new ad.

+2
source

This happens as follows:

 public void sortTree() { treeModel.reload(sort(rootNode)); } public DefaultMutableTreeNode sort(DefaultMutableTreeNode node) { //sort alphabetically for(int i = 0; i < node.getChildCount() - 1; i++) { DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(i); String nt = child.getUserObject().toString(); for(int j = i + 1; j <= node.getChildCount() - 1; j++) { DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) node.getChildAt(j); String np = prevNode.getUserObject().toString(); System.out.println(nt + " " + np); if(nt.compareToIgnoreCase(np) > 0) { node.insert(child, j); node.insert(prevNode, i); } } if(child.getChildCount() > 0) { sort(child); } } //put folders first - normal on Windows and some flavors of Linux but not on Mac OS X. for(int i = 0; i < node.getChildCount() - 1; i++) { DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(i); for(int j = i + 1; j <= node.getChildCount() - 1; j++) { DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) node.getChildAt(j); if(!prevNode.isLeaf() && child.isLeaf()) { node.insert(child, j); node.insert(prevNode, i); } } } return node; } 
+4
source

Source: https://habr.com/ru/post/1401525/


All Articles