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 nodesmodel : the model we are requesting for tree nodesobject : 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.
source share