JavaTM 2 Platform
Standard Ed. 6

javax.swing.tree
类 DefaultTreeSelectionModel

java.lang.Object
  继承者 javax.swing.tree.DefaultTreeSelectionModel
所有已实现的接口:
Serializable, Cloneable, TreeSelectionModel
直接已知子类:
JTree.EmptySelectionModel

public class DefaultTreeSelectionModel
extends Object
implements Cloneable, Serializable, TreeSelectionModel

TreeSelectionModel 的默认实现。每当 selection 中的路径(不是行)发生更改时,侦听器就会获得通知。为了能够跟踪行的更改,您可能希望获得一个侦听器,用于树上的扩展事件,并从该位置测试更改。

从所有更新选择路径的方法中调用 resetRowSelection。如果对这些方法中的任何方法创建子类来过滤允许选择的内容,并且没有通知超类,请确定并通知 resetRowSelection

警告:此类的序列化对象将与以后的 Swing 版本不兼容。当前的序列化支持适用于短期存储或运行相同 Swing 版本的应用程序之间的 RMI。从 1.4 版本开始,已在 java.beans 包中添加了支持所有 JavaBeansTM 长期存储的功能。请参见 XMLEncoder

另请参见:
JTree

字段摘要
protected  SwingPropertyChangeSupport changeSupport
          用于接收通知的注册侦听器。
protected  int leadIndex
          selection 中前导路径的索引。
protected  TreePath leadPath
          添加的最后一条路径。
protected  int leadRow
          前导行。
protected  EventListenerList listenerList
          事件侦听器列表。
protected  DefaultListSelectionModel listSelectionModel
          维护列表选择模型的句柄。
protected  RowMapper rowMapper
          提供给定路径的行。
protected  TreePath[] selection
          当前选择的路径。
static String SELECTION_MODE_PROPERTY
          selectionMode 的属性名称。
protected  int selectionMode
          用于 selection 的模式,将是 SINGLE_TREE_SELECTION、CONTIGUOUS_TREE_SELECTION 或 DISCONTIGUOUS_TREE_SELECTION。
 
从接口 javax.swing.tree.TreeSelectionModel 继承的字段
CONTIGUOUS_TREE_SELECTION, DISCONTIGUOUS_TREE_SELECTION, SINGLE_TREE_SELECTION
 
构造方法摘要
DefaultTreeSelectionModel()
          使用 DISCONTIGUOUS_TREE_SELECTION 的选择模式创建空的 DefaultTreeSelectionModel 新实例。
 
方法摘要
 void addPropertyChangeListener(PropertyChangeListener listener)
          将 PropertyChangeListener 添加到侦听器列表。
 void addSelectionPath(TreePath path)
          将路径添加到当前的 selection 中。
 void addSelectionPaths(TreePath[] paths)
          向当前的 selection 中添加路径。
 void addTreeSelectionListener(TreeSelectionListener x)
          向每次已选的 TreePaths 集合发生更改时都会得到通知的侦听器列表添加 x。
protected  boolean arePathsContiguous(TreePath[] paths)
          如果路径是连续的,或者此对象没有 RowMapper,则返回 true。
protected  boolean canPathsBeAdded(TreePath[] paths)
          用于测试是否可以添加特定的 TreePath 集合。
protected  boolean canPathsBeRemoved(TreePath[] paths)
          如果可以移除该路径而不中断该模型的连续性,则返回 true。
 void clearSelection()
          清空当前的 selection。
 Object clone()
          返回具有相同 selection 的此对象的副本。
protected  void fireValueChanged(TreeSelectionEvent e)
          通知已注册此对象上的树选择事件的所有侦听器。
 TreePath getLeadSelectionPath()
          返回添加的最后一条路径。
 int getLeadSelectionRow()
          返回 lead selection 索引。
<T extends EventListener>
T[]
getListeners(Class<T> listenerType)
          返回当前已在此模型上注册为 FooListener 的所有对象组成的数组。
 int getMaxSelectionRow()
          返回从 RowMapper 获取的已选择的 TreePath 的当前集合中的最大值。
 int getMinSelectionRow()
          返回从 RowMapper 获取的已选择的 TreePath 的当前集合中的最小值。
 PropertyChangeListener[] getPropertyChangeListeners()
          返回此 DefaultTreeSelectionModel 上注册的所有属性更改侦听器组成的数组。
 RowMapper getRowMapper()
          返回能够将 TreePath 映射到行的 RowMapper 实例。
 int getSelectionCount()
          返回选择的路径数。
 int getSelectionMode()
          返回选择模式,它是 SINGLE_TREE_SELECTIONDISCONTIGUOUS_TREE_SELECTIONCONTIGUOUS_TREE_SELECTION 之一。
 TreePath getSelectionPath()
          返回 selection 中的第一个路径。
 TreePath[] getSelectionPaths()
          返回 selection 中的路径。
 int[] getSelectionRows()
          返回当前已选的所有行。
 TreeSelectionListener[] getTreeSelectionListeners()
          返回已在此模型上注册的所有树选择侦听器组成的数组。
protected  void insureRowContinuity()
          确保当前选择的 TreePath 对于当前选择模式是有效的。
protected  void insureUniqueness()
          此方法已过时,并且其实现现在是一个 noop。
 boolean isPathSelected(TreePath path)
          如果当前选择了路径 path,则返回 true。
 boolean isRowSelected(int row)
          如果选择了由 row 标识的行,则返回 true。
 boolean isSelectionEmpty()
          如果 selection 当前为空,则返回 true。
protected  void notifyPathChange(Vector<javax.swing.tree.PathPlaceHolder> changedPaths, TreePath oldLeadSelection)
          通知侦听器路径中的更改。
 void removePropertyChangeListener(PropertyChangeListener listener)
          从侦听器列表移除 PropertyChangeListener。
 void removeSelectionPath(TreePath path)
          从 selection 中移除路径。
 void removeSelectionPaths(TreePath[] paths)
          从 selection 中移除路径。
 void removeTreeSelectionListener(TreeSelectionListener x)
          从每次已选的 TreePaths 集合发生更改时都会得到通知的侦听器列表中移除 x。
 void resetRowSelection()
          更新此对象从 TreePath 到行的映射关系。
 void setRowMapper(RowMapper newMapper)
          设置 RowMapper 实例。
 void setSelectionMode(int mode)
          设置选择模式,它必须是 SINGLE_TREE_SELECTION、CONTIGUOUS_TREE_SELECTION 或 DISCONTIGUOUS_TREE_SELECTION 之一。
 void setSelectionPath(TreePath path)
          为该选择设置路径。
 void setSelectionPaths(TreePath[] pPaths)
          为该选择设置路径中的路径。
 String toString()
          返回显示并标识此对象属性的字符串。
protected  void updateLeadIndex()
          更新 leadIndex 实例变量。
 
从类 java.lang.Object 继承的方法
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

字段详细信息

SELECTION_MODE_PROPERTY

public static final String SELECTION_MODE_PROPERTY
selectionMode 的属性名称。

另请参见:
常量字段值

changeSupport

protected SwingPropertyChangeSupport changeSupport
用于接收通知的注册侦听器。


selection

protected TreePath[] selection
当前选择的路径。如果当前没有选择任何内容,则路径为 null。


listenerList

protected EventListenerList listenerList
事件侦听器列表。


rowMapper

protected transient RowMapper rowMapper
提供给定路径的行。


listSelectionModel

protected DefaultListSelectionModel listSelectionModel
维护列表选择模型的句柄。RowMapper 用于从 TreePath 映射到行,并将该值放置在此处。


selectionMode

protected int selectionMode
用于 selection 的模式,将是 SINGLE_TREE_SELECTION、CONTIGUOUS_TREE_SELECTION 或 DISCONTIGUOUS_TREE_SELECTION。


leadPath

protected TreePath leadPath
添加的最后一条路径。


leadIndex

protected int leadIndex
selection 中前导路径的索引。


leadRow

protected int leadRow
前导行。

构造方法详细信息

DefaultTreeSelectionModel

public DefaultTreeSelectionModel()
使用 DISCONTIGUOUS_TREE_SELECTION 的选择模式创建空的 DefaultTreeSelectionModel 新实例。

方法详细信息

setRowMapper

public void setRowMapper(RowMapper newMapper)
设置 RowMapper 实例。此实例用于确定特定 TreePath 的行。

指定者:
接口 TreeSelectionModel 中的 setRowMapper

getRowMapper

public RowMapper getRowMapper()
返回能够将 TreePath 映射到行的 RowMapper 实例。

指定者:
接口 TreeSelectionModel 中的 getRowMapper

setSelectionMode

public void setSelectionMode(int mode)
设置选择模式,它必须是 SINGLE_TREE_SELECTION、CONTIGUOUS_TREE_SELECTION 或 DISCONTIGUOUS_TREE_SELECTION 之一。如果模式不是定义的值之一,则假定模式为 DISCONTIGUOUS_TREE_SELECTION

如果当前的 selection 对新模式无效,这可以更改该 selection。例如,如果将模式更改为 SINGLE_TREE_SELECTION 时选中三个 TreePath,则只有一个 TreePath 仍然被选中。由特定的实现来决定保留选定哪一个 TreePath。

将该模式设置为定义的类型以外的其他类型,将导致该模式变成 DISCONTIGUOUS_TREE_SELECTION

指定者:
接口 TreeSelectionModel 中的 setSelectionMode

getSelectionMode

public int getSelectionMode()
返回选择模式,它是 SINGLE_TREE_SELECTIONDISCONTIGUOUS_TREE_SELECTIONCONTIGUOUS_TREE_SELECTION 之一。

指定者:
接口 TreeSelectionModel 中的 getSelectionMode

setSelectionPath

public void setSelectionPath(TreePath path)
为该选择设置路径。如果这表示一个更改,则通知 TreeSelectionListener。如果 path 为 null,则这与调用 clearSelection 具有相同的效果。

指定者:
接口 TreeSelectionModel 中的 setSelectionPath
参数:
path - 要选择的新路径

setSelectionPaths

public void setSelectionPaths(TreePath[] pPaths)
为该选择设置路径中的路径。如果这表示一个更改,则通知 TreeSelectionListener。此对象将可能保持路径;换句话说,不更改传入数组中的任何对象。

如果 paths 为 null,则这与调用 clearSelection 具有相同的效果。

将前导路径设置为 pPaths 中的最后一个路径。

如果选择模式为 CONTIGUOUS_TREE_SELECTION,并且添加新路径会使该选择不连续,则将该选择重新设置为 paths 中的第一个 TreePath。

指定者:
接口 TreeSelectionModel 中的 setSelectionPaths
参数:
pPaths - 新的选择

addSelectionPath

public void addSelectionPath(TreePath path)
将路径添加到当前的 selection 中。如果路径当前不在 selection 中,则通知 TreeSelectionListener。如果 path 为 null,则通知无效。

指定者:
接口 TreeSelectionModel 中的 addSelectionPath
参数:
path - 添加到当前 selection 中的新路径

addSelectionPaths

public void addSelectionPaths(TreePath[] paths)
向当前的 selection 中添加路径。如果路径中的任何路径当前不在 selection 中,则通知 TreeSelectionListener。如果 paths 为 null,则此方法无效。

将前导路径设置为 paths 中的最后元素。

如果选择模式为 CONTIGUOUS_TREE_SELECTION,则添加新的路径会使选择不连续。那么可导致两种结果:如果 paths 中的 TreePaths 是连续的,则该选择会变为这些 TreePaths,相反,如果 TreePaths 不是连续的,则该选择会变为 paths 中的第一个 TreePath。

指定者:
接口 TreeSelectionModel 中的 addSelectionPaths
参数:
paths - 添加到当前 selection 的新路径

removeSelectionPath

public void removeSelectionPath(TreePath path)
从 selection 中移除路径。如果路径在 selection 中,则通知 TreeSelectionListener。如果 path 为 null,则此方法无效。

指定者:
接口 TreeSelectionModel 中的 removeSelectionPath
参数:
path - 要从选择中移除的路径

removeSelectionPaths

public void removeSelectionPaths(TreePath[] paths)
从 selection 中移除路径。如果路径中的任何路径在 selection 中,则通知 TreeSelectionListeners。如果 paths 为 null,则此方法无效。

指定者:
接口 TreeSelectionModel 中的 removeSelectionPaths
参数:
paths - 从 selection 中移除的路径

getSelectionPath

public TreePath getSelectionPath()
返回 selection 中的第一个路径。如果当前只选择一项,则此方法非常有用。

指定者:
接口 TreeSelectionModel 中的 getSelectionPath

getSelectionPaths

public TreePath[] getSelectionPaths()
返回 selection 中的路径。如果当前没有选择任何内容,则返回 null(或一个空数组)。

指定者:
接口 TreeSelectionModel 中的 getSelectionPaths

getSelectionCount

public int getSelectionCount()
返回选择的路径数。

指定者:
接口 TreeSelectionModel 中的 getSelectionCount

isPathSelected

public boolean isPathSelected(TreePath path)
如果当前选择了路径 path,则返回 true。

指定者:
接口 TreeSelectionModel 中的 isPathSelected

isSelectionEmpty

public boolean isSelectionEmpty()
如果 selection 当前为空,则返回 true。

指定者:
接口 TreeSelectionModel 中的 isSelectionEmpty

clearSelection

public void clearSelection()
清空当前的 selection。如果这表示当前 selection 中的更改,则通知选择侦听器。

指定者:
接口 TreeSelectionModel 中的 clearSelection

addTreeSelectionListener

public void addTreeSelectionListener(TreeSelectionListener x)
向每次已选的 TreePaths 集合发生更改时都会得到通知的侦听器列表添加 x。

指定者:
接口 TreeSelectionModel 中的 addTreeSelectionListener
参数:
x - 要添加的新侦听器

removeTreeSelectionListener

public void removeTreeSelectionListener(TreeSelectionListener x)
从每次已选的 TreePaths 集合发生更改时都会得到通知的侦听器列表中移除 x。

指定者:
接口 TreeSelectionModel 中的 removeTreeSelectionListener
参数:
x - 要移除的侦听器

getTreeSelectionListeners

public TreeSelectionListener[] getTreeSelectionListeners()
返回已在此模型上注册的所有树选择侦听器组成的数组。

返回:
此模型的所有 TreeSelectionListener,如果当前没有已注册的树选择侦听器,则返回一个空数组。
从以下版本开始:
1.4
另请参见:
addTreeSelectionListener(javax.swing.event.TreeSelectionListener), removeTreeSelectionListener(javax.swing.event.TreeSelectionListener)

fireValueChanged

protected void fireValueChanged(TreeSelectionEvent e)
通知已注册此对象上的树选择事件的所有侦听器。

另请参见:
addTreeSelectionListener(javax.swing.event.TreeSelectionListener), EventListenerList

getListeners

public <T extends EventListener> T[] getListeners(Class<T> listenerType)
返回当前已在此模型上注册为 FooListener 的所有对象组成的数组。FooListener 是用 addFooListener 方法注册的。

可以使用 class 字面值来指定 listenerType 参数,如 FooListener.class。例如,可以通过下面的代码查询 DefaultTreeSelectionModel m 的树选择侦听器:

TreeSelectionListener[] tsls = (TreeSelectionListener[])(m.getListeners(TreeSelectionListener.class));
如果不存在这样的侦听器,则此方法将返回一个空数组。

参数:
listenerType - 所请求侦听器的类型;该参数应该指定一个从 java.util.EventListener 继承的接口
返回:
在此容器上注册为 FooListener 的所有对象的数组,如果没有添加这样的侦听器,则返回一个空数组
抛出:
ClassCastException - 如果 listenerType 没有指定实现 java.util.EventListener 的类或接口
从以下版本开始:
1.3
另请参见:
getTreeSelectionListeners(), getPropertyChangeListeners()

getSelectionRows

public int[] getSelectionRows()
返回当前已选的所有行。如果没有已选的 TreePath 或没有设置 RowMapper,此方法将返回 null(或一个空数组)。如果某些行不可见(即:对于与 TreePath 对应的行,RowMapper 返回 -1),则可能返回长度小于选择的 TreePath 长度的数组。

指定者:
接口 TreeSelectionModel 中的 getSelectionRows

getMinSelectionRow

public int getMinSelectionRow()
返回从 RowMapper 获取的已选择的 TreePath 的当前集合中的最小值。如果没有选定任何内容,或不存在 RowMapper,则返回 -1。

指定者:
接口 TreeSelectionModel 中的 getMinSelectionRow

getMaxSelectionRow

public int getMaxSelectionRow()
返回从 RowMapper 获取的已选择的 TreePath 的当前集合中的最大值。如果没有选定任何内容,或不存在 RowMapper,则返回 -1。

指定者:
接口 TreeSelectionModel 中的 getMaxSelectionRow

isRowSelected

public boolean isRowSelected(int row)
如果选择了由 row 标识的行,则返回 true。

指定者:
接口 TreeSelectionModel 中的 isRowSelected

resetRowSelection

public void resetRowSelection()
更新此对象从 TreePath 到行的映射关系。当从 TreePaths 到整数的映射关系更改时(例如,扩展了节点),应调用此方法。

通常不必调用此方法,JTree 及其相关的侦听器将为您调用它。如果您正在实现自已的 View 类,则必须调用该方法。

这将调用 insureRowContinuity,以确保当前选择的 TreePath 基于选择模式仍然有效。

指定者:
接口 TreeSelectionModel 中的 resetRowSelection

getLeadSelectionRow

public int getLeadSelectionRow()
返回 lead selection 索引。它是添加的最后一个索引。

指定者:
接口 TreeSelectionModel 中的 getLeadSelectionRow

getLeadSelectionPath

public TreePath getLeadSelectionPath()
返回添加的最后一条路径。这可能与 JTree 维护的 leadSelectionPath 属性不同。

指定者:
接口 TreeSelectionModel 中的 getLeadSelectionPath

addPropertyChangeListener

public void addPropertyChangeListener(PropertyChangeListener listener)
将 PropertyChangeListener 添加到侦听器列表。该侦听器是为所有属性注册的。

当选择模式更改时,将激发 PropertyChangeEvent。

指定者:
接口 TreeSelectionModel 中的 addPropertyChangeListener
参数:
listener - 要添加的 PropertyChangeListener

removePropertyChangeListener

public void removePropertyChangeListener(PropertyChangeListener listener)
从侦听器列表移除 PropertyChangeListener。此方法移除一个为所有属性注册的 PropertyChangeListener。

指定者:
接口 TreeSelectionModel 中的 removePropertyChangeListener
参数:
listener - 要移除的 PropertyChangeListener

getPropertyChangeListeners

public PropertyChangeListener[] getPropertyChangeListeners()
返回此 DefaultTreeSelectionModel 上注册的所有属性更改侦听器组成的数组。

返回:
此模型的所有 PropertyChangeListener,如果当前没有注册的属性更改侦听器,则返回一个空数组
从以下版本开始:
1.4
另请参见:
addPropertyChangeListener(java.beans.PropertyChangeListener), removePropertyChangeListener(java.beans.PropertyChangeListener)

insureRowContinuity

protected void insureRowContinuity()
确保当前选择的 TreePath 对于当前选择模式是有效的。如果选择模式为 CONTIGUOUS_TREE_SELECTION,并且 RowMapper 存在,则此方法将确保所有的行是连续的,即:排序时,所有的行都是有顺序的,并且没有间隙。如果选择不是连续的,则重设该选择,以包含在排序时连续行的第一个设置。

如果选择模式为 SINGLE_TREE_SELECTION,并且选择了多个 TreePath,则重设该选择,以包含当前选择的第一个路径。


arePathsContiguous

protected boolean arePathsContiguous(TreePath[] paths)
如果路径是连续的,或者此对象没有 RowMapper,则返回 true。


canPathsBeAdded

protected boolean canPathsBeAdded(TreePath[] paths)
用于测试是否可以添加特定的 TreePath 集合。如果 paths 为 null(或空),或此对象没有 RowMapper,或当前没有选择任何内容,或选择模式为 DISCONTIGUOUS_TREE_SELECTION,或将该路径添加到当前的 selection 仍得到 TreePath 的连续集,则此方法返回 true。


canPathsBeRemoved

protected boolean canPathsBeRemoved(TreePath[] paths)
如果可以移除该路径而不中断该模型的连续性,则返回 true。这样开销相当大。


notifyPathChange

protected void notifyPathChange(Vector<javax.swing.tree.PathPlaceHolder> changedPaths,
                                TreePath oldLeadSelection)
通知侦听器路径中的更改。changePaths 应包含 PathPlaceHolder 的实例。


updateLeadIndex

protected void updateLeadIndex()
更新 leadIndex 实例变量。


insureUniqueness

protected void insureUniqueness()
此方法已过时,并且其实现现在是一个 noop。它仍由 setSelectionPaths 和 addSelectionPaths 调用,但只是用于向后兼容。


toString

public String toString()
返回显示并标识此对象属性的字符串。

覆盖:
Object 中的 toString
返回:
此对象的字符串表示形式

clone

public Object clone()
             throws CloneNotSupportedException
返回具有相同 selection 的此对象的副本。此方法不复制选择侦听器和属性侦听器。

覆盖:
Object 中的 clone
返回:
此实例的一个副本。
抛出:
CloneNotSupportedException - 此类的实例从不抛出
另请参见:
Cloneable

JavaTM 2 Platform
Standard Ed. 6

提交错误或意见

版权所有 2008 Sun Microsystems, Inc. 保留所有权利。请遵守GNU General Public License, version 2 only