JTree Multiple Selection

In this section, you will learn how to enable and disable the multiple selections in a JTree component.

JTree Multiple Selection

In this section, you will learn how to enable and disable the multiple selections in a JTree component.

 JTree Multiple Selection

Enable and Disable Multiple Selections in a JTree Component

     

In this section, you will learn how to enable and disable the multiple selections in a JTree component. The multiple selections in tree component that means user will allow or disallow the permission for selection the more than one tree component at a time. By default, the tree allows the multiple selections.

Description of program:

The following program helps you in enabling and disabling the multiple selections in a JTree component. First of all, this program constructs a simple tree that contains the root and child of root node. After creating a tree, you will allow the selection component of tree. If you want to select only one node at a time, you will use the SINGLE_TREE_SELECTION, if you will provide the selection to one vertical contiguous set of visible nodes, you will use the CONTIGUOUS_TREE_SELECTION and if you will allow the permission for multiple selection of visible nodes, you will use the DISCONTIGUOUS_TREE_SELECTION. This tree are displayed on the java swing frame.

Description of code:

getSelectionModel():
This is the method that returns the current selection model. 

setSelectionMode():
 This is the method that returns the value of selectionMode property either single-item or multiple-item.

TreeSelectionModel:
This is an interface that represents the current state of selection to the tree component. The TreeSledtionModel allows the selection only one path at a time by using the SINGLE_TREE_SELECTION, a number of contiguous paths to use the CONTIGUOUS_TREE_SELECTION  or a number of discontiguous paths to use the DISCONTIGUOUS_TREE_SELECTION.

SINGLE_TREE_SELECTION:
It allows the permission for selecting only one path at a time.

CONTIGUOUS_TREE_SELECTION:
It allows the permission for selection to contiguous.

DISCONTIGUOUS_TREE_SELECTION:
It allows the selection that contains any number of items but can not be necessary contiguous.

Here is the code of program:

import javax.swing.*;
import javax.swing.tree.*;

public class SelectionTree{
  public static void main(String[] args) {
  JFrame frame = new JFrame("Enabling and Disabling Multiple Selections in a 
   JTree Component!"
);
  DefaultMutableTreeNode myComputer = new DefaultMutableTreeNode

(
"My Computer");
  DefaultMutableTreeNode c = new DefaultMutableTreeNode("Local Disk(C:)");
  DefaultMutableTreeNode vinod = new DefaultMutableTreeNode("Vinod");
  DefaultMutableTreeNode swing = new DefaultMutableTreeNode("Swing");
  DefaultMutableTreeNode tr = new DefaultMutableTreeNode("Tree");
  DefaultMutableTreeNode a = new DefaultMutableTreeNode("3½ Floppy(A:)");
  DefaultMutableTreeNode e = new DefaultMutableTreeNode("New Volume(E:)");
  c.add(vinod);
  vinod.add(swing);
  swing.add(tr);
  myComputer.add(c);
  myComputer.add(a);
  myComputer.add(e);
  JTree tree = new JTree(myComputer);
//  Single selection node at a time
//  tree.getSelectionModel().setSelectionMode(TreeSelectionModel.
  SINGLE_TREE_SELECTION);

//  provide selection to one vertical contiguous set of visible nodes
  tree.getSelectionModel().setSelectionMode(TreeSelectionModel.
 CONTIGUOUS_TREE_SELECTION
);
//  multiple selection of visible nodes
//  tree.getSelectionModel().setSelectionMode(TreeSelectionModel.
  DISCONTIGUOUS_TREE_SELECTION);

  frame.add(tree);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setUndecorated(true);
  frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
  frame.setSize(200,200);
  frame.setVisible(true);
  }
}

Download this example.

Output of program:

Single selection node:

Selection to one vertical contiguous set of visible nodes:

Multiple selection of visible nodes: