如何获得所有项目的文本?

时间:2022-03-08 12:47:33

I want to get text of an JTree in format:

我想获得JTree的文本格式:

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.

我考虑扩展树和处理所有节点,但可能是坏主意。

5 个解决方案

#1


7  

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

用递归函数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

getTreeText接受三个参数

  • model: The model which we request for tree nodes
  • 模型:我们要求树节点的模型。
  • object: The object we ask a string representation for (including all children)
  • 对象:我们请求字符串表示的对象(包括所有子对象)
  • indent: indentation level
  • 缩进,缩进级别

#2


3  

    DefaultMutableTreeNode root = (DefaultMutableTreeNode)jTree.getModel().getRoot();
    Enumeration e = root.preorderEnumeration();
    while(e.hasMoreElements()){
        System.out.println(e.nextElement());
    }

#3


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:

DefaultMutableTreeNode具有遍历方法,可以根据具体的需求访问树的所有节点。在您的上下文中,一个preorderEnumeration看起来是合适的,一些代码片段:

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

#4


0  

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

这段代码对我有用。用自定义对象替换UserObject。从Obj对象可以获得必要的值。

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

    }

#5


0  

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.

我扩展了Howards的答案,并使用JTree方法convertValueToText调用真正的单元格渲染器,并将结果保存在一个集合中,而不是一个字符串。

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

用递归函数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

getTreeText接受四个参数

  • 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.

convertValueToText方法接受甚至在基本实现中都没有使用的参数。但是根据树中使用的对象类型,渲染可能会消耗这些值,参数可能需要进行微调。在我的情况下,他们根本不理会。

#1


7  

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

用递归函数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

getTreeText接受三个参数

  • model: The model which we request for tree nodes
  • 模型:我们要求树节点的模型。
  • object: The object we ask a string representation for (including all children)
  • 对象:我们请求字符串表示的对象(包括所有子对象)
  • indent: indentation level
  • 缩进,缩进级别

#2


3  

    DefaultMutableTreeNode root = (DefaultMutableTreeNode)jTree.getModel().getRoot();
    Enumeration e = root.preorderEnumeration();
    while(e.hasMoreElements()){
        System.out.println(e.nextElement());
    }

#3


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:

DefaultMutableTreeNode具有遍历方法,可以根据具体的需求访问树的所有节点。在您的上下文中,一个preorderEnumeration看起来是合适的,一些代码片段:

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

#4


0  

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

这段代码对我有用。用自定义对象替换UserObject。从Obj对象可以获得必要的值。

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

    }

#5


0  

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.

我扩展了Howards的答案,并使用JTree方法convertValueToText调用真正的单元格渲染器,并将结果保存在一个集合中,而不是一个字符串。

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

用递归函数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

getTreeText接受四个参数

  • 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.

convertValueToText方法接受甚至在基本实现中都没有使用的参数。但是根据树中使用的对象类型,渲染可能会消耗这些值,参数可能需要进行微调。在我的情况下,他们根本不理会。

相关文章