List of utility methods to do JTree Path
List | lastPathComponents(TreePath[] paths) last Path Components ArrayList lastPathComponents = new ArrayList(); for (int i = 0; i < paths.length; i++) { lastPathComponents.add(paths[i].getLastPathComponent()); return lastPathComponents; |
void | loadExpansionState(JTree tree, Enumeration Restore the expansion state of a JTree. if (expansionState != null) { while (expansionState.hasMoreElements()) { TreePath treePath = (TreePath) expansionState.nextElement(); tree.expandPath(treePath); |
String | makeLocalFilePath(TreePath treePath) make Local File Path String filePath = ""; for (Object node : treePath.getPath()) { if (node.toString().equals("/")) continue; filePath += System.getProperty("file.separator") + node.toString(); return filePath; |
TreePath | makeTreePath(TreeNode treeNode) Creates a tree path from the root node of the tree to the specified treeNode instance LinkedList list = new LinkedList(); while (treeNode != null) { list.addFirst(treeNode); treeNode = treeNode.getParent(); Object[] nodes = list.toArray(); return new TreePath(nodes); |
TreePath | pathByAddingChildAsStr(TreePath currPath, String child) path By Adding Child As Str DefaultMutableTreeNode currNode = (DefaultMutableTreeNode) currPath.getLastPathComponent(); for (int i = 0; i < currNode.getChildCount(); i++) { if (((DefaultMutableTreeNode) currNode.getChildAt(i)).getUserObject().toString().equals(child)) { return new TreePath(((DefaultMutableTreeNode) currNode.getChildAt(i)).getPath()); return null; |
boolean | pathContains(TreePath[] paths, Class clazz) path Contains for (int i = 0; i < paths.length; i++) { for (int j = 0; j < paths[i].getPathCount(); j++) { if (clazz.isAssignableFrom(paths[i].getPath()[j].getClass())) { return true; return false; ... |
TreePath | pathToDepth(TreePath path, int depth) path To Depth TreePath returnPath = path; if (path.getPathCount() < depth) return new TreePath(new Object[0]); else { for (int i = path.getPathCount(); i > (depth + 1); i--) { returnPath = returnPath.getParentPath(); return returnPath; |
String | resourceFromTreePath(TreePath path) resource From Tree Path StringBuilder resource = new StringBuilder(""); if (path.getPathCount() > 1) { for (int i = 2; i < path.getPathCount(); i++) { if (i > 2) { resource.append('/'); resource.append(path.getPathComponent(i).toString()); return resource.toString(); |
void | restoreSelectionPath(JTree parentTree) restore Selection Path if (selectedPath != null) {
parentTree.setSelectionPath(selectedPath);
|
TreePath | searchForNodeV2(TreeNode node, String query, JTree tree, TreePath actualPath, boolean isRoot) search For Node V if (node.toString().startsWith(query)) return actualPath.pathByAddingChild(node); if (node.isLeaf()) { return null; Enumeration<TreeNode> e = (Enumeration<TreeNode>) node.children(); if (!e.hasMoreElements()) { TreePath tempPath = actualPath.pathByAddingChild(node); ... |