my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes JTree won't show expanded node Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTree won Watch "JTree won New topic
Author

JTree won't show expanded node

Jon Wynett
Greenhorn

Joined: Jun 28, 2004
Posts: 10
Hi:

I'm trying to set the selected/expanded path programatically in a JTree and it won't work. I've tried all the following:

fileTree.setExpandsSelectedPaths(true);
fileTree.setSelectionPath(lastPath);
fileTree.expandPath(lastPath);
fileTree.scrollPathToVisible(lastPath);

If I check fileTree.getSelectionPath() and fileTree.isExpanded(lastPath), both indicate that the path is indeed selected and expanded. However when the tree appears, it is not visibly expanded. I've made sure the TreePath does not end with a leaf node. I've tried updateUI and all sorts of repaint calls. Nothing seems to make a difference.

Thanks,

Jon
Serghei Jelauc
Ranch Hand

Joined: Jul 24, 2002
Posts: 128
Hi.
I can guess you does not put your code in the apropriate place. You can manipulate of your tree after you has you tree created and model set.

For example:


In this case setExpandedState does not work, because tree is not ready yet, but if you change like this:

it will work.

Hope it help
[ November 10, 2004: Message edited by: Serghei Jelauc ]

SCJP 1.4 <br />SCBCD 1.3<br />SCWCD 1.4
Jon Wynett
Greenhorn

Joined: Jun 28, 2004
Posts: 10
Hi Serghei:

Thanks. That actually didn't solve the problem but enabled me to identify the real problem. It leads to another question.

What I want to do is save the SelectedPath from an old tree. Then when I have a new tree with the same structure, automatically expand the previously selected path.

The problem I ran into is that if you set the selected path for one tree with a TreePath that came directly from another tree, even if they have the exact same logical structure, the new tree won't expand. It seems from my experiments that you must find the path you want in the current JTree.

However, I have been having trouble figuring out the best way to accomplish this.

Not sure if you or anyone else has an idea.

Jon
Serghei Jelauc
Ranch Hand

Joined: Jul 24, 2002
Posts: 128
Hi.
I do not understand what do you mean. Do you have two differrent trees with the same structure in your application? If so, may be you can try to use the row number for selected node.
I mean:

and in your other tree (if it has the same structure):


I did not try it, but may be it will work;

Or may be you can create the base class wich extends JTree, and for both of your tree create two different classes wich will extend your base class. In this case the selected TrreePath will be the same object for both trees.
And as the previous I did not try this. It's just my ideas.

Serghei
[ November 11, 2004: Message edited by: Serghei Jelauc ]
Jon Wynett
Greenhorn

Joined: Jun 28, 2004
Posts: 10
Thanks for the ideas. The problem with the row number is that it is relative to visible rows. So row 3, for example, in a tree that is expanded is different from row 3 in a tree that is not expanded at all.

I'll figure something out now that I understand the basic problem better.

Thanks again,

Jon
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTree won't show expanded node