Java Utililty Methods JTree Path

List of utility methods to do JTree Path

Description

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

Method

TreePathgetTreePath(TreePath path, JTree tree, boolean compareOnlyLabels)
get Tree Path
Object[] pathElements = path.getPath();
TreePath result = null;
TreeNode root = (TreeNode) tree.getModel().getRoot();
if ((root.toString() == null && pathElements[0].toString() == null)
        || (root.toString().compareTo(pathElements[0].toString()) == 0))
    result = new TreePath(root);
if (result == null || pathElements.length == 1)
    return result;
...
StringgetTreePathString(TreePath path)
get Tree Path String
return path.toString();
TreePath[]getUpdatedTreePath(TreePath[] paths, JTree tree)
get Updated Tree Path
if (paths == null || tree == null) {
    return null;
TreeNode root = (TreeNode) tree.getModel().getRoot();
ArrayList<TreePath> result = new ArrayList<TreePath>();
for (int i = 0; i < paths.length; i++) {
    TreePath updatedPath = findTreePath(new TreePath(root), root, getTreePathString(paths[i]));
    if (updatedPath != null) {
...
ObjectgetUserObject(TreePath path)
get User Object
Object value = path.getLastPathComponent();
if (value instanceof DefaultMutableTreeNode) {
    return ((DefaultMutableTreeNode) value).getUserObject();
return value;
ObjectgetUserObject(TreePath path)
get User Object
Object value = path.getLastPathComponent();
if (value instanceof DefaultMutableTreeNode) {
    return ((DefaultMutableTreeNode) value).getUserObject();
return value;
booleanisDescendant(TreePath path1, final TreePath path2)
is Descendant
int count1 = path1.getPathCount();
final int count2 = path2.getPathCount();
if (count1 <= count2) {
    return false;
while (count1 != count2) {
    path1 = path1.getParentPath();
    count1--;
...
booleanisDescendant(TreePath path1, final TreePath path2)
Is path1 descendant of path2?
int count1 = path1.getPathCount();
final int count2 = path2.getPathCount();
if (count1 <= count2) {
    return false;
while (count1 != count2) {
    path1 = path1.getParentPath();
    count1--;
...
booleanisFile(TreePath path)
is File
DefaultMutableTreeNode clickedNode = (DefaultMutableTreeNode) path.getLastPathComponent();
return (!clickedNode.getAllowsChildren());
booleanisSource(TreePath path)
is Source
int pathCount = path.getPathCount();
return (pathCount == 2);
booleanisTreePathContainedInExpansionState(TreePath treePath, ArrayList expansionStateStrings)
is Tree Path Contained In Expansion State
if (treePath != null && expansionStateStrings != null) {
    return expansionStateStrings.contains(treePath.toString());
return false;