- java.lang.Object
-
- javax.swing.tree.DefaultTreeSelectionModel
-
- 实现的所有接口
-
Serializable,Cloneable,TreeSelectionModel
- 已知直接子类:
-
JTree.EmptySelectionModel
public class DefaultTreeSelectionModel extends Object implements Cloneable, Serializable, TreeSelectionModel
TreeSelectionModel的默认实现。 只要选择中的路径发生更改而不是行,就会通知监听器。 为了能够跟踪行更改,您可能希望成为树上扩展事件的监听器,并从那里测试更改。从更新所选路径的任何方法调用resetRowSelection。 如果您将这些方法中的任何一个子类
resetRowSelection过滤允许选择的内容,请确保并且如果您没有消息超级,请发送消息resetRowSelection。 警告:此类的序列化对象与以后的Swing版本不兼容。 当前的序列化支持适用于运行相同版本Swing的应用程序之间的短期存储或RMI。 从1.4开始,对java.beans软件包中添加了对所有JavaBeansjava.beans长期存储的支持。 请参阅XMLEncoder。- 另请参见:
-
JTree, Serialized Form
-
-
字段汇总
字段 变量和类型 字段 描述 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 declared in interface javax.swing.tree.TreeSelectionModel
CONTIGUOUS_TREE_SELECTION, DISCONTIGUOUS_TREE_SELECTION, SINGLE_TREE_SELECTION
-
-
构造方法摘要
构造方法 构造器 描述 DefaultTreeSelectionModel()创建一个空的DefaultTreeSelectionModel的新实例,其选择模式为DISCONTIGUOUS_TREE_SELECTION。
-
方法摘要
所有方法 实例方法 具体的方法 弃用的方法 变量和类型 方法 描述 voidaddPropertyChangeListener(PropertyChangeListener listener)将PropertyChangeListener添加到侦听器列表。voidaddSelectionPath(TreePath path)添加当前选择的路径。voidaddSelectionPaths(TreePath[] paths)添加当前选择的路径。voidaddTreeSelectionListener(TreeSelectionListener x)将x添加到每次选定的TreePaths更改时通知的侦听器列表。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(类<T> listenerType)返回此模型上当前注册为FooListener的所有对象的数组。intgetMaxSelectionRow()返回从RowMapper获取的当前选定TreePath集合的最大值。intgetMinSelectionRow()返回从RowMapper获取的当前选定TreePath集合的最小值。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()确保当前所选的TreePath对当前选择模式有效。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)从每次选定的TreePaths更改集时收到通知的侦听器列表中删除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的属性名称。- 另请参见:
- 常数字段值
-
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 - 结果
- RowMapper实例,它能够将TreePath映射到一行
-
setSelectionMode
public void setSelectionMode(int mode)
设置选择模型,该模型必须是SINGLE_TREE_SELECTION,CONTIGUOUS_TREE_SELECTION或DISCONTIGUOUS_TREE_SELECTION之一。 如果mode不是定义的值之一,则假定为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中的所有null路径pPaths被忽略。如果这代表更改,则通知所有已注册的
TreeSelectionListener。引导路径设置为最后一个唯一路径。
从
getSelectionPaths返回的路径与提供给此方法的路径的顺序相同。- Specified by:
-
setSelectionPaths在界面TreeSelectionModel - 参数
-
pPaths- 新选择
-
addSelectionPath
public void addSelectionPath(TreePath path)
添加当前选择的路径。 如果路径当前未在选择中,则会通知TreeSelectionListeners。 如果path为空,则无效。- Specified by:
-
addSelectionPath在界面TreeSelectionModel - 参数
-
path- 要添加到当前选择的新路径
-
addSelectionPaths
public void addSelectionPaths(TreePath[] paths)
添加当前选择的路径。 如果路径中的任何路径当前不在选择中,则会通知TreeSelectionListeners。 如果paths为空,则无效。引导路径设置为
paths的最后一个元素。如果选择模式为
CONTIGUOUS_TREE_SELECTION,则添加新路径会使选择不连续。 然后可能会产生两件事:如果paths中的TreePaths是连续的,那么选择将成为这些TreePaths,否则TreePaths将不连续,并且选择将成为paths的第一个TreePath。- Specified by:
-
addSelectionPaths接口TreeSelectionModel - 参数
-
paths- 要添加到当前选择的新路径
-
removeSelectionPath
public void removeSelectionPath(TreePath path)
从选择中删除路径。 如果路径在选择中,则会通知TreeSelectionListeners。 如果path为null,则无效。- Specified by:
-
removeSelectionPath接口TreeSelectionModel - 参数
-
path- 要从选择中删除的路径
-
removeSelectionPaths
public void removeSelectionPaths(TreePath[] paths)
从选择中删除路径。 如果路径中的任何路径都在选择中,则会通知TreeSelectionListeners。 如果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添加到每次选定的TreePaths更改时通知的侦听器列表。- Specified by:
-
addTreeSelectionListener接口TreeSelectionModel - 参数
-
x- 要添加的新侦听器
-
removeTreeSelectionListener
public void removeTreeSelectionListener(TreeSelectionListener x)
从每次选定的TreePaths更改集时收到通知的侦听器列表中删除x。- Specified by:
-
removeTreeSelectionListener接口TreeSelectionModel - 参数
-
x- 要删除的侦听器
-
getTreeSelectionListeners
public TreeSelectionListener[] getTreeSelectionListeners()
返回在此模型上注册的所有树选择侦听器的数组。- 结果
-
如果当前没有注册树选择侦听器,则所有此模型的
TreeSelectionListener或空数组 - 从以下版本开始:
- 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(类<T> listenerType)
返回此模型上当前注册为FooListener的所有对象的数组。FooListeners使用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()
返回从RowMapper获取的当前选定TreePath集合的最小值。 如果未选择任何内容,或者没有RowMapper,则返回-1。- Specified by:
-
getMinSelectionRow接口TreeSelectionModel - 结果
- 从RowMapper获取的当前选定TreePath集合的最小值
-
getMaxSelectionRow
public int getMaxSelectionRow()
返回从RowMapper获取的当前选定TreePath集合的最大值。 如果未选择任何内容,或者没有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上注册的所有属性更改侦听器的数组。- 结果
-
如果当前没有注册属性更改侦听器,则所有此模型的
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。- 参数
-
paths- 要检查的路径数组 - 结果
- 路径是否连续,或者此对象没有RowMapper
-
canPathsBeAdded
protected boolean canPathsBeAdded(TreePath[] paths)
用于测试是否可以添加一组特定的TreePath。 如果paths为空(或为空),或者此对象没有RowMapper,或者当前没有选择任何内容,或者选择模式为DISCONTIGUOUS_TREE_SELECTION,或者将路径添加到当前选择仍会导致连续的TreePath组,则TreePath。- 参数
-
paths- 要检查的数组TreePaths - 结果
-
是否可以添加特定的
TreePaths组
-
canPathsBeRemoved
protected boolean canPathsBeRemoved(TreePath[] paths)
如果可以在不破坏模型连续性的情况下删除路径,则返回true。 这相当昂贵。- 参数
-
paths- 要检查的数组TreePath - 结果
- 是否可以在不破坏模型连续性的情况下删除路径
-
notifyPathChange
@Deprecated protected void notifyPathChange(Vector<?> changedPaths, TreePath oldLeadSelection)
Deprecated.As of JDK version 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
-
-