JavaTM 2 Platform
Standard Ed. 5.0

javax.swing
类 DefaultListSelectionModel

java.lang.Object
  继承者 javax.swing.DefaultListSelectionModel
所有已实现的接口:
Serializable, Cloneable, ListSelectionModel

public class DefaultListSelectionModel
extends Object
implements ListSelectionModel, Cloneable, Serializable

列表选择的默认数据模型。

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

另请参见:
ListSelectionModel

字段摘要
protected  boolean leadAnchorNotificationEnabled
           
protected  EventListenerList listenerList
           
 
从接口 javax.swing.ListSelectionModel 继承的字段
MULTIPLE_INTERVAL_SELECTION, SINGLE_INTERVAL_SELECTION, SINGLE_SELECTION
 
构造方法摘要
DefaultListSelectionModel()
           
 
方法摘要
 void addListSelectionListener(ListSelectionListener l)
          将侦听器添加到每次在选择发生更改时都得到通知的列表。
 void addSelectionInterval(int index0, int index1)
          将该选择更改为当前选择与 index0 和 index1(包括)之间索引的并集。
 void clearSelection()
          将该选择更改为空集。
 Object clone()
          返回此选择模型的具有相同选择的克隆。
protected  void fireValueChanged(boolean isAdjusting)
          通知侦听器已经结束了一系列的调整。
protected  void fireValueChanged(int firstIndex, int lastIndex)
          通知 ListSelectionListener 选择的值已发生更改,该值在 firstIndexlastIndex 组成的闭区间内。
protected  void fireValueChanged(int firstIndex, int lastIndex, boolean isAdjusting)
           
 int getAnchorSelectionIndex()
          从对 setSelectionInterval()、addSelectionInterval() 或 removeSelectionInterval() 的最近调用中返回第一个索引参数。
 int getLeadSelectionIndex()
          从对 setSelectionInterval()、addSelectionInterval() 或 removeSelectionInterval() 的最近调用中返回第二个索引参数。
<T extends EventListener>
T[]
getListeners(Class<T> listenerType)
          返回当前已在此模型上注册为 FooListener 的所有对象组成的数组。
 ListSelectionListener[] getListSelectionListeners()
          返回已在此 DefaultListSelectionModel 上注册的所有列表选择侦听器组成的数组。
 int getMaxSelectionIndex()
          返回最后选择的索引,如果选择为空,则返回 -1。
 int getMinSelectionIndex()
          返回第一个选择的索引,如果选择为空,则返回 -1。
 int getSelectionMode()
          返回选择模式。
 boolean getValueIsAdjusting()
          如果该值在进行一系列更改,则返回 true。
 void insertIndexInterval(int index, int length, boolean before)
          从 index 之前/之后开始插入 length 个索引。
 boolean isLeadAnchorNotificationEnabled()
          返回 leadAnchorNotificationEnabled 标志的值。
 boolean isSelectedIndex(int index)
          如果选择了指定的索引,则返回 true。
 boolean isSelectionEmpty()
          如果没有选择索引,则返回 true。
 void moveLeadSelectionIndex(int leadIndex)
          设置 lead 选择索引,并不作更改地保留所有选择值。
 void removeIndexInterval(int index0, int index1)
          从选择模型中移除 index0,index1 区间中(包括两端点)的索引。
 void removeListSelectionListener(ListSelectionListener l)
          将侦听器从每次在选择发生更改时都得到通知的列表中移除。
 void removeSelectionInterval(int index0, int index1)
          将该选择更改为当前选择与 index0 和 index1(包括)之间索引的差集。
 void setAnchorSelectionIndex(int anchorIndex)
          设置 anchor 选择索引,并不作更改地保留所有选择值。
 void setLeadAnchorNotificationEnabled(boolean flag)
          设置 leadAnchorNotificationEnabled 标志的值。
 void setLeadSelectionIndex(int leadIndex)
          设置 lead 选择索引,确保 anchor 和新 lead 之间的值要么全部被选定,要么全部被取消选定。
 void setSelectionInterval(int index0, int index1)
          更改要位于 index0 和 index1(包括)之间的选择。
 void setSelectionMode(int selectionMode)
          设置选择模式。
 void setValueIsAdjusting(boolean isAdjusting)
          如果将来对该模型的值的更改被视为单个事件,则此属性为 true。
 String toString()
          返回显示并标识此对象的属性的字符串。
 
从类 java.lang.Object 继承的方法
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

字段详细信息

listenerList

protected EventListenerList listenerList

leadAnchorNotificationEnabled

protected boolean leadAnchorNotificationEnabled
构造方法详细信息

DefaultListSelectionModel

public DefaultListSelectionModel()
方法详细信息

getMinSelectionIndex

public int getMinSelectionIndex()
从接口 ListSelectionModel 复制的描述
返回第一个选择的索引,如果选择为空,则返回 -1。

指定者:
接口 ListSelectionModel 中的 getMinSelectionIndex

getMaxSelectionIndex

public int getMaxSelectionIndex()
从接口 ListSelectionModel 复制的描述
返回最后选择的索引,如果选择为空,则返回 -1。

指定者:
接口 ListSelectionModel 中的 getMaxSelectionIndex

getValueIsAdjusting

public boolean getValueIsAdjusting()
从接口 ListSelectionModel 复制的描述
如果该值在进行一系列更改,则返回 true。

指定者:
接口 ListSelectionModel 中的 getValueIsAdjusting
返回:
如果正在调整该值,则返回 true
另请参见:
ListSelectionModel.setValueIsAdjusting(boolean)

getSelectionMode

public int getSelectionMode()
返回选择模式。

指定者:
接口 ListSelectionModel 中的 getSelectionMode
返回:
以下这些值之一:
  • SINGLE_SELECTION
  • SINGLE_INTERVAL_SELECTION
  • MULTIPLE_INTERVAL_SELECTION
另请参见:
getSelectionMode()

setSelectionMode

public void setSelectionMode(int selectionMode)
设置选择模式。默认值是 MULTIPLE_INTERVAL_SELECTION。

指定者:
接口 ListSelectionModel 中的 setSelectionMode
参数:
selectionMode - 以下三个值之一:
  • SINGLE_SELECTION
  • SINGLE_INTERVAL_SELECTION
  • MULTIPLE_INTERVAL_SELECTION
抛出:
IllegalArgumentException - 如果 selectionMode 不是上面显示的合法值之一
另请参见:
setSelectionMode(int)

isSelectedIndex

public boolean isSelectedIndex(int index)
从接口 ListSelectionModel 复制的描述
如果选择了指定的索引,则返回 true。

指定者:
接口 ListSelectionModel 中的 isSelectedIndex

isSelectionEmpty

public boolean isSelectionEmpty()
从接口 ListSelectionModel 复制的描述
如果没有选择索引,则返回 true。

指定者:
接口 ListSelectionModel 中的 isSelectionEmpty

addListSelectionListener

public void addListSelectionListener(ListSelectionListener l)
从接口 ListSelectionModel 复制的描述
将侦听器添加到每次在选择发生更改时都得到通知的列表。

指定者:
接口 ListSelectionModel 中的 addListSelectionListener
参数:
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)
从接口 ListSelectionModel 复制的描述
将侦听器从每次在选择发生更改时都得到通知的列表中移除。

指定者:
接口 ListSelectionModel 中的 removeListSelectionListener
参数:
l - ListSelectionListener
另请参见:
ListSelectionModel.addListSelectionListener(javax.swing.event.ListSelectionListener)

getListSelectionListeners

public ListSelectionListener[] getListSelectionListeners()
返回已在此 DefaultListSelectionModel 上注册的所有列表选择侦听器组成的数组。

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

fireValueChanged

protected void fireValueChanged(boolean isAdjusting)
通知侦听器已经结束了一系列的调整。


fireValueChanged

protected void fireValueChanged(int firstIndex,
                                int lastIndex)
通知 ListSelectionListener 选择的值已发生更改,该值在 firstIndexlastIndex 组成的闭区间内。


fireValueChanged

protected void fireValueChanged(int firstIndex,
                                int lastIndex,
                                boolean isAdjusting)
参数:
firstIndex - 区间中的第一个索引
lastIndex - 区间中的最后一个索引
isAdjusting - 如果这是一系列调整中的最终更改,则该参数为 true
另请参见:
EventListenerList

getListeners

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

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

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

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

setLeadAnchorNotificationEnabled

public void setLeadAnchorNotificationEnabled(boolean flag)
设置 leadAnchorNotificationEnabled 标志的值。

另请参见:
isLeadAnchorNotificationEnabled()

isLeadAnchorNotificationEnabled

public boolean isLeadAnchorNotificationEnabled()
返回 leadAnchorNotificationEnabled 标志的值。当 leadAnchorNotificationEnabled 为 true 时,该模型生成具有限定范围的通知事件,该范围覆盖对选择所做的所有更改以及对 lead 和 anchor 索引所做的更改。将标志设置为 false 会导致事件的限定范围缩小,使其只包括那些已选择的或自上一次更改之后取消选择的元素。总之,该模型将继续内部维护 lead 和 anchor 变量。默认值为 true。

注:在不更改选择的情况下更改 lead 或 anchor 是有可能的。通知这些更改常常很重要,比如需要在视图中更新新的 lead 或 anchor 时。因此,在更改默认值时,一定要小心谨慎。

返回:
leadAnchorNotificationEnabled 标志的值
另请参见:
setLeadAnchorNotificationEnabled(boolean)

clearSelection

public void clearSelection()
从接口 ListSelectionModel 复制的描述
将该选择更改为空集。如果这表示对当前选择的更改,则通知每个 ListSelectionListener。

指定者:
接口 ListSelectionModel 中的 clearSelection
另请参见:
ListSelectionModel.addListSelectionListener(javax.swing.event.ListSelectionListener)

setSelectionInterval

public void setSelectionInterval(int index0,
                                 int index1)
从接口 ListSelectionModel 复制的描述
更改要位于 index0 和 index1(包括)之间的选择。如果这表示对当前选择的更改,则通知每个 ListSelectionListener。注意,index0 不必小于或等于 index1。

指定者:
接口 ListSelectionModel 中的 setSelectionInterval
参数:
index0 - 区间的一端。
index1 - 区间的另一端
另请参见:
ListSelectionModel.addListSelectionListener(javax.swing.event.ListSelectionListener)

addSelectionInterval

public void addSelectionInterval(int index0,
                                 int index1)
从接口 ListSelectionModel 复制的描述
将该选择更改为当前选择与 index0 和 index1(包括)之间索引的并集。如果这表示对当前选择的更改,则通知每个 ListSelectionListener。注意,index0 不必小于或等于 index1。

指定者:
接口 ListSelectionModel 中的 addSelectionInterval
参数:
index0 - 区间的一端。
index1 - 区间的另一端
另请参见:
ListSelectionModel.addListSelectionListener(javax.swing.event.ListSelectionListener)

removeSelectionInterval

public void removeSelectionInterval(int index0,
                                    int index1)
从接口 ListSelectionModel 复制的描述
将该选择更改为当前选择与 index0 和 index1(包括)之间索引的差集。如果这表示对当前选择的更改,则通知每个 ListSelectionListener。注意,index0 不必小于或等于 index1。

指定者:
接口 ListSelectionModel 中的 removeSelectionInterval
参数:
index0 - 区间的一端。
index1 - 区间的另一端
另请参见:
ListSelectionModel.addListSelectionListener(javax.swing.event.ListSelectionListener)

insertIndexInterval

public void insertIndexInterval(int index,
                                int length,
                                boolean before)
从 index 之前/之后开始插入 length 个索引。如果 index 处的值本身处于已选状态,并且选择模式不是 SINGLE_SELECTION,则将新插入的所有项都设置为已选状态。否则将它们都设置为未选择状态。通常调用此方法来同步选择模型与数据模型中的相应更改。

指定者:
接口 ListSelectionModel 中的 insertIndexInterval

removeIndexInterval

public void removeIndexInterval(int index0,
                                int index1)
从选择模型中移除 index0,index1 区间中(包括两端点)的索引。通常调用此方法来同步选择模型与数据模型中的相应更改。注意,同往常一样,index0 不必 <= index1。

指定者:
接口 ListSelectionModel 中的 removeIndexInterval

setValueIsAdjusting

public void setValueIsAdjusting(boolean isAdjusting)
从接口 ListSelectionModel 复制的描述
如果将来对该模型的值的更改被视为单个事件,则此属性为 true。例如,如果在更新该模型以响应一个用户的拖动,则 valueIsAdjusting 属性的值在发起该拖动操作时将被设置为 true,并在该拖动操作结束时被设置为 false。此属性允许侦听器仅在更改结束时进行更新,而不是始终处理所有的中间值。

指定者:
接口 ListSelectionModel 中的 setValueIsAdjusting
参数:
isAdjusting - 属性的新值。
另请参见:
ListSelectionModel.getValueIsAdjusting()

toString

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

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

clone

public Object clone()
             throws CloneNotSupportedException
返回此选择模型的具有相同选择的克隆。listenerList 没有被复制。

覆盖:
Object 中的 clone
返回:
此实例的一个克隆。
抛出:
CloneNotSupportedException - 如果选择模型没有 (a) 实现 Cloneable 接口且 (b) 定义一个 clone 方法。
另请参见:
Cloneable

getAnchorSelectionIndex

public int getAnchorSelectionIndex()
从接口 ListSelectionModel 复制的描述
从对 setSelectionInterval()、addSelectionInterval() 或 removeSelectionInterval() 的最近调用中返回第一个索引参数。最近的 index0 被认为是 "anchor",最近的 index1 被认为是 "lead"。一些界面特别地显示这些索引,例如,Windows95 显示带有黄色点式轮廓的 lead 索引。

指定者:
接口 ListSelectionModel 中的 getAnchorSelectionIndex
另请参见:
ListSelectionModel.getLeadSelectionIndex(), ListSelectionModel.setSelectionInterval(int, int), ListSelectionModel.addSelectionInterval(int, int)

getLeadSelectionIndex

public int getLeadSelectionIndex()
从接口 ListSelectionModel 复制的描述
从对 setSelectionInterval()、addSelectionInterval() 或 removeSelectionInterval() 的最近调用中返回第二个索引参数。

指定者:
接口 ListSelectionModel 中的 getLeadSelectionIndex
另请参见:
ListSelectionModel.getAnchorSelectionIndex(), ListSelectionModel.setSelectionInterval(int, int), ListSelectionModel.addSelectionInterval(int, int)

setAnchorSelectionIndex

public void setAnchorSelectionIndex(int anchorIndex)
设置 anchor 选择索引,并不作更改地保留所有选择值。如果 leadAnchorNotificationEnabled 为 true,则发送一个覆盖原有 anchor 单元格和新 anchor 单元格的通知。

指定者:
接口 ListSelectionModel 中的 setAnchorSelectionIndex
另请参见:
getAnchorSelectionIndex(), setLeadSelectionIndex(int)

moveLeadSelectionIndex

public void moveLeadSelectionIndex(int leadIndex)
设置 lead 选择索引,并不作更改地保留所有选择值。如果 leadAnchorNotificationEnabled 为 true,则发送一个覆盖原有 lead 单元格和新 lead 单元格的通知。

参数:
leadIndex - 新的 lead 选择索引
从以下版本开始:
1.5
另请参见:
setAnchorSelectionIndex(int), setLeadSelectionIndex(int), getLeadSelectionIndex()

setLeadSelectionIndex

public void setLeadSelectionIndex(int leadIndex)
设置 lead 选择索引,确保 anchor 和新 lead 之间的值要么全部被选定,要么全部被取消选定。如果 anchor 索引处的值被选定,则首先清除范围 [anchor, oldLeadIndex] 中的所有值,然后选择范围 [anchor, newLeadIndex] 中的所有值,其中 oldLeadIndex 是原有的 leadIndex,newLeadIndex 是新的 leadIndex。

如果 anchor 索引处的值未被选定,则反过来执行上述操作:选择原有范围中的值,并取消选择新范围中的值。

为此更改生成一个事件,并通知所有侦听器。为了在此事件中生成最小的限定范围,要一次完成该操作;那样广播的 ListSelectionEvent 中第一个和最后一个索引将指出由于此方法而实际更改了其值的单元格。如果分两步完成此操作,则选择状态上的效果将是相同的,但会生成两个事件,更改值的限定范围变得更宽,包括之前已清除但稍后又被设置的单元格。

可以在 UI 类的 mouseDragged 方法中使用此方法来扩展选择。

指定者:
接口 ListSelectionModel 中的 setLeadSelectionIndex
另请参见:
getLeadSelectionIndex(), setAnchorSelectionIndex(int)

JavaTM 2 Platform
Standard Ed. 5.0

提交错误或意见
有关更多的 API 参考资料和开发人员文档,请参阅 Java 2 SDK SE 开发人员文档。该文档包含更详细的、面向开发人员的描述,以及总体概述、术语定义、使用技巧和工作代码示例。

版权所有 2004 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策