- java.lang.Object
-
- javax.swing.DefaultListSelectionModel
-
- All Implemented Interfaces:
-
Serializable,Cloneable,ListSelectionModel
public class DefaultListSelectionModel extends Object implements ListSelectionModel, Cloneable, Serializable
列表选择的默认数据模型。警告:此类的序列化对象与将来的Swing版本不兼容。 当前的序列化支持适用于运行相同版本的Swing的应用程序之间的短期存储或RMI。 从1.4版本开始,所有JavaBeans的长期存储支持已被添加到
java.beans软件包中。 请参阅XMLEncoder。- 从以下版本开始:
- 1.2
- 另请参见:
-
ListSelectionModel, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field 描述 protected booleanleadAnchorNotificationEnabled是否启用牵头锚通知。protected EventListenerListlistenerList听众列表。-
Fields inherited from interface javax.swing.ListSelectionModel
MULTIPLE_INTERVAL_SELECTION, SINGLE_INTERVAL_SELECTION, SINGLE_SELECTION
-
-
构造方法摘要
构造方法 Constructor 描述 DefaultListSelectionModel()
-
方法摘要
所有方法 接口方法 具体的方法 Modifier and Type 方法 描述 voidaddListSelectionListener(ListSelectionListener l)将侦听器添加到每次发生更改选择时通知的列表。voidaddSelectionInterval(int index0, int index1)将选择更改为当前选择的集合,以及index0和index1之间的索引。voidclearSelection()将选择更改为空集。Objectclone()返回具有相同选择的此选择模型的克隆。protected voidfireValueChanged(boolean isAdjusting)通知听众我们已经结束了一系列调整。protected voidfireValueChanged(int firstIndex, int lastIndex)通知ListSelectionListeners该选择的值,在闭区间firstIndex,lastIndex,已经改变。protected voidfireValueChanged(int firstIndex, int lastIndex, boolean isAdjusting)intgetAnchorSelectionIndex()从最近调用setSelectionInterval(),addSelectionInterval()或removeSelectionInterval()返回第一个索引参数。intgetLeadSelectionIndex()将第二个索引参数从最近的调用返回给setSelectionInterval(),addSelectionInterval()或removeSelectionInterval()。<T extends EventListener>
T[]getListeners(Class<T> listenerType)返回此模型上当前注册为FooListener的所有对象的数组。ListSelectionListener[]getListSelectionListeners()返回在此DefaultListSelectionModel上注册的所有列表选择侦听器的数组。intgetMaxSelectionIndex()返回上一个选定的索引,如果选择为空,则返回-1。intgetMinSelectionIndex()返回第一个选择的索引,如果选择为空,则返回-1。intgetSelectionMode()返回当前选择模式。booleangetValueIsAdjusting()如果选择正在进行一系列更改,则返回true。voidinsertIndexInterval(int index, int length, boolean before)在索引之前/之后插入长度索引。booleanisLeadAnchorNotificationEnabled()返回值为leadAnchorNotificationEnabled标志。booleanisSelectedIndex(int index)如果选择了指定的索引,则返回true。booleanisSelectionEmpty()如果没有选择索引,则返回true。voidmoveLeadSelectionIndex(int leadIndex)设置引导选择索引,保持所有选择值不变。voidremoveIndexInterval(int index0, int index1)从选择模型中删除间隔index0,index1(包括)中的索引。voidremoveListSelectionListener(ListSelectionListener l)从列表中删除一个侦听器,每次发生更改时都会通知该侦听器。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属性,其指示即将到来的选择更改是否应被视为单个更改的一部分。StringtoString()返回显示和标识此对象属性的字符串。
-
-
-
字段详细信息
-
listenerList
protected EventListenerList listenerList
听众列表。
-
leadAnchorNotificationEnabled
protected boolean leadAnchorNotificationEnabled
是否启用牵头锚通知。
-
-
方法详细信息
-
getMinSelectionIndex
public int getMinSelectionIndex()
返回第一个选择的索引,如果选择为空,则返回-1。- Specified by:
-
getMinSelectionIndex在接口ListSelectionModel - 结果
- 第一个选择的索引,如果选择为空,则为-1。
-
getMaxSelectionIndex
public int getMaxSelectionIndex()
返回上一个选定的索引,如果选择为空,则返回-1。- Specified by:
-
getMaxSelectionIndex在接口ListSelectionModel - 结果
- 最后选择的索引,如果选择为空,则为-1。
-
getValueIsAdjusting
public boolean getValueIsAdjusting()
如果选择正在进行一系列更改,则返回true。- Specified by:
-
getValueIsAdjusting在接口ListSelectionModel - 结果
- 如果选择正在进行一系列更改,则为true
- 另请参见:
-
ListSelectionModel.setValueIsAdjusting(boolean)
-
getSelectionMode
public int getSelectionMode()
返回当前选择模式。- Specified by:
-
getSelectionMode在接口ListSelectionModel - 结果
- 当前选择模式
- 另请参见:
-
ListSelectionModel.setSelectionMode(int)
-
setSelectionMode
public void setSelectionMode(int selectionMode)
设置选择模式。 以下列表描述了接受的选择模式:-
ListSelectionModel.SINGLE_SELECTION- 一次只能选择一个列表索引。 在这种模式下,setSelectionInterval和addSelectionInterval是等效的,都将当前选择替换为由第二个参数(“lead”)表示的索引。 -
ListSelectionModel.SINGLE_INTERVAL_SELECTION- 一次只能选择一个连续的间隔。 在此模式下,addSelectionInterval行为类似于setSelectionInterval(替换当前选择),除非给定的间隔与现有选择紧密相邻或重叠,因此可用于增加它。 -
ListSelectionModel.MULTIPLE_INTERVAL_SELECTION- 在这种模式下,没有什么限制可以选择。
- Specified by:
-
setSelectionMode在接口ListSelectionModel - 参数
-
selectionMode- 选择模式 - 异常
-
IllegalArgumentException- 如果选择模式不是允许的选择模式之一 - 另请参见:
-
ListSelectionModel.getSelectionMode()
-
-
isSelectedIndex
public boolean isSelectedIndex(int index)
如果选择了指定的索引,则返回true。- Specified by:
-
isSelectedIndex在接口ListSelectionModel - 参数
-
index- 一个索引 - 结果
-
如果选择了指定的索引,则为
true
-
isSelectionEmpty
public boolean isSelectionEmpty()
如果没有选择索引,则返回true。- Specified by:
-
isSelectionEmpty在接口ListSelectionModel - 结果
-
true如果没有选择任何索引。
-
addListSelectionListener
public void addListSelectionListener(ListSelectionListener l)
将侦听器添加到每次发生更改选择时通知的列表。- Specified by:
-
addListSelectionListener在接口ListSelectionModel - 参数
-
l- ListSelectionListener - 另请参见:
-
ListSelectionModel.removeListSelectionListener(javax.swing.event.ListSelectionListener),ListSelectionModel.setSelectionInterval(int, int),ListSelectionModel.addSelectionInterval(int, int),ListSelectionModel.removeSelectionInterval(int, int),ListSelectionModel.clearSelection(),ListSelectionModel.insertIndexInterval(int, int, boolean),ListSelectionModel.removeIndexInterval(int, int)
-
removeListSelectionListener
public void removeListSelectionListener(ListSelectionListener l)
从列表中删除一个侦听器,每次发生更改时都会通知该侦听器。- Specified by:
-
removeListSelectionListener在接口ListSelectionModel - 参数
-
l- ListSelectionListener - 另请参见:
-
ListSelectionModel.addListSelectionListener(javax.swing.event.ListSelectionListener)
-
getListSelectionListeners
public ListSelectionListener[] getListSelectionListeners()
返回在此DefaultListSelectionModel上注册的所有列表选择侦听器的数组。- 结果
-
所有这个模型的
ListSelectionListeners或一个空数组,如果没有列表选择侦听器当前注册 - 从以下版本开始:
- 1.4
- 另请参见:
-
addListSelectionListener(javax.swing.event.ListSelectionListener),removeListSelectionListener(javax.swing.event.ListSelectionListener)
-
fireValueChanged
protected void fireValueChanged(boolean isAdjusting)
通知听众我们已经结束了一系列调整。- 参数
-
isAdjusting- 如果这是一系列调整的最终变化,isAdjustingtrue
-
fireValueChanged
protected void fireValueChanged(int firstIndex, int lastIndex)通知ListSelectionListeners该选择的值,在闭区间firstIndex,lastIndex,已经改变。- 参数
-
firstIndex- 间隔中的第一个索引 -
lastIndex- 间隔中的最后一个索引
-
fireValueChanged
protected void fireValueChanged(int firstIndex, int lastIndex, boolean isAdjusting)- 参数
-
firstIndex- 间隔中的第一个索引 -
lastIndex- 间隔中的最后一个索引 -
isAdjusting- 如果这是一系列调整的最终更改,isAdjustingtrue - 另请参见:
-
EventListenerList
-
getListeners
public <T extends EventListener> T[] getListeners(Class<T> listenerType)
返回此模型当前注册为FooListener的所有对象的数组。FooListener使用addFooListener方法进行注册。您可以使用类文字指定
listenerType参数,如FooListener.class。 例如,您可以使用以下代码查询DefaultListSelectionModel实例m的列表选择侦听器:ListSelectionListener[] lsls = (ListSelectionListener[])(m.getListeners(ListSelectionListener.class));如果没有这样的侦听器存在,这个方法返回一个空数组。- 参数类型
-
T- 正在请求类型的EventListener类 - 参数
-
listenerType- 所请求的听众的类型; 此参数应指定从java.util.EventListener下降的接口 - 结果
-
在该模型上注册为
FooListener的所有对象的数组,如果没有添加此类侦听器,则为空数组 - 异常
-
ClassCastException- 如果listenerType未指定实现java.util.EventListener的类或接口 - 从以下版本开始:
- 1.3
- 另请参见:
-
getListSelectionListeners()
-
setLeadAnchorNotificationEnabled
public void setLeadAnchorNotificationEnabled(boolean flag)
设置leadAnchorNotificationEnabled标志的值。- 参数
-
flag-布尔值leadAnchorNotificationEnabled - 另请参见:
-
isLeadAnchorNotificationEnabled()
-
isLeadAnchorNotificationEnabled
public boolean isLeadAnchorNotificationEnabled()
返回leadAnchorNotificationEnabled标志的值。 当leadAnchorNotificationEnabled为真时,模型将生成具有边界的通知事件,其中包含所有选择的更改以及leadAnchorNotificationEnabled和锚索引的更改。 将标志设置为false会导致事件范围变窄,以便仅包含自上次更改以来已被选择或取消选中的元素。 无论哪种方式,模型都会在内部继续保持领先和固定变量。 默认值为true。注意:可以更改引脚或锚点,而不更改选择。 通知这些更改通常很重要,例如需要在视图中更新新的线索或锚点时。 因此,当更改默认值时请注意。
- 结果
-
的值为
leadAnchorNotificationEnabled旗帜 - 另请参见:
-
setLeadAnchorNotificationEnabled(boolean)
-
clearSelection
public void clearSelection()
将选择更改为空集。 如果这表示对当前选择的更改,则通知每个ListSelectionListener。
-
setSelectionInterval
public void setSelectionInterval(int index0, int index1)将选择更改为index0和index1含)。index0不必小于或等于index1。在
SINGLE_SELECTION选择模式下,仅使用第二个索引。如果这表示对当前选择的更改,则通知每个
ListSelectionListener更改。如果任一索引为
-1,此方法不执行任何操作并返回。 否则,如果任一索引小于-1,则抛出一个IndexOutOfBoundsException。- Specified by:
-
setSelectionInterval在接口ListSelectionModel - 参数
-
index0- 间隔的一端。 -
index1- 间隔的另一端 - 异常
-
IndexOutOfBoundsException- 如果任一索引小于-1(并且索引都不是-1) - 另请参见:
-
addListSelectionListener(javax.swing.event.ListSelectionListener)
-
addSelectionInterval
public void addSelectionInterval(int index0, int index1)将选择更改为当前选择的index0并且包含index0和index1之间的索引。在
SINGLE_SELECTION选择模式下,这相当于调用setSelectionInterval,只使用第二个索引。 在SINGLE_INTERVAL_SELECTION选择模式中,该方法的行为类似于setSelectionInterval,除非给定的间隔与现有选择紧密相邻或重叠,因此可以用于增长它。如果这表示对当前选择的更改,则通知每个
ListSelectionListener更改。 请注意,index0不必小于或等于index1。如果任一索引为
-1,此方法不执行任何操作并返回。 否则,如果任一索引小于-1,则抛出一个IndexOutOfBoundsException。- Specified by:
-
addSelectionInterval在接口ListSelectionModel - 参数
-
index0- 间隔的一端。 -
index1- 间隔的另一端 - 异常
-
IndexOutOfBoundsException- 如果任一指数小于-1(且-1指数为-1) - 另请参见:
-
addListSelectionListener(javax.swing.event.ListSelectionListener),setSelectionInterval(int, int)
-
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。- Specified by:
-
removeSelectionInterval在接口ListSelectionModel - 参数
-
index0- 间隔的一端 -
index1- 间隔的另一端 - 异常
-
IndexOutOfBoundsException- 如果任一指数小于-1(既不是指数是-1) - 另请参见:
-
addListSelectionListener(javax.swing.event.ListSelectionListener)
-
insertIndexInterval
public void insertIndexInterval(int index, int length, boolean before)在索引之前/之后插入长度索引。 如果索引处的值本身被选择,并且选择模式不是SINGLE_SELECTION,则将所有新插入的项目设置为选定的。 否则不选择它们。 该方法通常被称为将选择模型与数据模型中的相应变化同步。- Specified by:
-
insertIndexInterval在接口ListSelectionModel - 参数
-
index- 间隔的开始 -
length- 间隔的长度 -
before- 如果true,间隔插入index之前,否则,间隔后插入index
-
removeIndexInterval
public void removeIndexInterval(int index0, int index1)从选择模型中删除间隔index0,index1(包括)中的索引。 这通常被称为将选择模型宽度与数据模型中的相应变化同步。 注意(一如既往)index0不需要<= index1。- Specified by:
-
removeIndexInterval在接口ListSelectionModel - 参数
-
index0- 间隔的开始 -
index1- 间隔的结束
-
setValueIsAdjusting
public void setValueIsAdjusting(boolean isAdjusting)
设置valueIsAdjusting属性,其指示即将到来的选择更改是否应被视为单个更改的一部分。 此属性的值用于初始化生成的ListSelectionEvents的valueIsAdjusting属性。例如,如果响应于用户拖动来更新选择,则当拖动启动时,该属性可以设置为
true,并且在拖动完成时将其设置为false。 在拖动期间,监听器接收valueIsAdjusting属性设置为true。 在拖动结束时,当更改完成时,监听器将收到一个值设置为false的事件。 如果希望仅在更改完成后更新,则侦听器可以使用此模式。将此属性设置为
true开始一系列将被视为单个更改的一部分的更改。 当属性更改为false,会发送一个事件,表征整个选择更改(如果有的话),事件的valueIsAdjusting属性设置为false。- Specified by:
-
setValueIsAdjusting在接口ListSelectionModel - 参数
-
isAdjusting- 该属性的新值 - 另请参见:
-
ListSelectionModel.getValueIsAdjusting(),ListSelectionEvent.getValueIsAdjusting()
-
clone
public Object clone() throws CloneNotSupportedException
返回具有相同选择的此选择模型的克隆。listenerLists不重复。- 重写:
-
clone在Object - 结果
- 这个实例的一个克隆。
- 异常
-
CloneNotSupportedException- 如果选择模型不同时(a)实现可clone接口和(b)定义clone方法。 - 另请参见:
-
Cloneable
-
getAnchorSelectionIndex
public int getAnchorSelectionIndex()
从最近调用setSelectionInterval(),addSelectionInterval()或removeSelectionInterval()返回第一个索引参数。 最近的index0被认为是“锚”,最近的index1被认为是“引导”。 一些界面特别显示这些索引,例如Windows95显示带有虚线黄色轮廓的引导索引。
-
getLeadSelectionIndex
public int getLeadSelectionIndex()
将第二个索引参数从最近的调用返回给setSelectionInterval(),addSelectionInterval()或removeSelectionInterval()。
-
setAnchorSelectionIndex
public void setAnchorSelectionIndex(int anchorIndex)
设置锚选择索引,使所有选择值保持不变。 如果leadAnchorNotificationEnabled为true,请发送覆盖旧锚点和新锚点的通知。- Specified by:
-
setAnchorSelectionIndex在接口ListSelectionModel - 参数
-
anchorIndex- 锚选择索引 - 另请参见:
-
getAnchorSelectionIndex(),setLeadSelectionIndex(int)
-
moveLeadSelectionIndex
public void moveLeadSelectionIndex(int leadIndex)
设置引导选择索引,保持所有选择值不变。 如果leadAnchorNotificationEnabled为true,请发送覆盖旧的和新的引线单元的通知。- 参数
-
leadIndex- 新的铅选择指数 - 从以下版本开始:
- 1.5
- 另请参见:
-
setAnchorSelectionIndex(int),setLeadSelectionIndex(int),getLeadSelectionIndex()
-
setLeadSelectionIndex
public void setLeadSelectionIndex(int leadIndex)
设置引导选择索引,确保锚和新引线之间的值全部被选中或全部被取消选择。 如果选择了锚索引处的值,首先清除范围[anchor,oldLeadIndex]中的所有值,然后选择[anchor,newLeadIndex]范围内的所有值,其中oldLeadIndex是旧的leadIndex,newLeadIndex是新的一。如果没有选择锚索引的值,则在旧范围内反向选择值并取消选择新值中的值,做同样的事情。
为此更改生成单个事件并通知所有侦听器。 为了在此事件中产生最小限制,请在单次传递中执行操作; 这样,广播的ListSelectionEvent里的第一个和最后一个索引将会引用由于这个方法而实际改变了值的单元格。 如果相反,这个操作是在两个步骤中完成的,那么对选择状态的影响将是相同的,但是会产生两个事件,并且改变的值周围的界限将会更宽,包括首先被清除以后被设置的单元格。
该方法可以在UI类的
mouseDragged方法中使用来扩展选择。- Specified by:
-
setLeadSelectionIndex在接口ListSelectionModel - 参数
-
leadIndex- 主导选择指数 - 另请参见:
-
getLeadSelectionIndex(),setAnchorSelectionIndex(int)
-
-