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

voidredrawAllTreeCells(DefaultTreeModel treeModel)
Repaints the cells for all the tree nodes in the tree represented by the given tree model.
Object root = treeModel.getRoot();
if (root instanceof TreeNode) {
    redrawAllTreeCellsRec(treeModel, (TreeNode) root);
} else {
    System.err.println("Error: Tree root is not a TreeNode");
voidredrawAllTreeCellsRec(DefaultTreeModel treeModel, TreeNode parent)
redraw All Tree Cells Rec
treeModel.nodeChanged(parent);
for (int i = 0; i < parent.getChildCount(); i++) {
    redrawAllTreeCellsRec(treeModel, parent.getChildAt(i));
TreePathsearchPath(TreeModel model, TreePath oldPath)
Search for a path in the specified tree model, whose nodes have the same name (compared using equals()) as the ones specified in the old path.
Object treenode = model.getRoot();
Object[] oldPathNodes = oldPath.getPath();
TreePath newPath = new TreePath(treenode);
for (int i = 0; i < oldPathNodes.length; ++i) {
    Object oldPathNode = oldPathNodes[i];
    if (treenode.toString().equals(oldPathNode.toString())) {
        if (i == (oldPathNodes.length - 1)) {
            return newPath;
...
intsetTreeObjects(TreeModel in, Object root, Object[] items, int index)
return the objects in a tree modes as a simple array
items[index++] = root;
for (int i = 0; i < in.getChildCount(root); i++)
    index = setTreeObjects(in, in.getChild(root, i), items, index);
return (index);
intsetTreePaths(TreeModel in, Object root, TreePath[] items, Object[] path, int index)
Fill in an array of TreePaths for the entire tree
Object[] newPath = new Object[path.length + 1];
System.arraycopy(path, 0, newPath, 0, path.length);
newPath[path.length] = root;
items[index++] = new TreePath(newPath);
for (int i = 0; i < in.getChildCount(root); i++) {
    index = setTreePaths(in, in.getChild(root, i), items, newPath, index);
return (index);
...
voidshowTreeComponent(String blank, TreeModel model, Object node)
show Tree Component
System.out.println(blank + " - \"" + node + "\"");
for (int i = 0; i < model.getChildCount(node); i++) {
    showTreeComponent(blank + " ", model, model.getChild(node, i));