- All Known Implementing Classes:
DefaultTreeSelectionModel
,JTree.EmptySelectionModel
The state of the tree selection is characterized by
a set of TreePaths, and optionally a set of integers. The mapping
from TreePath to integer is done by way of an instance of RowMapper.
It is not necessary for a TreeSelectionModel to have a RowMapper to
correctly operate, but without a RowMapper getSelectionRows
will return null.
A TreeSelectionModel can be configured to allow only one
path (SINGLE_TREE_SELECTION
) a number of
contiguous paths (CONTIGUOUS_TREE_SELECTION
) or a number of
discontiguous paths (DISCONTIGUOUS_TREE_SELECTION
).
A RowMapper
is used to determine if TreePaths are
contiguous.
In the absence of a RowMapper CONTIGUOUS_TREE_SELECTION
and
DISCONTIGUOUS_TREE_SELECTION
behave the same, that is they
allow any number of paths to be contained in the TreeSelectionModel.
For a selection model of CONTIGUOUS_TREE_SELECTION
any
time the paths are changed (setSelectionPath
,
addSelectionPath
...) the TreePaths are again checked to
make they are contiguous. A check of the TreePaths can also be forced
by invoking resetRowSelection
. How a set of discontiguous
TreePaths is mapped to a contiguous set is left to implementors of
this interface to enforce a particular policy.
Implementations should combine duplicate TreePaths that are added to the selection. For example, the following code
TreePath[] paths = new TreePath[] { treePath, treePath }; treeSelectionModel.setSelectionPaths(paths);should result in only one path being selected:
treePath
, and
not two copies of treePath
.
The lead TreePath is the last path that was added (or set). The lead row is then the row that corresponds to the TreePath as determined from the RowMapper.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Selection can only be contiguous.static final int
Selection can contain any number of items that are not necessarily contiguous.static final int
Selection can only contain one path at a time. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a PropertyChangeListener to the listener list.void
addSelectionPath
(TreePath path) Adds path to the current selection.void
addSelectionPaths
(TreePath[] paths) Adds paths to the current selection.void
Adds x to the list of listeners that are notified each time the set of selected TreePaths changes.void
Empties the current selection.Returns the last path that was added.int
Returns the lead selection index.int
Returns the largest value obtained from the RowMapper for the current set of selected TreePaths.int
Returns the smallest value obtained from the RowMapper for the current set of selected TreePaths.Returns the RowMapper instance that is able to map a TreePath to a row.int
Returns the number of paths that are selected.int
Returns the current selection mode, one ofSINGLE_TREE_SELECTION
,CONTIGUOUS_TREE_SELECTION
orDISCONTIGUOUS_TREE_SELECTION
.Returns the first path in the selection.TreePath[]
Returns the paths in the selection.int[]
Returns all of the currently selected rows.boolean
isPathSelected
(TreePath path) Returns true if the path,path
, is in the current selection.boolean
isRowSelected
(int row) Returns true if the row identified byrow
is selected.boolean
Returns true if the selection is currently empty.void
Removes a PropertyChangeListener from the listener list.void
removeSelectionPath
(TreePath path) Removes path from the selection.void
removeSelectionPaths
(TreePath[] paths) Removes paths from the selection.void
Removes x from the list of listeners that are notified each time the set of selected TreePaths changes.void
Updates this object's mapping from TreePaths to rows.void
setRowMapper
(RowMapper newMapper) Sets the RowMapper instance.void
setSelectionMode
(int mode) Sets the selection model, which must be one of SINGLE_TREE_SELECTION, CONTIGUOUS_TREE_SELECTION or DISCONTIGUOUS_TREE_SELECTION.void
setSelectionPath
(TreePath path) Sets the selection to path.void
setSelectionPaths
(TreePath[] paths) Sets the selection to path.
-
Field Details
-
SINGLE_TREE_SELECTION
static final int SINGLE_TREE_SELECTIONSelection can only contain one path at a time.- See Also:
-
CONTIGUOUS_TREE_SELECTION
static final int CONTIGUOUS_TREE_SELECTIONSelection can only be contiguous. This will only be enforced if a RowMapper instance is provided. That is, if no RowMapper is set this behaves the same as DISCONTIGUOUS_TREE_SELECTION.- See Also:
-
DISCONTIGUOUS_TREE_SELECTION
static final int DISCONTIGUOUS_TREE_SELECTIONSelection can contain any number of items that are not necessarily contiguous.- See Also:
-
-
Method Details
-
setSelectionMode
void setSelectionMode(int mode) Sets the selection model, which must be one of SINGLE_TREE_SELECTION, CONTIGUOUS_TREE_SELECTION or DISCONTIGUOUS_TREE_SELECTION.This may change the selection if the current selection is not valid for the new mode. For example, if three TreePaths are selected when the mode is changed to
SINGLE_TREE_SELECTION
, only one TreePath will remain selected. It is up to the particular implementation to decide what TreePath remains selected.- Parameters:
mode
- selection mode to be set
-
getSelectionMode
int getSelectionMode()Returns the current selection mode, one ofSINGLE_TREE_SELECTION
,CONTIGUOUS_TREE_SELECTION
orDISCONTIGUOUS_TREE_SELECTION
.- Returns:
- the current selection mode
-
setSelectionPath
Sets the selection to path. If this represents a change, then the TreeSelectionListeners are notified. Ifpath
is null, this has the same effect as invokingclearSelection
.- Parameters:
path
- new path to select
-
setSelectionPaths
Sets the selection to path. If this represents a change, then the TreeSelectionListeners are notified. Ifpaths
is null, this has the same effect as invokingclearSelection
.- Parameters:
paths
- new selection
-
addSelectionPath
Adds path to the current selection. If path is not currently in the selection the TreeSelectionListeners are notified. This has no effect ifpath
is null.- Parameters:
path
- the new path to add to the current selection
-
addSelectionPaths
Adds paths to the current selection. If any of the paths in paths are not currently in the selection the TreeSelectionListeners are notified. This has no effect ifpaths
is null.- Parameters:
paths
- the new paths to add to the current selection
-
removeSelectionPath
Removes path from the selection. If path is in the selection The TreeSelectionListeners are notified. This has no effect ifpath
is null.- Parameters:
path
- the path to remove from the selection
-
removeSelectionPaths
Removes paths from the selection. If any of the paths inpaths
are in the selection, the TreeSelectionListeners are notified. This method has no effect ifpaths
is null.- Parameters:
paths
- the path to remove from the selection
-
getSelectionPath
TreePath getSelectionPath()Returns the first path in the selection. How first is defined is up to implementors, and may not necessarily be the TreePath with the smallest integer value as determined from theRowMapper
.- Returns:
- the first path in the selection
-
getSelectionPaths
TreePath[] getSelectionPaths()Returns the paths in the selection. This will return null (or an empty array) if nothing is currently selected.- Returns:
- the paths in the selection
-
getSelectionCount
int getSelectionCount()Returns the number of paths that are selected.- Returns:
- the number of paths that are selected
-
isPathSelected
Returns true if the path,path
, is in the current selection.- Parameters:
path
- the path to be loked for- Returns:
- whether the
path
is in the current selection
-
isSelectionEmpty
boolean isSelectionEmpty()Returns true if the selection is currently empty.- Returns:
- whether the selection is currently empty
-
clearSelection
void clearSelection()Empties the current selection. If this represents a change in the current selection, the selection listeners are notified. -
setRowMapper
Sets the RowMapper instance. This instance is used to determine the row for a particular TreePath.- Parameters:
newMapper
- RowMapper to be set
-
getRowMapper
RowMapper getRowMapper()Returns the RowMapper instance that is able to map a TreePath to a row.- Returns:
- the RowMapper instance that is able to map a TreePath to a row
-
getSelectionRows
int[] getSelectionRows()Returns all of the currently selected rows. This will return null (or an empty array) if there are no selected TreePaths or a RowMapper has not been set.- Returns:
- all of the currently selected rows
-
getMinSelectionRow
int getMinSelectionRow()Returns the smallest value obtained from the RowMapper for the current set of selected TreePaths. If nothing is selected, or there is no RowMapper, this will return -1.- Returns:
- the smallest value obtained from the RowMapper for the current set of selected TreePaths
-
getMaxSelectionRow
int getMaxSelectionRow()Returns the largest value obtained from the RowMapper for the current set of selected TreePaths. If nothing is selected, or there is no RowMapper, this will return -1.- Returns:
- the largest value obtained from the RowMapper for the current set of selected TreePaths
-
isRowSelected
boolean isRowSelected(int row) Returns true if the row identified byrow
is selected.- Parameters:
row
- row to check- Returns:
- whether the row is selected
-
resetRowSelection
void resetRowSelection()Updates this object's mapping from TreePaths to rows. This should be invoked when the mapping from TreePaths to integers has changed (for example, a node has been expanded).You do not normally have to call this; JTree and its associated listeners will invoke this for you. If you are implementing your own view class, then you will have to invoke this.
-
getLeadSelectionRow
int getLeadSelectionRow()Returns the lead selection index. That is the last index that was added.- Returns:
- the lead selection index
-
getLeadSelectionPath
TreePath getLeadSelectionPath()Returns the last path that was added. This may differ from the leadSelectionPath property maintained by the JTree.- Returns:
- the last path that was added
-
addPropertyChangeListener
Adds a PropertyChangeListener to the listener list. The listener is registered for all properties.A PropertyChangeEvent will get fired when the selection mode changes.
- Parameters:
listener
- the PropertyChangeListener to be added
-
removePropertyChangeListener
Removes a PropertyChangeListener from the listener list. This removes a PropertyChangeListener that was registered for all properties.- Parameters:
listener
- the PropertyChangeListener to be removed
-
addTreeSelectionListener
Adds x to the list of listeners that are notified each time the set of selected TreePaths changes.- Parameters:
x
- the new listener to be added
-
removeTreeSelectionListener
Removes x from the list of listeners that are notified each time the set of selected TreePaths changes.- Parameters:
x
- the listener to remove
-