JTree: how to get the text of all items?
Asked Answered
B

5

5

I want to get text of an JTree in format:

root
  sudir1
    node1
    node2
  subdir2
    node3
    node4

Is it possible?

I wrote some code

public static String getLastSelectedText(JTree tree) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
    if (node == null) return null;
    return node.getUserObject().toString();
}

But it get only selected component text.

I think about expand tree and handle all nodes, but maybe it bad idea.

Behalf answered 7/2, 2011 at 5:39 Comment(0)
T
8

I think you shouldn't build the string within a single function - but I do not know what exactly you aim at with your question.

In order to keep my answer as close to your suggested function as possible you may try something like this:

TreeModel model = tree.getModel();
System.out.println(getTreeText(model, model.getRoot(), ""));

with recursive function getTreeText

private static String getTreeText(TreeModel model, Object object, String indent) {
    String myRow = indent + object + "\n";
    for (int i = 0; i < model.getChildCount(object); i++) {
        myRow += getTreeText(model, model.getChild(object, i), indent + "  ");
    }
    return myRow;
}

getTreeText takes three arguments

  • model: The model which we request for tree nodes
  • object: The object we ask a string representation for (including all children)
  • indent: indentation level
Thruster answered 7/2, 2011 at 6:16 Comment(1)
This approach gets the node's userobject. If the JTree is using a non-default TreeCellRenderer, how would you get the node's rendered text?Diorio
C
4
    DefaultMutableTreeNode root = (DefaultMutableTreeNode)jTree.getModel().getRoot();
    Enumeration e = root.preorderEnumeration();
    while(e.hasMoreElements()){
        System.out.println(e.nextElement());
    }
Caputo answered 26/7, 2013 at 16:30 Comment(2)
Welcome. Please consider providing a bit of an explanation of your answerGain
ya its working but u should explain that na. and ya can u tell me what what is the logic behind this and how its working.?Madrigal
A
2

DefaultMutableTreeNode has traversal methods which help in visiting all nodes of a a tree, depending on concrete requirements. In your context, a preorderEnumeration looks appropriate, some snippet:

    String result = "\n";
    Enumeration<?> enumer = root.preorderEnumeration();
    while (enumer.hasMoreElements()) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) enumer.nextElement();
        String nodeValue = String.valueOf(node.getUserObject());
        String indent = "";
        while (node.getParent() != null) {
           indent += "    "; 
           node = (DefaultMutableTreeNode) node.getParent();
        }
        result += indent + nodeValue + "\n";
    }
Anette answered 12/9, 2011 at 12:43 Comment(0)
T
1

I extended Howards Answer and used the JTree method convertValueToText to call the real cell renderer and save the results in a collection rather as one string.

TreeModel model = tree.getModel();
Collection<String> results = new LinkedList<>();
getTreeText(tree, model, model.getRoot(), result);
System.out.println(Arrays.toString(results.toArray()));

with recursive function getTreeText

private static void getTreeText(JTree tree, TreeModel model, Object object, Collection<String> result) {
    result.add(tree.convertValueToText(object, true, true, true, 0, true));
    for (int i = 0; i < model.getChildCount(object); i++) {
        getTreeText(tree, model, model.getChild(object, i), result);
    }
}

getTreeText takes four arguments

  • tree: Tree with tree nodes
  • model: The model which we request for tree nodes
  • object: The object we ask a string representation for (including all children)
  • result: Collection to save each node String value

The method convertValueToText takes parameters not even used in base implementation. But depending on the object types used in the tree the renders might consume these values and the parameters could need fine tuning. In my case they where ignored at all.

Tolly answered 17/11, 2016 at 7:12 Comment(0)
A
0

This code worked for me. Replace UserObject with your custom object. From the Obj object you can get the necessary values.

TreePath[] nodes = tre.getSelectionPaths ();
    for (int i = 0; i < nodes.length; i ++)
    {
        TreePath treePath = nodes[i];

        DefaultMutableTreeNode node =(DefaultMutableTreeNode)treePath.getLastPathComponent ();
        <UserObject> Obj = (<UserObject>) node.getUserObject ();

       String text=Obj.textvalue

    }
Aminaamine answered 3/8, 2012 at 11:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.