Getting the Selected Nodes in a JTree Component : JTree Selection « Swing « Java Tutorial






import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.TreePath;

public class Main {
  public static void main(String[] argv) throws Exception {
    JTree tree = new JTree();

    JFrame f = new JFrame();
    f.add(new JScrollPane(tree));
    f.setSize(300, 300);
    f.setVisible(true);

    // Get paths of all selected nodes
    TreePath[] paths = tree.getSelectionPaths();

  }
}








14.71.JTree Selection
14.71.1.Enabling and Disabling Multiple Selections in a JTree Component
14.71.2.Allow only a single node to be selected (default)Allow only a single node to be selected (default)
14.71.3.Allow selection to span one vertical contiguous set of visible nodes
14.71.4.Allow multiple selections of visible nodes
14.71.5.Listening for Selection Events in a JTree Component
14.71.6.Getting the Selected Nodes in a JTree Component
14.71.7.Returns a TreePath containing the specified node.
14.71.8.Determine if the third item is selected