APIdia Launch Partner
Top Description Fields Methods
javax.swing.tree

public Interface TreeSelectionModel

Known Direct Implementers
javax.swing.tree.DefaultTreeSelectionModel
Imports
javax.swing.event.*, java.beans.PropertyChangeListener

This interface represents the current state of the selection for the tree component. For information and examples of using tree selection models, see How to Use Trees in The Java Tutorial.

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.

Author
Scott Violet

Field Summary

Modifier and TypeField and Description
public static final int
CONTIGUOUS_TREE_SELECTION

Selection can only be contiguous.

public static final int
DISCONTIGUOUS_TREE_SELECTION

Selection can contain any number of items that are not necessarily contiguous.

public static final int
SINGLE_TREE_SELECTION

Selection can only contain one path at a time.

Method Summary

Modifier and TypeMethod and Description
public void
addPropertyChangeListener(PropertyChangeListener
the PropertyChangeListener to be added
listener
)

Adds a PropertyChangeListener to the listener list.

public void
addSelectionPath(TreePath
the new path to add to the current selection
path
)

Adds path to the current selection.

public void
addSelectionPaths(TreePath[]
the new paths to add to the current selection
paths
)

Adds paths to the current selection.

public void
addTreeSelectionListener(TreeSelectionListener
the new listener to be added
x
)

Adds x to the list of listeners that are notified each time the set of selected TreePaths changes.

public void
clearSelection()

Empties the current selection.

public TreePath

Returns:

the last path that was added
getLeadSelectionPath
()

Returns the last path that was added.

public int

Returns:

the lead selection index
getLeadSelectionRow
()

Returns the lead selection index.

public int

Returns:

the largest value obtained from the RowMapper for the current set of selected TreePaths
getMaxSelectionRow
()

Returns the largest value obtained from the RowMapper for the current set of selected TreePaths.

public int

Returns:

the smallest value obtained from the RowMapper for the current set of selected TreePaths
getMinSelectionRow
()

Returns the smallest value obtained from the RowMapper for the current set of selected TreePaths.

public RowMapper

Returns:

the RowMapper instance that is able to map a TreePath to a row
getRowMapper
()

Returns the RowMapper instance that is able to map a TreePath to a row.

public int

Returns:

the number of paths that are selected
getSelectionCount
()

Returns the number of paths that are selected.

public int

Returns:

the current selection mode
getSelectionMode
()

Returns the current selection mode, one of SINGLE_TREE_SELECTION, CONTIGUOUS_TREE_SELECTION or DISCONTIGUOUS_TREE_SELECTION.

public TreePath

Returns:

the first path in the selection
getSelectionPath
()

Returns the first path in the selection.

public TreePath[]

Returns:

the paths in the selection
getSelectionPaths
()

Returns the paths in the selection.

public int[]

Returns:

all of the currently selected rows
getSelectionRows
()

Returns all of the currently selected rows.

public boolean

Returns:

whether the path is in the current selection
isPathSelected
(TreePath
the path to be looked for
path
)

Returns true if the path, path, is in the current selection.

public boolean

Returns:

whether the row is selected
isRowSelected
(int
row to check
row
)

Returns true if the row identified by row is selected.

public boolean

Returns:

whether the selection is currently empty
isSelectionEmpty
()

Returns true if the selection is currently empty.

public void
removePropertyChangeListener(PropertyChangeListener
the PropertyChangeListener to be removed
listener
)

Removes a PropertyChangeListener from the listener list.

public void
removeSelectionPath(TreePath
the path to remove from the selection
path
)

Removes path from the selection.

public void
removeSelectionPaths(TreePath[]
the path to remove from the selection
paths
)

Removes paths from the selection.

public void
removeTreeSelectionListener(TreeSelectionListener
the listener to remove
x
)

Removes x from the list of listeners that are notified each time the set of selected TreePaths changes.

public void
resetRowSelection()

Updates this object's mapping from TreePaths to rows.

public void
setRowMapper(RowMapper
RowMapper to be set
newMapper
)

Sets the RowMapper instance.

public void
setSelectionMode(int
selection mode to be set
mode
)

Sets the selection model, which must be one of SINGLE_TREE_SELECTION, CONTIGUOUS_TREE_SELECTION or DISCONTIGUOUS_TREE_SELECTION.

public void
setSelectionPath(TreePath
new path to select
path
)

Sets the selection to path.

public void
setSelectionPaths(TreePath[]
new selection
paths
)

Sets the selection to path.

Field Detail

CONTIGUOUS_TREE_SELECTIONback to summary
public static final int CONTIGUOUS_TREE_SELECTION

Selection 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.

DISCONTIGUOUS_TREE_SELECTIONback to summary
public static final int DISCONTIGUOUS_TREE_SELECTION

Selection can contain any number of items that are not necessarily contiguous.

SINGLE_TREE_SELECTIONback to summary
public static final int SINGLE_TREE_SELECTION

Selection can only contain one path at a time.

Method Detail

addPropertyChangeListenerback to summary
public void addPropertyChangeListener(PropertyChangeListener listener)

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:PropertyChangeListener

the PropertyChangeListener to be added

addSelectionPathback to summary
public void addSelectionPath(TreePath path)

Adds path to the current selection. If path is not currently in the selection the TreeSelectionListeners are notified. This has no effect if path is null.

Parameters
path:TreePath

the new path to add to the current selection

addSelectionPathsback to summary
public void addSelectionPaths(TreePath[] paths)

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 if paths is null.

Parameters
paths:TreePath[]

the new paths to add to the current selection

addTreeSelectionListenerback to summary
public void addTreeSelectionListener(TreeSelectionListener x)

Adds x to the list of listeners that are notified each time the set of selected TreePaths changes.

Parameters
x:TreeSelectionListener

the new listener to be added

clearSelectionback to summary
public void clearSelection()

Empties the current selection. If this represents a change in the current selection, the selection listeners are notified.

getLeadSelectionPathback to summary
public TreePath getLeadSelectionPath()

Returns the last path that was added. This may differ from the leadSelectionPath property maintained by the JTree.

Returns:TreePath

the last path that was added

getLeadSelectionRowback to summary
public int getLeadSelectionRow()

Returns the lead selection index. That is the last index that was added.

Returns:int

the lead selection index

getMaxSelectionRowback to summary
public 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:int

the largest value obtained from the RowMapper for the current set of selected TreePaths

getMinSelectionRowback to summary
public 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:int

the smallest value obtained from the RowMapper for the current set of selected TreePaths

getRowMapperback to summary
public RowMapper getRowMapper()

Returns the RowMapper instance that is able to map a TreePath to a row.

Returns:RowMapper

the RowMapper instance that is able to map a TreePath to a row

getSelectionCountback to summary
public int getSelectionCount()

Returns the number of paths that are selected.

Returns:int

the number of paths that are selected

getSelectionModeback to summary
public int getSelectionMode()

Returns the current selection mode, one of SINGLE_TREE_SELECTION, CONTIGUOUS_TREE_SELECTION or DISCONTIGUOUS_TREE_SELECTION.

Returns:int

the current selection mode

getSelectionPathback to summary
public 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 the RowMapper.

Returns:TreePath

the first path in the selection

getSelectionPathsback to summary
public TreePath[] getSelectionPaths()

Returns the paths in the selection. This will return null (or an empty array) if nothing is currently selected.

Returns:TreePath[]

the paths in the selection

getSelectionRowsback to summary
public 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:int[]

all of the currently selected rows

isPathSelectedback to summary
public boolean isPathSelected(TreePath path)

Returns true if the path, path, is in the current selection.

Parameters
path:TreePath

the path to be looked for

Returns:boolean

whether the path is in the current selection

isRowSelectedback to summary
public boolean isRowSelected(int row)

Returns true if the row identified by row is selected.

Parameters
row:int

row to check

Returns:boolean

whether the row is selected

isSelectionEmptyback to summary
public boolean isSelectionEmpty()

Returns true if the selection is currently empty.

Returns:boolean

whether the selection is currently empty

removePropertyChangeListenerback to summary
public void removePropertyChangeListener(PropertyChangeListener listener)

Removes a PropertyChangeListener from the listener list. This removes a PropertyChangeListener that was registered for all properties.

Parameters
listener:PropertyChangeListener

the PropertyChangeListener to be removed

removeSelectionPathback to summary
public void removeSelectionPath(TreePath path)

Removes path from the selection. If path is in the selection The TreeSelectionListeners are notified. This has no effect if path is null.

Parameters
path:TreePath

the path to remove from the selection

removeSelectionPathsback to summary
public void removeSelectionPaths(TreePath[] paths)

Removes paths from the selection. If any of the paths in paths are in the selection, the TreeSelectionListeners are notified. This method has no effect if paths is null.

Parameters
paths:TreePath[]

the path to remove from the selection

removeTreeSelectionListenerback to summary
public void removeTreeSelectionListener(TreeSelectionListener x)

Removes x from the list of listeners that are notified each time the set of selected TreePaths changes.

Parameters
x:TreeSelectionListener

the listener to remove

resetRowSelectionback to summary
public 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.

setRowMapperback to summary
public void setRowMapper(RowMapper newMapper)

Sets the RowMapper instance. This instance is used to determine the row for a particular TreePath.

Parameters
newMapper:RowMapper

RowMapper to be set

setSelectionModeback to summary
public 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:int

selection mode to be set

setSelectionPathback to summary
public void setSelectionPath(TreePath path)

Sets the selection to path. If this represents a change, then the TreeSelectionListeners are notified. If path is null, this has the same effect as invoking clearSelection.

Parameters
path:TreePath

new path to select

setSelectionPathsback to summary
public void setSelectionPaths(TreePath[] paths)

Sets the selection to path. If this represents a change, then the TreeSelectionListeners are notified. If paths is null, this has the same effect as invoking clearSelection.

Parameters
paths:TreePath[]

new selection