Organizing Nodes in JTree

I have a JTree in which the user can drag and drop nodes, when I save, I have to re-arrange the nodes so that file type nodes appear in front of the folder type nodes. I do not need to sort the file / folder name.

User tree:

-FolderA +FFA1 -FA1 -FA2 -FolderB -FB1 -File1 -File2 +FolderC -File3 

Resulting tree:

 -File1 -File2 -File3 -FolderA -FA1 -FA2 +FAF1 -FolderB -FB1 +FolderC 

I have the following codes below, it worked, but I don't know if this is possible correctly or good practice. Can you suggest which of the two solutions is better, or can you suggest another way.

Thank you very much.

Solution 1:

 private void arrange(DefaultMutableTreeNode parent){ DefaultMutableTreeNode sorted = new DefaultMutableTreeNode(); List<DefaultMutableTreeNode> files = new ArrayList<DefaultMutableTreeNode>(); List<DefaultMutableTreeNode> folders = new ArrayList<DefaultMutableTreeNode>(); for (int i = 0; i < parent.getChildCount(); i++){ DefaultMutableTreeNode node = (DefaultMutableTreeNode) parent.getChildAt(i); int type = ((BusinessObject) node.getUserObject()).getType(); if (type == BusinessObject.FILE) files.add(node); else{ arrange(node); folders.add(node); } } for (int i = 0; i < files.size(); i++) sorted.add((DefaultMutableTreeNode) files.get(i)); for (int i = 0; i < folders.size(); i++) sorted.add((DefaultMutableTreeNode) folders.get(i)); while (sorted.getChildCount() > 0) parent.add((DefaultMutableTreeNode) sorted.getChildAt(0)); sorted = null; files = null; folders = null; } 

Solution 2:

 private void arrange(DefaultMutableTreeNode parent){ DefaultMutableTreeNode sorted = new DefaultMutableTreeNode(); List<DefaultMutableTreeNode> nodes = new ArrayList<DefaultMutableTreeNode>(); for (int i = 0; i < parent.getChildCount(); i++){ DefaultMutableTreeNode node = (DefaultMutableTreeNode) parent.getChildAt(i); int type = ((BusinessObject) node.getUserObject()).getType(); if (type == BusinessObject.FILE) nodes.add(node); } for (int i = 0; i < parent.getChildCount(); i++){ DefaultMutableTreeNode node = (DefaultMutableTreeNode) parent.getChildAt(i); int type = ((BusinessObject) node.getUserObject()).getType(); if (type == BusinessObject.FOLDER){ arrange(node); nodes.add(node); } } for (int i = 0; i < nodes.size(); i++) sorted.add((DefaultMutableTreeNode) nodes.get(i)); while (sorted.getChildCount() > 0) parent.add((DefaultMutableTreeNode) sorted.getChildAt(0)); sorted = null; nodes = null; } 
+4
source share
5 answers

I think both are great solutions. It was pretty easy to say what they were doing: pull out the files, pull out the folders and throw them back into the tree in the correct order. In addition, the recursive call was direct and intuitive.

Choose what seems most natural to you. The second seems more like the way I did it, but it's just me, and there is not much there.

Are you using Java 5 or 6? If so, use for each cycle . In addition, you do not need to clear the values โ€‹โ€‹of your personal variables at the end of the method. They disappear when the method returns.

+3
source

this really works as I use it in my application. My sites all by default cannot be changed without changes. The sortable node is passed and changed. He makes arraylist child nodes. Then two arraylists text nodes that are sorted. Another is used to search for nodes in arralylist. Nodes are all moved from the starting node, and then added back from the arraylist. It works with charm, it may be redundant to use an arraylist, but I like them.

 private void sortchildren(DefaultMutableTreeNode node) { ArrayList children = Collections.list(node.children()); // for getting original location ArrayList<String> orgCnames = new ArrayList<String>(); // new location ArrayList<String> cNames = new ArrayList<String>(); //move the child to here so we can move them back DefaultMutableTreeNode temParent = new DefaultMutableTreeNode(); for(Object child:children) { DefaultMutableTreeNode ch = (DefaultMutableTreeNode)child; temParent.insert(ch,0); cNames.add(ch.toString().toUpperCase()); orgCnames.add(ch.toString().toUpperCase()); } Collections.sort(cNames); for(String name:cNames) { // find the original location to get from children arrayList int indx = orgCnames.indexOf(name); node.insert((DefaultMutableTreeNode)children.get(indx),node.getChildCount()); } } 
+3
source

I slightly modified the sample Mike code to account for duplicate names and sort folders in front of files. Otherwise, it worked like a dream. Thanks to Mike.

 public static void sortTreeNode(DefaultMutableTreeNode node) { List<DefaultMutableTreeNode> children = Collections.list(node.children()); List<String> sortFileNames = new ArrayList<>(); List<String> sortFolderNames = new ArrayList<>(); List<String> sortNames = new ArrayList<>(); List<String> origNames = new ArrayList<>(); DefaultMutableTreeNode temParent = new DefaultMutableTreeNode(); for (int x = 0; x < children.size(); x++) { DefaultMutableTreeNode child = children.get(x); temParent.insert(child, 0); if (!child.isLeaf()) { sortFolderNames.add(ViewMethods.getStringForNode(child).toUpperCase() + x); origNames.add(ViewMethods.getStringForNode(child).toUpperCase() + x); if (child.getChildCount() > 0) { sortTreeNode(child); } } else { sortFileNames.add(ViewMethods.getStringForNode(child).toUpperCase() + x); origNames.add(ViewMethods.getStringForNode(child).toUpperCase() + x); } } Collections.sort(sortFolderNames); Collections.sort(sortFileNames); sortNames.addAll(sortFolderNames); sortNames.addAll(sortFileNames); for (String name : sortNames) { // find the original location to get from children arrayList int indx = origNames.indexOf(name); node.insert(children.get(indx), node.getChildCount()); } } 
+1
source

Currently with java 8 it is easier:

 DefaultMutableTreeNode node; // node can't be null, it an example List<DefaultMutableTreeNode> childrenList = Collections.list(node.children()); Collections.sort(childrenList, (o1,o2) -> ((Node) o1.getUserObject()).getName().compareToIgnoreCase(((Node)o2.getUserObject()).getName())); node.removeAllChildren(); childrenList.forEach(node::add); 
+1
source

It's simple: Put all the sheets in the folder into an array (named hear o_people)

  DefaultMutableTreeNode all_node = new DefaultMutableTreeNode("Root folder"); DefaultMutableTreeNode one_node; Vector sorted_people=new Vector(); // a simple algorithm of sorting array for (int i=0;i&lt;o_people.length-1;i++){ for (int j=i+1;j&lt;o_people.length;j++){ if(o_people[j].toString().compareTo (o_people[i].toString())<0) { String permut=o_people[i].toString(); o_people[i]=o_people[j]; o_people[j]=permut; } } sorted_people.add(o_people[i]); //in my case the leaf is a JChechbox but you can put a String one_node = new DefaultMutableTreeNode ( new JCheckBox(o_people[i].toString(), boolien)); all_node.add(one_node); } tree_model.setRoot(all_node); 

Plain!!! is not it?

0
source

All Articles