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

voidinsertNodeInAlphabeticalOrder(DefaultMutableTreeNode childNode, DefaultMutableTreeNode parentNode, DefaultTreeModel model)
insert Node In Alphabetical Order
Enumeration<DefaultMutableTreeNode> children = parentNode.children();
String nodeName = childNode.toString();
int index = 0;
if (children.hasMoreElements()) {
    while (children.hasMoreElements()) {
        String displayString = children.nextElement().toString();
        if (nodeName.compareToIgnoreCase(displayString) < 1) {
            break;
...
voidinsertStrAsNodeLexi(DefaultTreeModel treeModel, String child, DefaultMutableTreeNode parent, Boolean childIsFile)
insert Str As Node Lexi
DefaultMutableTreeNode currNode;
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);
if (childIsFile)
    childNode.setAllowsChildren(false);
else
    childNode.setAllowsChildren(true);
Boolean parentIsFile;
boolean inserted = false;
...
booleanisAncestor(final TreeNode node, final TreeNode parent)
Tests whether a given node is an ancestor node of another node.
if (parent == node) {
    return true;
for (int i = 0; i < parent.getChildCount(); i++) {
    if (isAncestor(node, parent.getChildAt(i))) {
        return true;
return false;
booleanisChildNode(DefaultMutableTreeNode parent, DefaultMutableTreeNode child)
is Child Node
if (parent == child)
    return false;
DefaultMutableTreeNode node = child;
while ((node = (DefaultMutableTreeNode) node.getParent()) != null) {
    if (node.getUserObject() == parent.getUserObject())
        return true;
return false;
...
booleanisMovingUp(DefaultMutableTreeNode ntarget, DefaultMutableTreeNode nsource)
Verifica se si sta spostando il nodo source verso l'alto
boolean areSiblings;
DefaultMutableTreeNode targetParent = (DefaultMutableTreeNode) ntarget.getParent();
DefaultMutableTreeNode sourceParent = (DefaultMutableTreeNode) nsource.getParent();
areSiblings = (sourceParent.getUserObject().equals(targetParent.getUserObject()));
if (areSiblings) {
    int targetIndex = targetParent.getIndex(ntarget);
    int sourceIndex = sourceParent.getIndex(nsource);
    if (sourceIndex > targetIndex) {
...
booleanisNodeAsStrChild(DefaultMutableTreeNode parent, String child)
is Node As Str Child
for (int i = 0; i < parent.getChildCount(); i++) {
    if (((DefaultMutableTreeNode) parent.getChildAt(i)).getUserObject().toString().equals(child)) {
        return true;
return false;
booleanisNodeOrChildSelected(JTree tree, DefaultMutableTreeNode node)
is Node Or Child Selected
TreePath[] selectionPaths = tree.getSelectionPaths();
if (selectionPaths == null || selectionPaths.length == 0)
    return false;
TreePath path = new TreePath(node.getPath());
for (TreePath selectionPath : selectionPaths) {
    if (path.isDescendant(selectionPath))
        return true;
return false;
booleanisReflectanceType(TreeNode productTypeNode, String type)
is Reflectance Type
for (int i = 0; i < productTypeNode.getChildCount(); i++) {
    final TreeNode productTypeChildNode = productTypeNode.getChildAt(i);
    final String productTypeChildNodeName = productTypeChildNode.toString();
    if (productTypeChildNodeName.equals("RADIOMETRY")) {
        final TreeNode radiometryChildNode = productTypeChildNode.getChildAt(0);
        return radiometryChildNode.getChildAt(0).toString().equals(type);
return false;
StringlistTree(TreeNode node, String prefix, String lineSeparator)
list Tree
String result = prefix + ((node.toString() != null) ? node.toString() : "") + lineSeparator;
if (!node.isLeaf()) {
    for (int i = 0; i < node.getChildCount(); i++) {
        result += listTree(node.getChildAt(i), prefix + LEVEL_SEPARATOR, lineSeparator);
return result;
voidprintTree(final DefaultMutableTreeNode tree, final String indent)
print Tree
debug("LOG: \n", treetoString(tree, indent));