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.*;
|
Output of program:
Single selection node:
![]() |
Selection to one vertical contiguous set of visible nodes:
![]() |
Multiple selection of visible nodes:
![]() |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: JTree Multiple Selection View All Comments
Post your Comment