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

[546 byte] By [swissManua] at [2007-10-2 2:07:12]
# 1

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 > top of Java-index,Desktop,Core GUI APIs...
# 2

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 :)

swissManua at 2007-7-15 19:48:38 > top of Java-index,Desktop,Core GUI APIs...
# 3
Can be because that code work quite well in my application.Try to put larger piece of codeLP
lukika at 2007-7-15 19:48:38 > top of Java-index,Desktop,Core GUI APIs...
# 4

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?

swissManua at 2007-7-15 19:48:38 > top of Java-index,Desktop,Core GUI APIs...
# 5

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 ;)

swissManua at 2007-7-15 19:48:38 > top of Java-index,Desktop,Core GUI APIs...
# 6
Thx for dukes :-).I spend with JTree component some time so I know it's a tougher part of the Swing.LP
lukika at 2007-7-15 19:48:38 > top of Java-index,Desktop,Core GUI APIs...