- java.lang.Object
-
- javax.swing.AbstractListModel<E>
-
- javax.swing.DefaultListModel<E>
-
- 参数类型
-
E- 该模型的元素的类型
- All Implemented Interfaces:
-
Serializable,ListModel<E>
public class DefaultListModel<E> extends AbstractListModel<E>
该类松散地实现了java.util.VectorAPI,因为它实现了1.1.x版本的java.util.Vector,没有收集类支持,并在发生更改时通知ListDataListener。 目前它代表一个Vector,在将来的版本中,它将是一个真正的Collection实现。警告:此类的序列化对象与将来的Swing版本不兼容。 当前的序列化支持适用于运行相同版本的Swing的应用程序之间的短期存储或RMI。 从1.4开始,支持所有JavaBeans的长期存储已被添加到
java.beans包中。 请参阅XMLEncoder。- 从以下版本开始:
- 1.2
- 另请参见:
- Serialized Form
-
-
Field Summary
-
Fields inherited from class javax.swing.AbstractListModel
listenerList
-
-
构造方法摘要
构造方法 Constructor 描述 DefaultListModel()
-
方法摘要
所有方法 接口方法 具体的方法 Modifier and Type 方法 描述 voidadd(int index, E element)在此列表中的指定位置插入指定的元素。voidaddElement(E element)将指定的组件添加到此列表的末尾。intcapacity()返回此列表的当前容量。voidclear()从列表中删除所有元素。booleancontains(Object elem)测试指定的对象是否是此列表中的组件。voidcopyInto(Object[] anArray)将此列表的组件复制到指定的数组中。EelementAt(int index)返回指定索引处的组件。Enumeration<E>elements()返回此列表的组件的枚举。voidensureCapacity(int minCapacity)如有必要,增加此列表的容量,以确保它至少能够容纳最小容量参数指定的组件数。EfirstElement()返回此列表的第一个组件。Eget(int index)返回此列表中指定位置的元素。EgetElementAt(int index)返回指定索引处的组件。intgetSize()返回此列表中的组件数。intindexOf(Object elem)搜索首次发生elem。intindexOf(Object elem, int index)搜索第一次发生elem,开始搜索index。voidinsertElementAt(E element, int index)在指定的index中将指定的元素作为该列表中的组件插入。booleanisEmpty()测试此列表是否包含任何组件。ElastElement()返回列表的最后一个组件。intlastIndexOf(Object elem)返回最后一次出现的索引elem。intlastIndexOf(Object elem, int index)向后搜索elem,从指定的索引开始,并返回一个索引。Eremove(int index)删除该列表中指定位置的元素。voidremoveAllElements()从此列表中删除所有组件,并将其大小设置为零。booleanremoveElement(Object obj)从此列表中删除参数的第一个(最低索引)出现次数。voidremoveElementAt(int index)删除指定索引处的组件。voidremoveRange(int fromIndex, int toIndex)删除指定范围的索引中的组件。Eset(int index, E element)用指定的元素替换此列表中指定位置的元素。voidsetElementAt(E element, int index)将此列表中指定的index的组件设置为指定的元素。voidsetSize(int newSize)设置此列表的大小。intsize()返回此列表中的组件数。Object[]toArray()以正确的顺序返回包含此列表中所有元素的数组。StringtoString()返回显示和标识此对象属性的字符串。voidtrimToSize()将此列表的容量修改为列表的当前大小。-
Methods inherited from class javax.swing.AbstractListModel
addListDataListener, fireContentsChanged, fireIntervalAdded, fireIntervalRemoved, getListDataListeners, getListeners, removeListDataListener
-
-
-
-
方法详细信息
-
getSize
public int getSize()
返回此列表中的组件数。该方法与
size相同,它实现了1.2 Collections框架中定义的List接口。 该方法与setSize一起存在,以便可以size属性识别为size。- 结果
- 该列表中的组件数
- 另请参见:
-
size()
-
getElementAt
public E getElementAt(int index)
返回指定索引处的组件。Note: Although this method is not deprecated, the preferred method to use is
get(int), which implements theListinterface defined in the 1.2 Collections framework.- 参数
-
index- 此列表中的索引 - 结果
- 指定索引处的组件
- 异常
-
ArrayIndexOutOfBoundsException- 如果index为负数或大于此列表的当前大小 - 另请参见:
-
get(int)
-
copyInto
public void copyInto(Object[] anArray)
将此列表的组件复制到指定的数组中。 该数组必须足够大以容纳此列表中的所有对象,否则抛出一个IndexOutOfBoundsException。- 参数
-
anArray- 组件被复制到的阵列 - 另请参见:
-
Vector.copyInto(Object[])
-
trimToSize
public void trimToSize()
将此列表的容量修改为列表的当前大小。- 另请参见:
-
Vector.trimToSize()
-
ensureCapacity
public void ensureCapacity(int minCapacity)
如有必要,增加此列表的容量,以确保它至少能够容纳最小容量参数指定的组件数。- 参数
-
minCapacity- 所需的最小容量 - 另请参见:
-
Vector.ensureCapacity(int)
-
setSize
public void setSize(int newSize)
设置此列表的大小。- 参数
-
newSize- 这个列表的新大小 - 另请参见:
-
Vector.setSize(int)
-
capacity
public int capacity()
返回此列表的当前容量。- 结果
- 当前的容量
- 另请参见:
-
Vector.capacity()
-
size
public int size()
返回此列表中的组件数。- 结果
- 该列表中的组件数
- 另请参见:
-
Vector.size()
-
isEmpty
public boolean isEmpty()
测试此列表是否包含任何组件。- 结果
-
true当且仅当此列表没有组件时,即其大小为零; 否则为false - 另请参见:
-
Vector.isEmpty()
-
elements
public Enumeration<E> elements()
返回此列表的组件的枚举。- 结果
- 列出该列表的组件
- 另请参见:
-
Vector.elements()
-
contains
public boolean contains(Object elem)
测试指定的对象是否是此列表中的组件。- 参数
-
elem- 一个对象 - 结果
-
如果指定的对象与此列表中的组件相同,
true - 另请参见:
-
Vector.contains(Object)
-
indexOf
public int indexOf(Object elem)
搜索首次发生elem。- 参数
-
elem- 一个对象 - 结果
-
该列表中第一次出现参数的索引;
如果没有找到该对象,则返回
-1 - 另请参见:
-
Vector.indexOf(Object)
-
indexOf
public int indexOf(Object elem, int index)
搜索第一次发生elem,开始搜索index。- 参数
-
elem- 所需组件 -
index- 开始搜索的索引 - 结果
-
首次发现
index指数elem后index; 返回-1如果elem没有在列表中找到 - 另请参见:
-
Vector.indexOf(Object,int)
-
lastIndexOf
public int lastIndexOf(Object elem)
返回上一次发生的索引elem。- 参数
-
elem- 所需组件 - 结果
-
最近发生的指数为
elem; 如果找不到该对象,则返回-1 - 另请参见:
-
Vector.lastIndexOf(Object)
-
lastIndexOf
public int lastIndexOf(Object elem, int index)
向后搜索elem,从指定的索引开始,并返回一个索引。- 参数
-
elem- 所需组件 -
index- 开始搜索的索引 - 结果
-
该列表中最近发生的
elem的指数在位置小于index; 如果找不到该对象,则返回-1 - 另请参见:
-
Vector.lastIndexOf(Object,int)
-
elementAt
public E elementAt(int index)
返回指定索引处的组件。 如果索引为负数或不小于列表的大小,则抛出一个ArrayIndexOutOfBoundsException。Note: Although this method is not deprecated, the preferred method to use is
get(int), which implements theListinterface defined in the 1.2 Collections framework.- 参数
-
index- 此列表中的索引 - 结果
- 指定索引处的组件
- 另请参见:
-
get(int),Vector.elementAt(int)
-
firstElement
public E firstElement()
返回此列表的第一个组件。 如果此向量没有组件,则抛出一个NoSuchElementException。- 结果
- 这个列表的第一个组件
- 另请参见:
-
Vector.firstElement()
-
lastElement
public E lastElement()
返回列表的最后一个组件。 如果此矢量没有组件,则抛出一个NoSuchElementException。- 结果
- 列表的最后一个组件
- 另请参见:
-
Vector.lastElement()
-
setElementAt
public void setElementAt(E element, int index)
将此列表中指定的index的组件设置为指定的元素。 该位置的上一个组件被丢弃。如果索引无效,则抛出一个
ArrayIndexOutOfBoundsException。Note: Although this method is not deprecated, the preferred method to use is
set(int,Object), which implements theListinterface defined in the 1.2 Collections framework.- 参数
-
element- 要设置的组件 -
index- 指定的索引 - 另请参见:
-
set(int,Object),Vector.setElementAt(Object,int)
-
removeElementAt
public void removeElementAt(int index)
删除指定索引处的组件。如果索引无效,则抛出一个
ArrayIndexOutOfBoundsException。Note: Although this method is not deprecated, the preferred method to use is
remove(int), which implements theListinterface defined in the 1.2 Collections framework.- 参数
-
index- 要删除的对象的索引 - 另请参见:
-
remove(int),Vector.removeElementAt(int)
-
insertElementAt
public void insertElementAt(E element, int index)
在指定的index中将指定的元素作为该列表中的组件插入。如果索引无效,则抛出一个
ArrayIndexOutOfBoundsException。Note: Although this method is not deprecated, the preferred method to use is
add(int,Object), which implements theListinterface defined in the 1.2 Collections framework.- 参数
-
element- 要插入的组件 -
index- 在哪里插入新组件 - 异常
-
ArrayIndexOutOfBoundsException- 如果索引无效 - 另请参见:
-
add(int,Object),Vector.insertElementAt(Object,int)
-
addElement
public void addElement(E element)
将指定的组件添加到此列表的末尾。- 参数
-
element- 要添加的组件 - 另请参见:
-
Vector.addElement(Object)
-
removeElement
public boolean removeElement(Object obj)
从此列表中删除参数的第一个(最低索引)出现次数。- 参数
-
obj- 要删除的组件 - 结果
-
true如果参数是此列表的组件; 否则为false - 另请参见:
-
Vector.removeElement(Object)
-
removeAllElements
public void removeAllElements()
从此列表中删除所有组件,并将其大小设置为零。Note: Although this method is not deprecated, the preferred method to use is
clear, which implements theListinterface defined in the 1.2 Collections framework.- 另请参见:
-
clear(),Vector.removeAllElements()
-
toArray
public Object[] toArray()
以正确的顺序返回包含此列表中所有元素的数组。- 结果
- 一个包含列表元素的数组
- 另请参见:
-
Vector.toArray()
-
get
public E get(int index)
返回此列表中指定位置的元素。抛出一个
ArrayIndexOutOfBoundsException如果索引超出范围(index < 0 || index >= size())。- 参数
-
index- 要返回的元素的索引 - 结果
- 该列表中指定位置的元素
-
set
public E set(int index, E element)
用指定的元素替换此列表中指定位置的元素。抛出一个
ArrayIndexOutOfBoundsException如果索引超出范围(index < 0 || index >= size())。- 参数
-
index- 要替换的元素的索引 -
element- 要存储在指定位置的元素 - 结果
- 该元素以前在指定的位置
-
add
public void add(int index, E element)在此列表中的指定位置插入指定的元素。抛出一个
ArrayIndexOutOfBoundsException如果索引超出范围(index < 0 || index > size())。- 参数
-
index- 要在其中插入指定元素的索引 -
element- 要插入的元素
-
remove
public E remove(int index)
删除该列表中指定位置的元素。 返回从列表中删除的元素。抛出一个
ArrayIndexOutOfBoundsException如果索引超出范围(index < 0 || index >= size())。- 参数
-
index- 要删除的元素的索引 - 结果
- 该元素以前在指定的位置
-
clear
public void clear()
从列表中删除所有元素。 此调用返回后,列表将为空(除非引发异常)。
-
removeRange
public void removeRange(int fromIndex, int toIndex)删除指定范围的索引中的组件。 删除是包含的,因此指定(1,5)的范围将删除索引1处的组件和索引5处的组件以及其间的所有组件。如果索引无效,则抛出一个
ArrayIndexOutOfBoundsException。 抛出一个IllegalArgumentException如果fromIndex > toIndex。- 参数
-
fromIndex- 范围下限的索引 -
toIndex- 范围上限的索引 - 另请参见:
-
remove(int)
-
-