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

DefaultMutableTreeNodeaddChildNode(DefaultMutableTreeNode parent, Object child)
add Child Node
HashSet hashSet = new HashSet();
DefaultMutableTreeNode childNode;
for (int i = 0; i < parent.getChildCount(); i++) {
    hashSet.add(parent.getChildAt(i).toString());
if (hashSet.add(child.toString())) {
    childNode = new DefaultMutableTreeNode(child);
    parent.insert(childNode, parent.getChildCount());
...
DefaultMutableTreeNodeaddCloneNode(DefaultMutableTreeNode srcNode, DefaultMutableTreeNode root)
add Clone Node
DefaultMutableTreeNode clone = new DefaultMutableTreeNode(srcNode.getUserObject());
root.add(clone);
for (int i = 0; i < srcNode.getChildCount(); i++) {
    DefaultMutableTreeNode child = (DefaultMutableTreeNode) srcNode.getChildAt(i);
    addCloneNode(child, clone);
return clone;
voidApplyFilter(DefaultMutableTreeNode node, String filter)
Apply Filter
if (node.getChildCount() == 0) {
    if (!node.toString().endsWith(filter)) {
        node.removeFromParent();
    return;
for (int i = 0; i < node.getChildCount(); i++) {
    ApplyFilter((DefaultMutableTreeNode) node.getChildAt(i), filter);
...
DefaultMutableTreeNodecloneTreeNode(DefaultMutableTreeNode srcNode)
clone Tree Node
if (srcNode == null)
    return null;
DefaultMutableTreeNode node = new DefaultMutableTreeNode(srcNode.getUserObject());
for (int i = 0; i < srcNode.getChildCount(); i++) {
    DefaultMutableTreeNode child = (DefaultMutableTreeNode) srcNode.getChildAt(i);
    addCloneNode(child, node);
return node;
...
voidcollapseSubTree(JTree tree, DefaultMutableTreeNode startNode, DefaultTreeModel model)
Collapse the subtree starting at startNode node
if (startNode.isLeaf()) {
    return;
for (Enumeration e = startNode.children(); e.hasMoreElements();) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
    collapseSubTree(tree, node, model);
TreePath tp = new TreePath(model.getPathToRoot(startNode));
...
booleancontains(final JTree tree, final TreeNode node)
Tests whether a tree contains a given node.
return isAncestor(node, (TreeNode) tree.getModel().getRoot());
voidcopyDMTreeNode(DefaultMutableTreeNode out, DefaultMutableTreeNode in)
Makes a deep copy of a gives DMTreeNode.
int curDepth = 0;
DefaultMutableTreeNode cur = null;
DefaultMutableTreeNode new_node = null;
DefaultMutableTreeNode curDepthNode = null;
cur = in;
curDepth = cur.getLevel();
curDepthNode = out;
cur = (DefaultMutableTreeNode) cur.getNextNode();
...
DefaultMutableTreeNodecreateFolderNode(Object userObject)
create Folder Node
DefaultMutableTreeNode newFolder = new DefaultMutableTreeNode(userObject) {
    public boolean isLeaf() {
        return false;
};
return newFolder;
DefaultMutableTreeNodecreateSubTreeIfNecessary(TreeModel model, DefaultMutableTreeNode parent, String childName)
create Sub Tree If Necessary
DefaultMutableTreeNode ret = null;
ret = getSubtree(parent, childName);
if (ret == null) {
    ret = new DefaultMutableTreeNode(childName + "(0)");
    DefaultTreeModel dtm = (DefaultTreeModel) model;
    dtm.insertNodeInto(ret, parent, 0);
return ret;
...
voidexpandAll(JTree tree, DefaultMutableTreeNode root)
Expand all the children nodes of the specified TreeNode.
Enumeration<?> nodes = root.children();
while (nodes.hasMoreElements()) {
    Object node = nodes.nextElement();
    if (node instanceof DefaultMutableTreeNode) {
        TreePath path = new TreePath(((DefaultMutableTreeNode) node).getPath());
        if (!tree.isExpanded(path)) {
            tree.expandPath(path);