Java JTree Node insertNodeInAlphabeticalOrder(DefaultMutableTreeNode childNode, DefaultMutableTreeNode parentNode, DefaultTreeModel model)

Here you can find the source of insertNodeInAlphabeticalOrder(DefaultMutableTreeNode childNode, DefaultMutableTreeNode parentNode, DefaultTreeModel model)

Description

insert Node In Alphabetical Order

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    public static void insertNodeInAlphabeticalOrder(DefaultMutableTreeNode childNode,
            DefaultMutableTreeNode parentNode, DefaultTreeModel model) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) JavaPEG developers/*from  w  w  w.  j  a v a 2  s  .c o m*/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import java.util.*;

public class Main {
    @SuppressWarnings("unchecked")
    public static void insertNodeInAlphabeticalOrder(DefaultMutableTreeNode childNode,
            DefaultMutableTreeNode parentNode, DefaultTreeModel model) {
        Enumeration<DefaultMutableTreeNode> children = parentNode.children();

        String nodeName = childNode.toString();
        int index = 0;

        if (children.hasMoreElements()) {
            while (children.hasMoreElements()) {
                String displayString = children.nextElement().toString();
                if (nodeName.compareToIgnoreCase(displayString) < 1) {
                    break;
                }
                index++;
            }
            model.insertNodeInto(childNode, parentNode, index);
        } else {
            model.insertNodeInto(childNode, parentNode, 0);
        }
    }
}

Related

  1. getNodeForEvent(JTree targetTree, DropTargetDragEvent dtde)
  2. getPathStringForNode(DefaultMutableTreeNode node)
  3. getSubTree(TreeNode root, String name)
  4. hasOnlyLeafs(JTree tree, Object node)
  5. initializeExpandedPathsBeforeChange(JTree tree, DefaultMutableTreeNode root)
  6. insertStrAsNodeLexi(DefaultTreeModel treeModel, String child, DefaultMutableTreeNode parent, Boolean childIsFile)
  7. isAncestor(final TreeNode node, final TreeNode parent)
  8. isChildNode(DefaultMutableTreeNode parent, DefaultMutableTreeNode child)
  9. isMovingUp(DefaultMutableTreeNode ntarget, DefaultMutableTreeNode nsource)