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:
This is the method that returns the current selection model.
This is the method that returns the value of selectionMode property either single-item or multiple-item.
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.
It allows the permission for selecting only one path at a time.
It allows the permission for selection to contiguous.
It allows the selection that contains any number of items but can not be necessary contiguous.
Here is the code of program:
Output of program:
Single selection node:
Selection to one vertical contiguous set of visible nodes:
Multiple selection of visible nodes:
Recommend the tutorial