模块 java.desktop

类 DefaultListSelectionModel

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

public class DefaultListSelectionModel extends Object implements ListSelectionModel , Cloneable , Serializable
列表选择的默认数据模型。

Warning: 此类的序列化对象将与未来的 Swing 版本不兼容。当前的序列化支持适用于运行相同版本 Swing 的应用程序之间的短期存储或 RMI。从 1.4 开始,对所有 JavaBeans 的长期存储的支持已添加到 java.beans 包中。请参阅 XMLEncoder

自从:
1.2
参见:
  • 字段详细信息

    • listenerList

      protected EventListenerList  listenerList
      听众名单。
    • leadAnchorNotificationEnabled

      protected boolean leadAnchorNotificationEnabled
      是否启用主锚通知。
  • 构造方法详细信息

    • DefaultListSelectionModel

      public DefaultListSelectionModel()
      构造一个 DefaultListSelectionModel
  • 方法详情

    • getMinSelectionIndex

      public int getMinSelectionIndex()
      返回第一个选定的索引,如果选择为空,则返回 -1。
      指定者:
      getMinSelectionIndex 在接口 ListSelectionModel
      返回:
      第一个选择的索引或 -1 如果选择为空。
    • getMaxSelectionIndex

      public int getMaxSelectionIndex()
      如果选择为空,则返回最后选择的索引或 -1。
      指定者:
      getMaxSelectionIndex 在接口 ListSelectionModel
      返回:
      最后选择的索引或 -1 如果选择为空。
    • getValueIsAdjusting

      public boolean getValueIsAdjusting()
      如果选择正在进行一系列更改,则返回 true
      指定者:
      getValueIsAdjusting 在接口 ListSelectionModel
      返回:
      如果选择正在进行一系列更改,则为真
      参见:
    • getSelectionMode

      public int getSelectionMode()
      返回当前选择模式。
      指定者:
      getSelectionMode 在接口 ListSelectionModel
      返回:
      当前选择模式
      参见:
    • setSelectionMode

      public void setSelectionMode(int selectionMode)
      设置选择模式。以下列表描述了可接受的选择模式:
      • ListSelectionModel.SINGLE_SELECTION - 一次只能选择一个列表索引。在这种模式下,setSelectionIntervaladdSelectionInterval 是等效的,都用第二个参数(“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

      public void addListSelectionListener(ListSelectionListener  l)
      将一个监听添加到列表中,每次发生选择更改时都会通知该列表。
      指定者:
      addListSelectionListener 在接口 ListSelectionModel
      参数:
      l - ListSelectionListener
      参见:
    • removeListSelectionListener

      public void removeListSelectionListener(ListSelectionListener  l)
      从每次发生选择更改时通知的列表中删除一个监听。
      指定者:
      removeListSelectionListener 在接口 ListSelectionModel
      参数:
      l - ListSelectionListener
      参见:
    • getListSelectionListeners

      public ListSelectionListener [] getListSelectionListeners()
      返回在此 DefaultListSelectionModel 上注册的所有列表选择监听器的数组。
      返回:
      此模型的所有 ListSelectionListener s 或空数组(如果当前未注册列表选择监听器)
      自从:
      1.4
      参见:
    • fireValueChanged

      protected void fireValueChanged(boolean isAdjusting)
      通知听众我们已经结束了一系列调整。
      参数:
      isAdjusting - 如果这是一系列调整中的最后一个更改,则为真
    • fireValueChanged

      protected void fireValueChanged(int firstIndex, int lastIndex)
      通知 ListSelectionListeners 在闭区间 firstIndexlastIndex 中选择的值已更改。
      参数:
      firstIndex - 区间中的第一个索引
      lastIndex - 间隔中的最后一个索引
    • fireValueChanged

      protected void fireValueChanged(int firstIndex, int lastIndex, boolean isAdjusting)
      通知 ListSelectionListeners 在闭区间 firstIndexlastIndex 中选择的值已经改变,如果这是一系列调整中的最后一个改变。
      参数:
      firstIndex - 区间中的第一个索引
      lastIndex - 间隔中的最后一个索引
      isAdjusting - 如果这是一系列调整中的最后一个更改,则为真
      参见:
    • 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
      参见:
    • setLeadAnchorNotificationEnabled

      public void setLeadAnchorNotificationEnabled(boolean flag)
      设置 leadAnchorNotificationEnabled 标志的值。
      参数:
      flag - leadAnchorNotificationEnabled 的boolean
      参见:
    • isLeadAnchorNotificationEnabled

      public boolean isLeadAnchorNotificationEnabled()
      返回 leadAnchorNotificationEnabled 标志的值。当 leadAnchorNotificationEnabled 为真时,模型会生成通知事件,其范围涵盖对选择的所有更改以及对引导和锚索引的更改。将标志设置为 false 会导致事件范围缩小,仅包括自上次更改以来已选择或取消选择的元素。无论哪种方式,模型都会继续在内部维护引导变量和锚定变量。默认为真。

      注意:可以在不更改选择的情况下更改引导或锚点。这些更改的通知通常很重要,例如何时需要在视图中更新新的线索或锚点。因此,更改默认值时应谨慎。

      返回:
      leadAnchorNotificationEnabled 标志的值
      参见:
    • clearSelection

      public void clearSelection()
      将选择更改为空集。如果这表示对当前选择的更改,则通知每个 ListSelectionListener。
      指定者:
      clearSelection 在接口 ListSelectionModel
      参见:
    • setSelectionInterval

      public void setSelectionInterval(int index0, int index1)
      将选择更改为介于 index0index1 之间(含)。 index0 不必小于或等于 index1

      SINGLE_SELECTION选择模式下,只使用第二个索引。

      如果这表示对当前选择的更改,则每个 ListSelectionListener 都会收到更改通知。

      如果任一索引为 -1 ,则此方法不执行任何操作并无一例外地返回。否则,如果任一索引小于 -1,则抛出 IndexOutOfBoundsException

      指定者:
      setSelectionInterval 在接口 ListSelectionModel
      参数:
      index0 - 区间的一端。
      index1 - 区间的另一端
      抛出:
      IndexOutOfBoundsException - 如果任一索引小于 -1 (并且两个索引都不是 -1
      参见:
    • addSelectionInterval

      public void addSelectionInterval(int index0, int index1)
      将选择更改为当前选择的集合并集以及 index0index1 之间的索引(含)。

      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)
      将选择更改为当前选择的集合差异以及 index0index1 之间的索引(含)。 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)
      在索引之前/之后插入长度索引。如果 index 处的值本身被选中并且选择模式不是 SINGLE_SELECTION,则将所有新插入的项目设置为选中。否则不要选择它们。通常调用此方法以将选择模型与数据模型中的相应更改同步。
      指定者:
      insertIndexInterval 在接口 ListSelectionModel
      参数:
      index - 间隔的开始
      length - 间隔的长度
      before - 如果是 true ,则在 index 之前插入间隔,否则,在 index 之后插入间隔
    • removeIndexInterval

      public void removeIndexInterval(int index0, int index1)
      从选择模型中删除区间 index0,index1(含)中的索引。这通常被调用以将选择模型宽度与数据模型中的相应更改同步。请注意(一如既往)index0 不必 <= index1。
      指定者:
      removeIndexInterval 在接口 ListSelectionModel
      参数:
      index0 - 间隔的开始
      index1 - 区间结束
    • setValueIsAdjusting

      public void setValueIsAdjusting(boolean isAdjusting)
      设置 valueIsAdjusting 属性,指示即将发生的选择更改是否应被视为单个更改的一部分。此属性的值用于初始化生成的 ListSelectionEventvalueIsAdjusting 属性。

      例如,如果选择正在更新以响应用户拖动,则可以在拖动开始时将此属性设置为 true,在拖动完成时设置为 false。在拖动期间,监听器会收到 valueIsAdjusting 属性设置为 true 的事件。在拖动结束时,当更改完成时,监听器会收到一个值设置为 false 的事件。如果监听器希望仅在更改完成后才更新,则可以使用此模式。

      将此属性设置为 true 开始一系列更改,这些更改被视为单个更改的一部分。当属性改回 false 时,将发送一个事件来表征整个选择更改(如果有的话),并将事件的 valueIsAdjusting 属性设置为 false

      指定者:
      setValueIsAdjusting 在接口 ListSelectionModel
      参数:
      isAdjusting - 属性的新值
      参见:
    • toString

      public String  toString()
      返回显示和标识此对象属性的字符串。
      重写:
      toString 在类 Object
      返回:
      此对象的 String 表示
    • clone

      public Object  clone() throws CloneNotSupportedException
      返回具有相同选择的此选择模型的克隆。 listenerLists 不重复。
      重写:
      clone 在类 Object
      返回:
      此实例的克隆。
      抛出:
      CloneNotSupportedException - 如果选择模型没有同时 (a) 实现 Cloneable 接口和 (b) 定义 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 为真,则发送重写新旧锚点的通知。
      指定者:
      setAnchorSelectionIndex 在接口 ListSelectionModel
      参数:
      anchorIndex - 锚点选择索引
      参见:
    • moveLeadSelectionIndex

      public void moveLeadSelectionIndex(int leadIndex)
      设置主导选择索引,保持所有选择值不变。如果leadAnchorNotificationEnabled为true,则发送一条通知,涵盖旧的和新的主导单元格.
      参数:
      leadIndex - 新的引导选择索引
      自从:
      1.5
      参见:
    • setLeadSelectionIndex

      public void setLeadSelectionIndex(int leadIndex)
      设置潜在客户选择索引,确保锚点和新潜在客户之间的值全部选中或全部取消选择。如果选择了anchor index处的值,首先清空[anchor, oldLeadIndex]范围内的所有值,然后选择[anchor, newLeadIndex]范围内的所有值,其中oldLeadIndex为旧leadIndex,newLeadIndex为新leadIndex .

      如果未选择锚索引处的值,则反向选择旧范围中的值并取消选择新范围中的值。

      为此更改生成单个事件并通知所有监听。为了在此事件中生成最小边界,请一次性完成操作;这样,广播的 ListSelectionEvent 中的第一个和最后一个索引将引用由于此方法而实际更改值的单元格。相反,如果此操作分两步完成,则对选择状态的影响将相同,但会生成两个事件,并且更改值周围的范围会更宽,包括先清除后才设置的单元格.

      此方法可用于 UI 类的 mouseDragged 方法以扩展选择。

      指定者:
      setLeadSelectionIndex 在接口 ListSelectionModel
      参数:
      leadIndex - 引导选择索引
      参见: