JTree: how to get the text of all items?
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 selecte开发者_JAVA技巧d component text.
I think about expand tree and handle all nodes, but maybe it bad idea.
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 nodesobject
: 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";
}
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 nodesmodel
: The model which we request for tree nodesobject
: 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.
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
}
精彩评论