Expand a TreePath in a JTree
hi
i face a really strange problem at the moment.
from my main tree, i want to copy the selected TreePath to my tree in a dialog.
so i pass the TreePath to the dialog and call the following method:
tree.expandPath(path);
in the tree-initialization i setsetExpandsSelectedPaths to true, so the path should be expanded.
nothing happens.
either no effect if i call directlyexpandPath().
does anybody have an idea why this happen like this?
thx & greetz
swissManu
Hi,
if I cought your problem, this method should help you. I use it to expand the tree.
private void expand(JTree tree, DefaultMutableTreeNode node) {
if (!node.getAllowsChildren()) {
return;
}
tree.expandPath(new TreePath(node.getPath()));
for (int step = 0; step < node.getChildCount(); step++) {
expand(tree, (DefaultMutableTreeNode)node.getChildAt(step));
}
}
you can call the method like this:
expand(tree, (DefaultMutableTreeNode)tree.getModel().getRoot());
, tree is an instance of JTree class :-)
LP
lukika at 2007-7-15 19:48:38 >

hi lukik
thx for your reply...
hum, i mentioned one thing when i read your snipet...
my model doesn't return an instance of DefaultMutableTreeNode (neither a subclass of it) ... it returns my own bean which i render with my own TreeCellRenderer.
could this be the problem perhaps?
thx again :)
i tried to set the selection with the object instances directly from the model, and it worked.
treKeywords.expandPath(new TreePath(new Object[]{model.getRoot(), model.getChild(model.getRoot(),0)}));
(lol, yeah, i know, its very ugly code, but for testing its enough ;) )
now i see the problem... i can set a selection/expansion path on the current tree, but i'm not able to transfer exactly this path to another tree (or better: another/a new TreeModel, even when it's an instance of the same model) one
strange... :(
any ideas?
ouu!
i got the problem!
in the constructor of my TreeModel, i create a "virtual" root-node (like "all") ... so there is a new root on every creation of a model... therefore, the old path is not matching to the new one because of the new root...
thx anyway lukik :) you'll get the dukes for your time ;)