Search code examples
javajtree

How can I tell what nodes are highlighted on a displayed JTree?


I'm working on an app that displays a JTree. I want the user to be able to highlight certain nodes and then do things to those nodes after they press a button.

Highlighting already works - they can click on things, and then shift-click or control click to highlight other things. is there any way to detect which nodes are highlighted in this way?

Thanks!


Solution

  • If I'm understanding you correctly you want to find all the tree nodes that a user has selected. See JTree.getSelectionPaths(). The Javadoc states that it returns

    An array of TreePath objects indicating the selected nodes, or null if nothing is currently selected

    Each TreePath

    represents an array of objects that uniquely identify the path to a node in a tree. The elements of the array are ordered with the root as the first element of the array

    As @MadProgrammer states in the comment the last object in the array is the selected node.