Project View - Expand node/path toward a PsiElement without altering selection

Hello there, 

I've been searching for hours in the SDK's source but cannot find any "easy" way to expand the Project View's tree to a given PsiElement without also selecting it.

The only thing working is this:

ProjectView.selectPsiElement(psiElement, false);

but it also selects the associated node in the tree, whereas I'd like the previous selection to remain.

I've tried:

AbstractTreeUI.expand(psiElement, null);
AbstractTreeUI.userScrollTo(psiElement, null);

which don't work consistently depending on the tree's state, and even:

JTree.scrollPathToVisible(new TreePath(treeUI.getNodeForElement(psiElement, false)))

but AbstractTreeUI.getNodeForElement returns null most of the time.

Any help would be greatly appreciated,

Thank you very much in advance.

Permanently deleted user

Have you tried It should scroll to this node, but should not add it to selection.


Thank you Sergey, unfortunately that doesn't seem to do anything, at least in my case.

In the end, the only solution I found is copying the source code from BaseProjectTreeBuilder into my own class (as the interesting stuff is unfortunately private) and make it a bit simpler. For anyone who's interested, it can be found here:

Just copy that in your project and call it like this:

TreeBuilderUtil.expandPathTo(treeBuilder, elementToExpandToward)

It's a bit ugly to have to do this, especially considering it might break in a future platform update, but it's working really well for now.

If anyone has a better suggestion, I'd be very happy to hear it.


Please sign in to leave a comment.