List of utility methods to do JTree Node
boolean | recursiveTreeNode(DefaultMutableTreeNode node) recursive Tree Node if (node == null) return false; Object[] objs = node.getUserObjectPath(); if (objs == null) return false; for (int i = 0; i < objs.length; i++) { for (int j = i; j < objs.length; j++) { if (i == j) ... |
void | refreshNode(final JTree tree, final TreeNode node) Refresh node. ((DefaultTreeModel) tree.getModel()).nodeStructureChanged(node); |
void | RemoveChildren(DefaultMutableTreeNode node) Remove Children while (node.getChildCount() > 0) {
RemoveChildren((DefaultMutableTreeNode) node.getFirstChild());
node.removeFromParent();
|
TreePath | searchForNode(TreeNode node, String query) search For Node if (node.toString().startsWith(query)) return new TreePath(node); if (node.isLeaf()) return null; Enumeration<TreeNode> e = (Enumeration<TreeNode>) node.children(); while (e.hasMoreElements()) { TreePath path = searchForNode(e.nextElement(), query); if (path != null) { ... |
void | setTreeSelectedNode(JTree tree, TreeNode node) set Tree Selected Node DefaultTreeModel treeModel = (DefaultTreeModel) tree.getModel(); tree.setSelectionPath(new TreePath(treeModel.getPathToRoot(node))); tree.scrollPathToVisible(new TreePath(treeModel.getPathToRoot(node))); |
DefaultMutableTreeNode | sort(DefaultMutableTreeNode node) Method by Adrian: [ StackOverflow ] & Mike: [ StackOverflow ] List<DefaultMutableTreeNode> children = Collections.list(node.children()); List<String> orgCnames = new ArrayList<String>(); List<String> cNames = new ArrayList<String>(); DefaultMutableTreeNode temParent = new DefaultMutableTreeNode(); for (DefaultMutableTreeNode child : children) { DefaultMutableTreeNode ch = (DefaultMutableTreeNode) child; temParent.insert(ch, 0); String uppser = ch.toString().toUpperCase(); ... |
void | sortNode(DefaultMutableTreeNode parent, Comparator sort Node int n = parent.getChildCount(); for (int i = 0; i < n - 1; i++) { int min = i; for (int j = i + 1; j < n; j++) { if (comparator.compare((DefaultMutableTreeNode) parent.getChildAt(min), (DefaultMutableTreeNode) parent.getChildAt(j)) > 0) { min = j; if (i != min) { MutableTreeNode a = (MutableTreeNode) parent.getChildAt(i); MutableTreeNode b = (MutableTreeNode) parent.getChildAt(min); parent.insert(b, i); parent.insert(a, min); |
void | sortTreeDepthFirst(DefaultMutableTreeNode root, Comparator sort Tree Depth First Enumeration e = root.depthFirstEnumeration(); while (e.hasMoreElements()) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement(); if (!node.isLeaf()) { sortNode(node, comparator); |
DefaultMutableTreeNode | sortTreeNode(DefaultMutableTreeNode root) sort Tree Node if (root == null) return root; int count = root.getChildCount(); if (count <= 0) return root; for (int i = 0; i < count; i++) { for (int j = count - 1; j > i; j--) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(j); ... |
DefaultMutableTreeNode | sortTreeNodesAlphaNumeric(final DefaultMutableTreeNode node) not tail recursive! List<String> keys = new ArrayList<String>(node.getChildCount()); HashMap<String, DefaultMutableTreeNode> childMap = new HashMap<String, DefaultMutableTreeNode>( node.getChildCount()); for (int i = 0; i < node.getChildCount(); i++) { DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(i); String key = child.getUserObject().toString(); keys.add(key); childMap.put(key, child); ... |