List of utility methods to do JTree Node
List | getAllTreeNodes(TreeModel model, int startLevel, Class clazz, DefaultMutableTreeNode root) Get a flat list of all the treeNodes at or below a certain level, where the userObject is of a certain class, starting from a particular root, or the topmost treeNode if root is null. List nodesResult = new ArrayList(); if (root == null) { root = (DefaultMutableTreeNode) model.getRoot(); treeNodeLevel = 0; return getAllTreeNodes(model, root, nodesResult, startLevel, clazz); |
void | getAllUserObject(TreeNode node, Set userObjectSet) get All User Object for (int i = 0; i < node.getChildCount(); i++) { DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) node.getChildAt(i); userObjectSet.add(childNode.getUserObject()); getAllUserObject(childNode, userObjectSet); |
int | getLevel(TreeNode treeNode) get Level int count = -1; TreeNode current = treeNode; do { current = current.getParent(); count++; } while (current != null); return count; |
DefaultMutableTreeNode[] | getMutableTreeNodes(TreeNode[] path) get Mutable Tree Nodes if (path == null) return null; DefaultMutableTreeNode[] nodes = new DefaultMutableTreeNode[path.length]; for (int i = 0; path != null && i < path.length; i++) { nodes[i] = (DefaultMutableTreeNode) path[i]; return nodes; |
Object | getNodeAt(final JTree tree, final int x, final int y) Finds the node at a given mouse cursor position. final TreePath selPath = tree.getPathForLocation(x, y); return selPath != null ? selPath.getLastPathComponent() : null; |
DefaultMutableTreeNode | getNodeForEvent(JTree targetTree, DropTargetDragEvent dtde) Restiuisce il nodo target Point p = dtde.getLocation();
TreePath path = targetTree.getClosestPathForLocation(p.x, p.y);
return (DefaultMutableTreeNode) path.getLastPathComponent();
|
String | getPathStringForNode(DefaultMutableTreeNode node) get Path String For Node StringBuilder sb = new StringBuilder(); Object[] path = node.getUserObjectPath(); for (int i = 0; i < path.length; i++) { sb.append("/@").append(((String) path[i])); return sb.toString(); |
TreeNode | getSubTree(TreeNode root, String name) find a subtree in the root node and return it, based on exact match. if (("" + root).equals(name)) return root; TreeNode node = null; for (int i = 0; i < root.getChildCount(); i++) { node = getSubTree(root.getChildAt(i), name); if (node != null) break; return node; |
boolean | hasOnlyLeafs(JTree tree, Object node) has Only Leafs TreeModel model = tree.getModel(); for (int i = 0; i < model.getChildCount(node); i++) { if (!model.isLeaf(model.getChild(node, i))) { return false; return true; |
Hashtable | initializeExpandedPathsBeforeChange(JTree tree, DefaultMutableTreeNode root) Call this before a JTree changes. Enumeration paths = tree.getExpandedDescendants(new TreePath(root.getPath())); Hashtable expandedPaths = new Hashtable(); if (paths != null) { while (paths.hasMoreElements()) { TreePath treePath = (TreePath) paths.nextElement(); expandedPaths.put(treePath.toString(), Boolean.TRUE); return expandedPaths; |