I've read plenty of java Swing forums or questions and none of them have the right answers. I am trying to dynamically select a node in a JTree by searching for it using a search panel based on its property.

My jtree allows single selection model. (SINGLE_TREE_SELECTION).

jTree.setExpandsSelectedPaths(true);
// jTree.expandPath(treePath);
jTree.setSelectionPath(treePath);
jTree.scrollPathToVisible(treePath);

I know the node is being selected and a call to jTree.getSelectionPath() shows the expected treePath. The problem is that graphically the treePath is neither expanded nor the specific searched node is highlighted.

TreePath is correct, and the node is being selected. The branch is just not being expanded and the node is not being highlighted. The node is not highlighted even when the branch is already in an expanded state.

Can anyone please help.

I am using Java 1.5

Thanks,
Bob

Recommended Answers

All 2 Replies

Well, that selection call should work. Are you certain the TreePath includes all nodes up to and including the one you want to select?

Well, that selection call should work. Are you certain the TreePath includes all nodes up to and including the one you want to select?

yes the treepath is prefectly fine and includes all the nodes upto the selection

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.