Here you can find the source of insertStrAsNodeLexi(DefaultTreeModel treeModel, String child, DefaultMutableTreeNode parent, Boolean childIsFile)
public static void insertStrAsNodeLexi(DefaultTreeModel treeModel, String child, DefaultMutableTreeNode parent, Boolean childIsFile)
//package com.java2s; //License from project: Open Source License import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; public class Main { public static void insertStrAsNodeLexi(DefaultTreeModel treeModel, String child, DefaultMutableTreeNode parent, Boolean childIsFile) { DefaultMutableTreeNode currNode; DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child); if (childIsFile) childNode.setAllowsChildren(false); else/* ww w. jav a 2s .co m*/ childNode.setAllowsChildren(true); Boolean parentIsFile; boolean inserted = false; for (int i = 0; i < parent.getChildCount(); i++) { currNode = (DefaultMutableTreeNode) parent.getChildAt(i); parentIsFile = !currNode.getAllowsChildren(); if ((currNode.getUserObject().toString().compareTo(child) > 0) && (parentIsFile.compareTo(childIsFile) >= 0)) { // dirs before files treeModel.insertNodeInto(childNode, parent, i); inserted = true; break; } } if (!inserted) treeModel.insertNodeInto(childNode, parent, parent.getChildCount()); } }