Allow only a single node to be selected (default) : JTree Selection « Swing « Java Tutorial






Allow only a single node to be selected (default)
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.TreeSelectionModel;


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

    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    JFrame frame = new JFrame("Tree single selection");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JScrollPane(tree));
    frame.setSize(380, 320);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

  }
}








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