Java Utililty Methods JTree Node

List of utility methods to do JTree Node

Description

The list of methods to do JTree Node are organized into topic(s).

Method

booleanrecursiveTreeNode(DefaultMutableTreeNode node)
recursive Tree Node
if (node == null)
    return false;
Object[] objs = node.getUserObjectPath();
if (objs == null)
    return false;
for (int i = 0; i < objs.length; i++) {
    for (int j = i; j < objs.length; j++) {
        if (i == j)
...
voidrefreshNode(final JTree tree, final TreeNode node)
Refresh node.
((DefaultTreeModel) tree.getModel()).nodeStructureChanged(node);
voidRemoveChildren(DefaultMutableTreeNode node)
Remove Children
while (node.getChildCount() > 0) {
    RemoveChildren((DefaultMutableTreeNode) node.getFirstChild());
node.removeFromParent();
TreePathsearchForNode(TreeNode node, String query)
search For Node
if (node.toString().startsWith(query))
    return new TreePath(node);
if (node.isLeaf())
    return null;
Enumeration<TreeNode> e = (Enumeration<TreeNode>) node.children();
while (e.hasMoreElements()) {
    TreePath path = searchForNode(e.nextElement(), query);
    if (path != null) {
...
voidsetTreeSelectedNode(JTree tree, TreeNode node)
set Tree Selected Node
DefaultTreeModel treeModel = (DefaultTreeModel) tree.getModel();
tree.setSelectionPath(new TreePath(treeModel.getPathToRoot(node)));
tree.scrollPathToVisible(new TreePath(treeModel.getPathToRoot(node)));
DefaultMutableTreeNodesort(DefaultMutableTreeNode node)
Method by Adrian: [ StackOverflow ] & Mike: [ StackOverflow ]
List<DefaultMutableTreeNode> children = Collections.list(node.children());
List<String> orgCnames = new ArrayList<String>();
List<String> cNames = new ArrayList<String>();
DefaultMutableTreeNode temParent = new DefaultMutableTreeNode();
for (DefaultMutableTreeNode child : children) {
    DefaultMutableTreeNode ch = (DefaultMutableTreeNode) child;
    temParent.insert(ch, 0);
    String uppser = ch.toString().toUpperCase();
...
voidsortNode(DefaultMutableTreeNode parent, Comparator comparator)
sort Node
int n = parent.getChildCount();
for (int i = 0; i < n - 1; i++) {
    int min = i;
    for (int j = i + 1; j < n; j++) {
        if (comparator.compare((DefaultMutableTreeNode) parent.getChildAt(min),
                (DefaultMutableTreeNode) parent.getChildAt(j)) > 0) {
            min = j;
    if (i != min) {
        MutableTreeNode a = (MutableTreeNode) parent.getChildAt(i);
        MutableTreeNode b = (MutableTreeNode) parent.getChildAt(min);
        parent.insert(b, i);
        parent.insert(a, min);
voidsortTreeDepthFirst(DefaultMutableTreeNode root, Comparator comparator)
sort Tree Depth First
Enumeration e = root.depthFirstEnumeration();
while (e.hasMoreElements()) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
    if (!node.isLeaf()) {
        sortNode(node, comparator);
DefaultMutableTreeNodesortTreeNode(DefaultMutableTreeNode root)
sort Tree Node
if (root == null)
    return root;
int count = root.getChildCount();
if (count <= 0)
    return root;
for (int i = 0; i < count; i++) {
    for (int j = count - 1; j > i; j--) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(j);
...
DefaultMutableTreeNodesortTreeNodesAlphaNumeric(final DefaultMutableTreeNode node)
not tail recursive!
List<String> keys = new ArrayList<String>(node.getChildCount());
HashMap<String, DefaultMutableTreeNode> childMap = new HashMap<String, DefaultMutableTreeNode>(
        node.getChildCount());
for (int i = 0; i < node.getChildCount(); i++) {
    DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(i);
    String key = child.getUserObject().toString();
    keys.add(key);
    childMap.put(key, child);
...