- java.lang.Object
-
- javax.swing.tree.DefaultTreeSelectionModel
-
- All Implemented Interfaces:
-
Serializable,Cloneable,TreeSelectionModel
- 已知直接子类:
-
JTree.EmptySelectionModel
public class DefaultTreeSelectionModel extends Object implements Cloneable, Serializable, TreeSelectionModel
TreeSelectionModel的默认实现。 当选择中的路径更改而不是行时,将通知侦听器。 为了能够跟踪行更改,您可能希望成为树上扩展事件的侦听器,并测试其中的更改。从更新所选路径的任何方法调用resetRowSelection。 如果您将任何这些方法子类化以过滤允许的选择,请确保并消息
resetRowSelection如果您不发送超级消息。 警告:此类的序列化对象与将来的Swing版本不兼容。 当前的序列化支持适用于运行相同版本的Swing的应用程序之间的短期存储或RMI。 从1.4版本开始,支持所有JavaBeans的长期存储已被添加到java.beans软件包中。 请参阅XMLEncoder。- 另请参见:
-
JTree, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field 描述 protected SwingPropertyChangeSupportchangeSupport用于发送已注册的听众。protected intleadIndex引导路径的索引在选择。protected TreePathleadPath添加的最后路径protected intleadRow铅排。protected EventListenerListlistenerList事件侦听器列表。protected DefaultListSelectionModellistSelectionModel处理维护列表选择模型。protected RowMapperrowMapper为给定的路径提供一行。protected TreePath[]selection当前选择的路径。static StringSELECTION_MODE_PROPERTYselectionMode的属性名称。protected intselectionMode选择的模式将是SINGLE_TREE_SELECTION,CONTIGUOUS_TREE_SELECTION或DISCONTIGUOUS_TREE_SELECTION。-
Fields inherited from interface javax.swing.tree.TreeSelectionModel
CONTIGUOUS_TREE_SELECTION, DISCONTIGUOUS_TREE_SELECTION, SINGLE_TREE_SELECTION
-
-
构造方法摘要
构造方法 Constructor 描述 DefaultTreeSelectionModel()创建一个新的DefaultTreeSelectionModel实例,该实例为空,选择模式为DISCONTIGUOUS_TREE_SELECTION。
-
方法摘要
所有方法 接口方法 具体的方法 弃用的方法 Modifier and Type 方法 描述 voidaddPropertyChangeListener(PropertyChangeListener listener)将PropertyChangeListener添加到侦听器列表。voidaddSelectionPath(TreePath path)添加当前选择的路径。voidaddSelectionPaths(TreePath[] paths)将路径添加到当前选择。voidaddTreeSelectionListener(TreeSelectionListener x)将x添加到每次选定的TreePath集合更改时通知的侦听器列表。protected booleanarePathsContiguous(TreePath[] paths)如果路径是连续的,或者该对象没有RowMapper,则返回true。protected booleancanPathsBeAdded(TreePath[] paths)用于测试是否可以添加一组特定的TreePath。protected booleancanPathsBeRemoved(TreePath[] paths)如果可以删除路径而不会破坏模型的连续性,则返回true。voidclearSelection()清空当前的选择。Objectclone()返回具有相同选择的此对象的克隆。protected voidfireValueChanged(TreeSelectionEvent e)通知所有在此对象上注册树选择事件的侦听器。TreePathgetLeadSelectionPath()返回添加的最后一个路径。intgetLeadSelectionRow()返回引导选择索引。<T extends EventListener>
T[]getListeners(Class<T> listenerType)在此模型上返回当前注册为FooListener的所有对象的数组。intgetMaxSelectionRow()返回从当前所选TreePath集合的RowMapper获取的最大值。intgetMinSelectionRow()返回从当前所选TreePath集合的RowMapper获取的最小值。PropertyChangeListener[]getPropertyChangeListeners()返回在此DefaultTreeSelectionModel上注册的所有属性更改侦听器的数组。RowMappergetRowMapper()返回能够将TreePath映射到行的RowMapper实例。intgetSelectionCount()返回所选路径的数量。intgetSelectionMode()返回选择模式,一个SINGLE_TREE_SELECTION,DISCONTIGUOUS_TREE_SELECTION或者CONTIGUOUS_TREE_SELECTION。TreePathgetSelectionPath()返回选择中的第一个路径。TreePath[]getSelectionPaths()返回选择。int[]getSelectionRows()以行为单位返回选择。TreeSelectionListener[]getTreeSelectionListeners()返回在此模型上注册的所有树选择侦听器的数组。protected voidinsureRowContinuity()确保当前选择的TreePaths对当前选择模式有效。protected voidinsureUniqueness()这种方法已经过时了,它的实现现在是一个noop。booleanisPathSelected(TreePath path)如果路径path在当前选择中,则返回true。booleanisRowSelected(int row)如果选择由row确定的行,则返回true。booleanisSelectionEmpty()如果选择当前为空,则返回true。protected voidnotifyPathChange(Vector<?> changedPaths, TreePath oldLeadSelection)已过时。从JDK版本1.7开始voidremovePropertyChangeListener(PropertyChangeListener listener)从侦听器列表中移除PropertyChangeListener。voidremoveSelectionPath(TreePath path)从选择中删除路径。voidremoveSelectionPaths(TreePath[] paths)从选择中删除路径。voidremoveTreeSelectionListener(TreeSelectionListener x)从每次所选树形路径组更改时通知的侦听器列表中删除x。voidresetRowSelection()将此对象的映射从TreePath更新为行。voidsetRowMapper(RowMapper newMapper)设置RowMapper实例。voidsetSelectionMode(int mode)设置选择模型,它必须是SINGLE_TREE_SELECTION,CONTIGUOUS_TREE_SELECTION或DISCONTIGUOUS_TREE_SELECTION之一。voidsetSelectionPath(TreePath path)将选择设置为路径。voidsetSelectionPaths(TreePath[] pPaths)设置选择。StringtoString()返回显示和标识此对象属性的字符串。protected voidupdateLeadIndex()更新leadIndex实例变量。
-
-
-
字段详细信息
-
SELECTION_MODE_PROPERTY
public static final String SELECTION_MODE_PROPERTY
selectionMode的属性名称。- 另请参见:
- Constant Field Values
-
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
选择的模式将是SINGLE_TREE_SELECTION,CONTIGUOUS_TREE_SELECTION或DISCONTIGUOUS_TREE_SELECTION。
-
leadPath
protected TreePath leadPath
添加的最后路径
-
leadIndex
protected int leadIndex
引导路径的索引在选择。
-
leadRow
protected int leadRow
铅排。
-
-
方法详细信息
-
setRowMapper
public void setRowMapper(RowMapper newMapper)
设置RowMapper实例。 此实例用于确定特定TreePath的行。- Specified by:
-
setRowMapper在接口TreeSelectionModel - 参数
-
newMapper- 要设置的RowMapper
-
getRowMapper
public RowMapper getRowMapper()
返回能够将TreePath映射到行的RowMapper实例。- Specified by:
-
getRowMapper在接口TreeSelectionModel - 结果
- 可以将TreePath映射到行的RowMapper实例
-
setSelectionMode
public void setSelectionMode(int mode)
设置选择模型,它必须是SINGLE_TREE_SELECTION,CONTIGUOUS_TREE_SELECTION或DISCONTIGUOUS_TREE_SELECTION之一。 如果模式不是定义的值之一,则假定为DISCONTIGUOUS_TREE_SELECTION。如果当前选择对新模式无效,这可能会更改选择。 例如,如果在模式更改为
SINGLE_TREE_SELECTION时选择了三个TreePath,则只保留一个TreePath。 确定什么TreePath保持选择是由特定的实现决定的。将模式设置为定义类型以外的模式将导致模式变为
DISCONTIGUOUS_TREE_SELECTION。- Specified by:
-
setSelectionMode接口TreeSelectionModel - 参数
-
mode- 要设置的选择模式
-
getSelectionMode
public int getSelectionMode()
返回选择模式,一个SINGLE_TREE_SELECTION,DISCONTIGUOUS_TREE_SELECTION或者CONTIGUOUS_TREE_SELECTION。- Specified by:
-
getSelectionMode在接口TreeSelectionModel - 结果
- 当前选择模式
-
setSelectionPath
public void setSelectionPath(TreePath path)
将选择设置为路径。 如果这表示更改,则会通知TreeSelectionListeners。 如果path为null,则与调用clearSelection具有相同的效果。- Specified by:
-
setSelectionPath在接口TreeSelectionModel - 参数
-
path- 要选择的新路径
-
setSelectionPaths
public void setSelectionPaths(TreePath[] pPaths)
设置选择。 所提供的路径是否作为新选择取决于选择模式。 如果提供的数组为null或为空,则清除选择。 如果选择模式为SINGLE_TREE_SELECTION,则仅使用pPaths的第一个路径。 如果选择模式为CONTIGUOUS_TREE_SELECTION,并且提供的路径不连续,则仅使用pPaths的第一个路径。 如果选择模式为DISCONTIGUOUS_TREE_SELECTION,则使用所有路径。全部
null路径在pPaths被忽略。如果这是一个变化,所有注册的
TreeSelectionListener都被通知。引导路径设置为最后一个唯一路径。
从
getSelectionPaths返回的路径与提供给此方法的路径相同。- Specified by:
-
setSelectionPaths在接口TreeSelectionModel - 参数
-
pPaths- 新的选择
-
addSelectionPath
public void addSelectionPath(TreePath path)
添加当前选择的路径。 如果路径当前不在选择中,则会通知TreeSelectionListeners。 如果path为空,则path。- Specified by:
-
addSelectionPath在接口TreeSelectionModel - 参数
-
path- 添加到当前选择的新路径
-
addSelectionPaths
public void addSelectionPaths(TreePath[] paths)
将路径添加到当前选择。 如果路径中的任何路径当前不在选择中,则会通知TreeSelectionListeners。 如果paths为空,则paths。引导路径设置为
paths的最后一个元素。如果选择模式为
CONTIGUOUS_TREE_SELECTION,并添加新路径将使选择不连续。 那么可以产生两件事情:如果paths中的TreePaths是连续的,则选择将成为这些TreePath,否则TreePath不连续,并且选择将成为paths的第一个TreePath。- Specified by:
-
addSelectionPaths在接口TreeSelectionModel - 参数
-
paths- 添加到当前选择的新路径
-
removeSelectionPath
public void removeSelectionPath(TreePath path)
从选择中删除路径。 如果路径在选择中TreeSelectionListeners被通知。 如果path为空,则path。- Specified by:
-
removeSelectionPath在接口TreeSelectionModel - 参数
-
path- 从选择中删除的路径
-
removeSelectionPaths
public void removeSelectionPaths(TreePath[] paths)
从选择中删除路径。 如果路径中的任何路径都在选择中,则会通知TreeSelectionListeners。 如果paths为空,则paths。- Specified by:
-
removeSelectionPaths在接口TreeSelectionModel - 参数
-
paths- 从选择中删除的路径
-
getSelectionPath
public TreePath getSelectionPath()
返回选择中的第一个路径。 如果只有当前选择的一个项目,这是有用的。- Specified by:
-
getSelectionPath接口TreeSelectionModel - 结果
- 选择中的第一条路径
-
getSelectionPaths
public TreePath[] getSelectionPaths()
返回选择。- Specified by:
-
getSelectionPaths在接口TreeSelectionModel - 结果
- 选择
-
getSelectionCount
public int getSelectionCount()
返回所选路径的数量。- Specified by:
-
getSelectionCount接口TreeSelectionModel - 结果
- 所选路径的数量
-
isPathSelected
public boolean isPathSelected(TreePath path)
如果当前选择的路径为path,则返回true。- Specified by:
-
isPathSelected在接口TreeSelectionModel - 参数
-
path- 要被爱的路径 - 结果
-
path是否在当前选择
-
isSelectionEmpty
public boolean isSelectionEmpty()
如果选择当前为空,则返回true。- Specified by:
-
isSelectionEmpty在接口TreeSelectionModel - 结果
- 选择当前是否为空
-
clearSelection
public void clearSelection()
清空当前的选择。 如果这表示当前选择的变化,通知选择听众。- Specified by:
-
clearSelection接口TreeSelectionModel
-
addTreeSelectionListener
public void addTreeSelectionListener(TreeSelectionListener x)
将x添加到每次选定的TreePath集合更改时通知的侦听器列表。- Specified by:
-
addTreeSelectionListener在接口TreeSelectionModel - 参数
-
x- 要添加的新侦听器
-
removeTreeSelectionListener
public void removeTreeSelectionListener(TreeSelectionListener x)
从每次所选树形路径组更改时通知的侦听器列表中删除x。- Specified by:
-
removeTreeSelectionListener在接口TreeSelectionModel - 参数
-
x- 要删除的侦听器
-
getTreeSelectionListeners
public TreeSelectionListener[] getTreeSelectionListeners()
返回在此模型上注册的所有树选择侦听器的数组。- 结果
-
所有这个模型的
TreeSelectionListeners或一个空数组,如果没有树选择侦听器当前注册 - 从以下版本开始:
- 1.4
- 另请参见:
-
addTreeSelectionListener(javax.swing.event.TreeSelectionListener),removeTreeSelectionListener(javax.swing.event.TreeSelectionListener)
-
fireValueChanged
protected void fireValueChanged(TreeSelectionEvent e)
通知所有在此对象上注册树选择事件的侦听器。- 参数
-
e- 表征变更的事件 - 另请参见:
-
addTreeSelectionListener(javax.swing.event.TreeSelectionListener),EventListenerList
-
getListeners
public <T extends EventListener> T[] getListeners(Class<T> listenerType)
返回此模型上当前注册为FooListener的所有对象的数组。FooListener用addFooListener注册。您可以使用类文字指定
listenerType参数,如FooListener.class。 例如,您可以使用以下代码查询DefaultTreeSelectionModelm的树选择侦听器:TreeSelectionListener[] tsls = (TreeSelectionListener[])(m.getListeners(TreeSelectionListener.class));如果没有这样的侦听器存在,这个方法返回一个空数组。- 参数类型
-
T- 监听器类型 - 参数
-
listenerType- 所请求的听众的类型 - 结果
-
在此组件上注册为
FooListener的所有对象的数组,如果没有添加此类侦听器,则为空数组 - 异常
-
ClassCastException- 如果listenerType没有指定实现java.util.EventListener的类或接口 - 从以下版本开始:
- 1.3
- 另请参见:
-
getTreeSelectionListeners(),getPropertyChangeListeners()
-
getSelectionRows
public int[] getSelectionRows()
以行为单位返回选择。 有不一定之间的一对一的映射TreePath从返回小号getSelectionPaths和这种方法。 特别地,如果TreePath不可见(RowMapper返回-1对应于TreePath的行),则相应的行不包括在返回的数组中。 例如,如果选择由两个路径组成,即A和B,其中A在10和B当前不可见,则该方法返回一个具有单个条目10的数组。- Specified by:
-
getSelectionRows接口TreeSelectionModel - 结果
- 根据行的选择
-
getMinSelectionRow
public int getMinSelectionRow()
返回从当前所选TreePath集合的RowMapper获取的最小值。 如果没有选择,或没有RowMapper,则返回-1。- Specified by:
-
getMinSelectionRow接口TreeSelectionModel - 结果
- 从RowMapper获取的当前所选TreePath集合的最小值
-
getMaxSelectionRow
public int getMaxSelectionRow()
返回从当前所选TreePath集合的RowMapper获取的最大值。 如果没有选择,或没有RowMapper,则返回-1。- Specified by:
-
getMaxSelectionRow在接口TreeSelectionModel - 结果
- 从RowMapper获取的当前所选TreePath的最大值
-
isRowSelected
public boolean isRowSelected(int row)
如果选择由row标识的行,则返回true。- Specified by:
-
isRowSelected接口TreeSelectionModel - 参数
-
row- 行检查 - 结果
- 是否选择该行
-
resetRowSelection
public void resetRowSelection()
将此对象的映射从TreePath更新为行。 当从TreePaths到整数的映射已经改变(例如,一个节点已经被扩展)时,这个应该被调用。通常不需要调用它,JTree及其关联的侦听器将为您调用。 如果你正在实现自己的View类,那么你将不得不调用这个。
这将调用
insureRowContinuity以确保当前选择的TreePaths基于选择模式仍然有效。- Specified by:
-
resetRowSelection接口TreeSelectionModel
-
getLeadSelectionRow
public int getLeadSelectionRow()
返回引导选择索引。 这是添加的最后一个索引。- Specified by:
-
getLeadSelectionRow在接口TreeSelectionModel - 结果
- 主导选择指数
-
getLeadSelectionPath
public TreePath getLeadSelectionPath()
返回添加的最后一个路径。 这可能与JTree维护的leadSelectionPath属性不同。- Specified by:
-
getLeadSelectionPath接口TreeSelectionModel - 结果
- 添加的最后一条路径
-
addPropertyChangeListener
public void addPropertyChangeListener(PropertyChangeListener listener)
将PropertyChangeListener添加到侦听器列表。 所有属性都注册了监听器。当选择模式更改时,PropertyChangeEvent将被触发。
- Specified by:
-
addPropertyChangeListener接口TreeSelectionModel - 参数
-
listener- 要添加的PropertyChangeListener
-
removePropertyChangeListener
public void removePropertyChangeListener(PropertyChangeListener listener)
从侦听器列表中移除PropertyChangeListener。 这会删除为所有属性注册的PropertyChangeListener。- Specified by:
-
removePropertyChangeListener接口TreeSelectionModel - 参数
-
listener- 要删除的PropertyChangeListener
-
getPropertyChangeListeners
public PropertyChangeListener[] getPropertyChangeListeners()
返回在该DefaultTreeSelectionModel上注册的所有属性更改侦听器的数组。- 结果
-
所有这个模型的
PropertyChangeListeners或一个空数组,如果没有属性更改侦听器当前注册 - 从以下版本开始:
- 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。- 参数
-
paths- 要检查的路径数组 - 结果
- 路径是否是连续的,或者该对象没有RowMapper
-
canPathsBeAdded
protected boolean canPathsBeAdded(TreePath[] paths)
用于测试是否可以添加一组特定的TreePath。 如果paths为空(或为空),或者该对象没有RowMapper,或者当前没有选择任何内容,或者选择模式为DISCONTIGUOUS_TREE_SELECTION,或者添加到当前选择的路径仍然会产生连续的一组TreePaths,则TreePath。- 参数
-
paths-的阵列TreePaths检查 - 结果
-
是否可以添加特定的
TreePaths集
-
canPathsBeRemoved
protected boolean canPathsBeRemoved(TreePath[] paths)
如果可以删除路径而不会破坏模型的连续性,则返回true。 这是相当昂贵的。- 参数
-
paths-的阵列TreePath检查 - 结果
- 是否可以删除路径,而不会破坏模型的连续性
-
notifyPathChange
@Deprecated protected void notifyPathChange(Vector<?> changedPaths, TreePath oldLeadSelection)
已过时。 从JDK版本1.7开始通知听众路径变化。 changePaths应该包含PathPlaceHolder的实例。- 参数
-
changedPaths- 改变路径的向量 -
oldLeadSelection- 旧的选择路径
-
updateLeadIndex
protected void updateLeadIndex()
更新leadIndex实例变量。
-
insureUniqueness
protected void insureUniqueness()
这种方法已经过时了,它的实现现在是一个noop。 它仍然由setSelectionPaths和addSelectionPaths调用,但仅用于向后兼容。
-
clone
public Object clone() throws CloneNotSupportedException
返回具有相同选择的此对象的克隆。 此方法不会重复选择侦听器和属性侦听器。- 重写:
-
clone类Object - 结果
- 这个实例的一个克隆。
- 异常
-
CloneNotSupportedException- 从不会被这个类的实例抛出 - 另请参见:
-
Cloneable
-
-