Java Utililty Methods JTree Model

List of utility methods to do JTree Model

Description

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

Method

voidappendTreeNodes(TreeModel model, Object node, StringBuilder builder, String indent)
append Tree Nodes
builder.append(NEWLINE);
builder.append(indent);
if (node instanceof Component) {
    builder.append(node.toString());
    appendComponentStructure((Component) node, builder, "|   " + indent);
} else {
    builder.append(node.toString());
    builder.append("  (");
...
ObjectfindChildNode(TreeModel model, Object node, String text)
Returns the node with the given text below the given node in the specified TreeModel
for (int i = 0; i < model.getChildCount(node); i++) {
    Object currNode = model.getChild(node, i);
    if (currNode.toString() != null && currNode.toString().equals(text))
        return currNode;
return null;
ObjectfindChildNode(TreeModel model, Object node, String text)
find Child Node
for (int i = 0; i < model.getChildCount(node); i++) {
    Object currNode = model.getChild(node, i);
    if (currNode.toString() != null && currNode.toString().equals(text))
        return currNode;
return null;
DefaultMutableTreeNodefindTreeNode(TreeModel model, Object obj)
Given an obj, find the treeNode that holds it.
DefaultMutableTreeNode result = null;
List nodesResult = new ArrayList();
nodesResult = getAllTreeNodes(model);
for (Iterator iter = nodesResult.iterator(); iter.hasNext();) {
    DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) iter.next();
    Object userObj = treeNode.getUserObject();
    if (obj.equals(userObj)) {
        result = treeNode;
...
ListgetAllTreeNodes(TreeModel model)
Get a flat list of all the treeNodes
return getAllTreeNodes(model, 1, null, null);
int[]getTreeIndents(TreeModel in)
return the size of a tree starting at a specific root
int[] ret = new int[getTreeModelSize(in)];
setTreeIndents(in, in.getRoot(), ret, 0, 0);
return (ret);
intgetTreeModelSize(TreeModel in)
return the size of a tree model
Object root = in.getRoot();
if (root == null)
    return (0);
return (getTreeModelSize(in, root));
booleanisTreePathInModel(TreeModel treeModel, TreePath treePath)
is Tree Path In Model
if (treeModel == null)
    return false;
if (treePath == null)
    return false;
TreePath parentPath = treePath.getParentPath();
if (parentPath == null) {
    return treeModel.getRoot() == treePath.getLastPathComponent();
if (!isTreePathInModel(treeModel, parentPath))
    return false;
Object node = treePath.getLastPathComponent();
Object parent = parentPath.getLastPathComponent();
return treeModel.getIndexOfChild(parent, node) >= 0;
voidlogTreeNodes(TreeModel model, Object node, String indent)
log Tree Nodes
logToConsole(indent + node.toString());
for (int i = 0; i < model.getChildCount(node); i++)
    logTreeNodes(model, model.getChild(node, i), "|   " + indent);
voidnodesChanged(DefaultTreeModel rightTreeModel)
nodes Changed
DefaultMutableTreeNode root = (DefaultMutableTreeNode) rightTreeModel.getRoot();
Enumeration enumeration = root.breadthFirstEnumeration();
while (enumeration.hasMoreElements()) {
    DefaultMutableTreeNode defaultMutableTreeNode = (DefaultMutableTreeNode) enumeration.nextElement();
    rightTreeModel.nodeChanged(defaultMutableTreeNode);