JTree: how to get the text of all items?

I want to get JTree text in the format:

root sudir1 node1 node2 subdir2 node3 node4 

Is it possible?

I wrote the code

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

But it only gets the selected component text.

I am thinking about expanding the tree and processing all nodes, but maybe this is a bad idea.

+4
source share
5 answers

I think that you should not build a line within the framework of one function, but I do not know what exactly you are aimed at your question.

To keep my answer as close as possible to the function you proposed, you can try something like this:

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

with recursive getTreeText function

 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 we are requesting for tree nodes
  • object : the object we are requesting a string representation for (including all children)
  • indent : indentation level
+7
source
  DefaultMutableTreeNode root = (DefaultMutableTreeNode)jTree.getModel().getRoot(); Enumeration e = root.preorderEnumeration(); while(e.hasMoreElements()){ System.out.println(e.nextElement()); } 
+3
source

DefaultMutableTreeNode has workarounds that help visit all nodes of the tree, depending on specific requirements. In your context, 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"; } 
+2
source

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 } 
0
source

I expanded Howards Answer and used the JTree convertValueToText method to invoke the actual rendering of the cells and store the results in a collection, not a single row.

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

with recursive getTreeText function

 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 we are requesting for tree nodes
  • object : the object we are requesting a string representation for (including all children)
  • result : collection to save each node String value

The convertValueToText method accepts parameters that are not even used in the base implementation. But depending on the types of objects used in the tree, rendering may use these values, and the parameters may need to be fine-tuned. In my case, they are ignored altogether.

0
source

Source: https://habr.com/ru/post/1338678/


All Articles