I've been trying to sort my nodes in my JTree for a few days now, but with no success. Here is my code to populate the JTree with a structure of a given folder. This is working fine: all the folders are shown in alphabetic order but not the files inside the folders.
DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) {
File[] tmp = dir.listFiles();
Vector<File> ol = new Vector<File>();
ol.addAll(Arrays.asList(tmp));
// Pass two: for files.
for (int fnum = 0; fnum < ol.size(); fnum++) {
File file = ol.elementAt(fnum);
DefaultMutableTreeNode node = new DefaultMutableTreeNode(file);
if (file.isDirectory()) {
addNodes(node, file);
}
curTop.add(node);
}
return curTop;
}
Any help on this would be really great.
dir.listFiles()
- doesn't guarantee order of files, because of you need to sort it by yourself like next: