Listening for Selection Events in a JTree Component
import javax.swing.JTree; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.tree.TreePath; public class Main { public static void main(String[] argv) throws Exception { JTree tree = new JTree(); tree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent evt) { TreePath[] paths = evt.getPaths(); for (int i = 0; i < paths.length; i++) { if (evt.isAddedPath(i)) { System.out.println("This node has been selected"); } else { System.out.println("This node has been deselected"); } } } }); } }