- 所有已实现的接口:
-
Serializable,Cloneable,ListSelectionModel
警告: 该类的序列化对象将不兼容未来的Swing版本。当前的序列化支持适用于短期存储或在运行相同Swing版本的应用程序之间的RMI。从1.4版本开始,已将所有JavaBeans的长期存储支持添加到java.beans包中。请参阅XMLEncoder。
- 自版本:
- 1.2
- 参见:
-
Field Summary
FieldsFields declared in interface javax.swing.ListSelectionModel
MULTIPLE_INTERVAL_SELECTION, SINGLE_INTERVAL_SELECTION, SINGLE_SELECTION -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid向列表添加一个监听器,每当选择发生变化时通知。voidaddSelectionInterval(int index0, int index1) 将选择更改为当前选择和index0到index1之间的索引的并集。void将选择更改为空集。clone()返回具有相同选择的此选择模型的克隆。protected voidfireValueChanged(boolean isAdjusting) 通知监听器我们已结束一系列调整。protected voidfireValueChanged(int firstIndex, int lastIndex) 通知ListSelectionListeners选择的值已更改,闭合区间为firstIndex到lastIndex。protected voidfireValueChanged(int firstIndex, int lastIndex, boolean isAdjusting) 通知ListSelectionListeners选择的值已更改,闭合区间为firstIndex到lastIndex,如果这是调整系列中的最终更改。int返回最近一次调用setSelectionInterval()、addSelectionInterval()或removeSelectionInterval()的第一个索引参数。int返回最近一次调用setSelectionInterval()、addSelectionInterval()或removeSelectionInterval()的第二个索引参数。<T extends EventListener>
T[]getListeners(Class<T> listenerType) 返回当前注册为此模型的所有FooListener的对象数组。返回在此DefaultListSelectionModel上注册的所有列表选择监听器的数组。int返回最后选择的索引,如果选择为空则返回-1。int返回第一个选择的索引,如果选择为空则返回-1。int返回当前选择模式。boolean如果选择正在进行一系列更改,则返回true。voidinsertIndexInterval(int index, int length, boolean before) 在索引之前/之后插入长度索引。boolean返回leadAnchorNotificationEnabled标志的值。booleanisSelectedIndex(int index) 如果指定的索引被选中,则返回true。boolean如果没有索引被选中,则返回true。voidmoveLeadSelectionIndex(int leadIndex) 设置主选择索引,保持所有选择值不变。voidremoveIndexInterval(int index0, int index1) 从选择模型中删除区间index0到index1(包括)的索引。void从列表中删除每次选择更改时通知的监听器。voidremoveSelectionInterval(int index0, int index1) 将选择更改为当前选择和index0到index1之间的索引的差集。voidsetAnchorSelectionIndex(int anchorIndex) 设置锚点选择索引,保持所有选择值不变。voidsetLeadAnchorNotificationEnabled(boolean flag) 设置leadAnchorNotificationEnabled标志的值。voidsetLeadSelectionIndex(int leadIndex) 设置主选择索引,确保锚点和新主之间的值要么全部选中,要么全部取消选中。voidsetSelectionInterval(int index0, int index1) 将选择更改为index0到index1之间的选择。voidsetSelectionMode(int selectionMode) 设置选择模式。voidsetValueIsAdjusting(boolean isAdjusting) 设置valueIsAdjusting属性,指示即将发生的选择更改是否应视为单个更改的一部分。toString()返回显示和标识此对象属性的字符串。Methods declared in class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods declared in interface javax.swing.ListSelectionModel
getSelectedIndices, getSelectedItemsCount
-
Field Details
-
listenerList
监听器列表。 -
leadAnchorNotificationEnabled
protected boolean leadAnchorNotificationEnabled是否启用主锚点通知。
-
-
Constructor Details
-
DefaultListSelectionModel
public DefaultListSelectionModel()构造一个DefaultListSelectionModel。
-
-
Method Details
-
getMinSelectionIndex
public int getMinSelectionIndex()返回第一个选择的索引,如果选择为空则返回-1。- 指定者:
-
getMinSelectionIndex在接口ListSelectionModel - 返回:
- 第一个选择的索引,如果选择为空则返回-1。
-
getMaxSelectionIndex
public int getMaxSelectionIndex()返回最后选择的索引,如果选择为空则返回-1。- 指定者:
-
getMaxSelectionIndex在接口ListSelectionModel - 返回:
- 最后选择的索引,如果选择为空则返回-1。
-
getValueIsAdjusting
public boolean getValueIsAdjusting()返回选择是否正在进行一系列更改。- 指定者:
-
getValueIsAdjusting在接口ListSelectionModel - 返回:
-
如果选择正在进行一系列更改,则返回
true - 参见:
-
getSelectionMode
public int getSelectionMode()返回当前选择模式。- 指定者:
-
getSelectionMode在接口ListSelectionModel - 返回:
- 当前选择模式
- 参见:
-
setSelectionMode
public void setSelectionMode(int selectionMode) 设置选择模式。以下列表描述了可接受的选择模式:ListSelectionModel.SINGLE_SELECTION- 一次只能选择一个列表索引。在此模式下,setSelectionInterval和addSelectionInterval是等效的,两者都用第二个参数("lead")表示的索引替换当前选择。ListSelectionModel.SINGLE_INTERVAL_SELECTION- 一次只能选择一个连续区间。在此模式下,addSelectionInterval的行为类似于setSelectionInterval(替换当前选择),除非给定的区间与现有选择紧邻或重叠,因此可以用于扩展选择。ListSelectionModel.MULTIPLE_INTERVAL_SELECTION- 在此模式下,对可选择内容没有限制。
- 指定者:
-
setSelectionMode在接口ListSelectionModel - 参数:
-
selectionMode- 选择模式 - 抛出:
-
IllegalArgumentException- 如果选择模式不是允许的模式之一 - 参见:
-
isSelectedIndex
public boolean isSelectedIndex(int index) 如果指定的索引被选中,则返回true。- 指定者:
-
isSelectedIndex在接口ListSelectionModel - 参数:
-
index- 一个索引 - 返回:
-
如果指定的索引被选中,则返回
true
-
isSelectionEmpty
public boolean isSelectionEmpty()如果没有索引被选中,则返回true。- 指定者:
-
isSelectionEmpty在接口ListSelectionModel - 返回:
-
如果没有索引被选中,则返回
true
-
addListSelectionListener
向列表添加一个监听器,每当选择发生变化时通知。- 指定者:
-
addListSelectionListener在接口ListSelectionModel中 - 参数:
-
l- ListSelectionListener - 另请参阅:
-
removeListSelectionListener
从通知每次更改选择时的列表中删除侦听器。- 指定者:
-
removeListSelectionListener在接口ListSelectionModel中 - 参数:
-
l- ListSelectionListener - 另请参阅:
-
getListSelectionListeners
返回在此DefaultListSelectionModel上注册的所有列表选择侦听器的数组。- 返回:
- 所有此模型的ListSelectionListener或如果当前未注册任何列表选择侦听器,则返回空数组
- 自从:
- 1.4
- 另请参阅:
-
fireValueChanged
protected void fireValueChanged(boolean isAdjusting) 通知侦听器我们已经结束了一系列调整。- 参数:
-
isAdjusting- 如果这是一系列调整中的最终更改,则为true
-
fireValueChanged
protected void fireValueChanged(int firstIndex, int lastIndex) 通知ListSelectionListeners选择的值在封闭区间firstIndex、lastIndex中已更改。- 参数:
-
firstIndex- 区间中的第一个索引 -
lastIndex- 区间中的最后一个索引
-
fireValueChanged
protected void fireValueChanged(int firstIndex, int lastIndex, boolean isAdjusting) 通知ListSelectionListeners选择的值在封闭区间firstIndex、lastIndex中已更改,以及是否为一系列调整中的最终更改。- 参数:
-
firstIndex- 区间中的第一个索引 -
lastIndex- 区间中的最后一个索引 -
isAdjusting- 如果这是一系列调整中的最终更改,则为true - 另请参阅:
-
getListeners
返回当前在此模型上注册为FooListener的所有对象的数组。使用addFooListener方法注册FooListener。您可以使用类字面量(例如
FooListener.class)指定listenerType参数。例如,您可以使用以下代码查询DefaultListSelectionModel实例m的列表选择侦听器:ListSelectionListener[] lsls = (ListSelectionListener[])(m.getListeners(ListSelectionListener.class));
如果不存在此类侦听器,则此方法返回一个空数组。- 类型参数:
-
T- 请求的EventListener类的类型 - 参数:
-
listenerType- 请求的侦听器类型;此参数应指定从java.util.EventListener继承的接口 - 返回:
- 在此模型上注册为FooListener的所有对象的数组,如果尚未添加此类侦听器,则返回一个空数组
- 抛出:
-
ClassCastException- 如果listenerType未指定实现java.util.EventListener的类或接口 - 自从:
- 1.3
- 另请参阅:
-
setLeadAnchorNotificationEnabled
public void setLeadAnchorNotificationEnabled(boolean flag) 设置leadAnchorNotificationEnabled标志的值。- 参数:
-
flag-leadAnchorNotificationEnabled的布尔值 - 另请参阅:
-
isLeadAnchorNotificationEnabled
public boolean isLeadAnchorNotificationEnabled()返回leadAnchorNotificationEnabled标志的值。当leadAnchorNotificationEnabled为true时,模型生成通知事件,其范围包括对选择的所有更改以及对主导和锚点索引的更改。将标志设置为false会导致事件范围缩小,仅包括自上次更改以来已选择或取消选择的元素。无论如何,模型都会继续在内部维护主导和锚点变量。默认值为true。注意:主导或锚点可能会更改而不更改选择。通知这些更改通常很重要,例如当需要在视图中更新新的主导或锚点时。因此,在更改默认值时应谨慎。
- 返回:
-
leadAnchorNotificationEnabled标志的值 - 另请参阅:
-
clearSelection
public void clearSelection()将选择更改为空集。如果这代表对当前选择的更改,则通知每个ListSelectionListener。- 指定者:
-
clearSelection在接口ListSelectionModel中 - 另请参阅:
-
setSelectionInterval
public void setSelectionInterval(int index0, int index1) 将选择更改为index0和index1之间(包括index0和index1)的集合。index0不必小于或等于index1。在SINGLE_SELECTION选择模式下,仅使用第二个索引。
如果这代表对当前选择的更改,则通知每个ListSelectionListener。
如果任一索引为-1,则此方法不执行任何操作并返回而不抛出异常。否则,如果任一索引小于-1,则会抛出IndexOutOfBoundsException。
- 指定者:
-
setSelectionInterval在接口ListSelectionModel中 - 参数:
-
index0- 区间的一端。 -
index1- 区间的另一端 - 抛出:
-
IndexOutOfBoundsException- 如果任一索引小于-1(且任一索引不是-1) - 另请参阅:
-
addSelectionInterval
public void addSelectionInterval(int index0, int index1) 将选择更改为当前选择和index0和index1之间(包括index0和index1)的索引的集合。在SINGLE_SELECTION选择模式下,这等效于调用setSelectionInterval,并且仅使用第二个索引。在SINGLE_INTERVAL_SELECTION选择模式下,此方法的行为类似于setSelectionInterval,除非给定的区间与现有选择紧邻或重叠,因此可以用于扩展选择。
如果这代表对当前选择的更改,则通知每个ListSelectionListener。请注意,index0不必小于或等于index1。
如果任一索引为-1,则此方法不执行任何操作并返回而不抛出异常。否则,如果任一索引小于-1,则会抛出IndexOutOfBoundsException。
- 指定者:
-
addSelectionInterval在接口ListSelectionModel中 - 参数:
-
index0- 区间的一个端点。 -
index1- 区间的另一个端点 - 抛出:
-
IndexOutOfBoundsException- 如果任一索引小于-1(且两个索引都不是-1) - 参见:
-
removeSelectionInterval
public void removeSelectionInterval(int index0, int index1) 改变选择为当前选择与index0和index1之间的索引的差集。index0不必小于或等于index1。在
SINGLE_INTERVAL_SELECTION选择模式下,如果移除会产生两个不相交的选择,则移除将通过选择的较大端点进行扩展。例如,如果选择是0-10,并且您提供索引5,6(以任何顺序),则结果选择是0-4。如果这代表对当前选择的更改,则通知每个
ListSelectionListener更改。如果任一索引为
-1,此方法不执行任何操作并返回而无异常。否则,如果任一索引小于-1,则抛出IndexOutOfBoundsException。- 指定者:
-
removeSelectionInterval在接口ListSelectionModel中 - 参数:
-
index0- 区间的一个端点 -
index1- 区间的另一个端点 - 抛出:
-
IndexOutOfBoundsException- 如果任一索引小于-1(且两个索引都不是-1) - 参见:
-
insertIndexInterval
public void insertIndexInterval(int index, int length, boolean before) 在索引之前/之后插入长度索引。如果索引处的值本身被选中且选择模式不是SINGLE_SELECTION,则将所有新插入的项目设置为选中。否则保持未选中状态。通常调用此方法以将选择模型与数据模型中的相应更改同步。- 指定者:
-
insertIndexInterval在接口ListSelectionModel中 - 参数:
-
index- 区间的起始位置 -
length- 区间的长度 -
before- 如果为true,则在index之前插入区间,否则在index之后插入区间 - 抛出:
-
IndexOutOfBoundsException- 如果index或length为负数
-
removeIndexInterval
public void removeIndexInterval(int index0, int index1) 从选择模型中删除区间index0,index1(包括)。通常调用此方法以将选择模型与数据模型中的相应更改同步。请注意(如常规),index0不必<= index1。- 指定者:
-
removeIndexInterval在接口ListSelectionModel中 - 参数:
-
index0- 区间的起始位置 -
index1- 区间的结束位置 - 抛出:
-
IndexOutOfBoundsException- 如果任一索引为负数
-
setValueIsAdjusting
public void setValueIsAdjusting(boolean isAdjusting) 设置valueIsAdjusting属性,指示即将发生的选择更改是否应视为单个更改的一部分。此属性的值用于初始化生成的ListSelectionEvent的valueIsAdjusting属性。例如,如果选择正在响应用户拖动而更新,则可以在拖动启动时将此属性设置为
true,并在拖动完成时将其设置为false。在拖动过程中,侦听器接收到具有true设置的valueIsAdjusting属性的事件。在拖动结束时,当更改完成时,侦听器接收到值设置为false的事件。如果侦听器希望仅在更改完成时更新,则可以使用此模式。将此属性设置为
true将开始一系列被视为单个更改的更改。当属性更改回false时,将发送一个事件,描述整个选择更改(如果有的话),事件的valueIsAdjusting属性设置为false。- 指定者:
-
setValueIsAdjusting在接口ListSelectionModel中 - 参数:
-
isAdjusting- 属性的新值 - 参见:
-
toString
返回显示和标识此对象属性的字符串。 -
clone
返回具有相同选择的此选择模型的克隆。不会复制listenerLists。- 覆盖:
-
clone在类Object中 - 返回:
- 此实例的克隆。
- 抛出:
-
CloneNotSupportedException- 如果选择模型既不实现Cloneable接口也不定义clone方法。 - 参见:
-
getAnchorSelectionIndex
public int getAnchorSelectionIndex()返回最近一次调用setSelectionInterval()、addSelectionInterval()或removeSelectionInterval()的第一个索引参数。最近的index0被视为“锚点”,最近的index1被视为“引导”。一些接口会特别显示这些索引,例如,Windows95会用虚线黄色轮廓显示引导索引。- 指定者:
-
getAnchorSelectionIndex在接口ListSelectionModel中 - 返回:
- 锚点选择索引
- 参见:
-
getLeadSelectionIndex
public int getLeadSelectionIndex()返回最近一次调用setSelectionInterval()、addSelectionInterval()或removeSelectionInterval()的第二个索引参数。- 指定者:
-
getLeadSelectionIndex在接口ListSelectionModel中 - 返回:
- 引导选择索引。
- 参见:
-
setAnchorSelectionIndex
public void setAnchorSelectionIndex(int anchorIndex) 设置锚点选择索引,保持所有选择值不变。如果leadAnchorNotificationEnabled为true,则发送一个覆盖旧锚点单元格和新锚点单元格的通知。- 指定者:
-
setAnchorSelectionIndex在接口ListSelectionModel中 - 参数:
-
anchorIndex- 锚点选择索引 - 参见:
-
moveLeadSelectionIndex
public void moveLeadSelectionIndex(int leadIndex) 设置引导选择索引,保持所有选择值不变。如果leadAnchorNotificationEnabled为true,则发送一个覆盖旧引导单元格和新引导单元格的通知。- 参数:
-
leadIndex- 新的主导选择索引 - 自版本:
- 1.5
- 参见:
-
setLeadSelectionIndex
public void setLeadSelectionIndex(int leadIndex) 设置主导选择索引,确保锚点和新主导之间的值要么全部选中,要么全部取消选中。如果锚点索引处的值已被选中,则首先清除范围[锚点,旧主导索引]内的所有值,然后选择范围[锚点,新主导索引]内的所有值,其中旧主导索引是旧的主导选择索引,新主导索引是新的主导选择索引。如果锚点索引处的值未被选中,则反向执行相同操作,在旧范围内选择值并在新范围内取消选择值。
为此更改生成单个事件并通知所有侦听器。为了在此事件中生成最小边界,以单个步骤执行操作;这样广播的ListSelectionEvent中的第一个和最后一个索引将指向由于此方法而实际更改值的单元格。相反,如果此操作分两步执行,则选择状态的影响将相同,但将生成两个事件,并且围绕更改值的边界将更宽,包括首先仅清除后来设置的单元格。
此方法可用于UI类的
mouseDragged方法中扩展选择。- 指定者:
-
setLeadSelectionIndex在接口ListSelectionModel - 参数:
-
leadIndex- 主导选择索引 - 参见:
-