Java examples for Swing:JTree
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) { JTree tree = null;//from w ww .j a va 2 s . co m tree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent evt) { // Get all nodes whose selection status has changed TreePath[] paths = evt.getPaths(); // Iterate through all affected nodes for (int i=0; i<paths.length; i++) { if (evt.isAddedPath(i)) { // This node has been selected } else { // This node has been deselected } } } }); } }