JTree: how to get the text of all items?

大城市里の小女人 提交于 2019-12-03 16:37:35

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
    DefaultMutableTreeNode root = (DefaultMutableTreeNode)jTree.getModel().getRoot();
    Enumeration e = root.preorderEnumeration();
    while(e.hasMoreElements()){
        System.out.println(e.nextElement());
    }

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";
    }
jan

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.

Bimzee

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

    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!