E
- the type of the elements of this modelpublic class DefaultListModel<E> extends AbstractListModel<E>
This class loosely implements the java.util.Vector
API, in that it implements the 1.1.x version of java.util.Vector
, has no collection class support, and notifies the ListDataListener
s when changes occur. Presently it delegates to a Vector
, in a future release it will be a real Collection implementation.
Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans™ has been added to the java.beans
package. Please see XMLEncoder
.
listenerList
public DefaultListModel()
public int getSize()
Returns the number of components in this list.
This method is identical to size
, which implements the List
interface defined in the 1.2 Collections framework. This method exists in conjunction with setSize
so that size
is identifiable as a JavaBean property.
size()
public E getElementAt(int index)
Returns the component at the specified index.
Note: Although this method is not deprecated, the preferred method to use is get(int), which implements the List interface defined in the 1.2 Collections framework.
index
- an index into this listArrayIndexOutOfBoundsException
- if the index
is negative or greater than the current size of this listget(int)
public void copyInto(Object[] anArray)
Copies the components of this list into the specified array. The array must be big enough to hold all the objects in this list, else an IndexOutOfBoundsException
is thrown.
anArray
- the array into which the components get copiedVector.copyInto(Object[])
public void trimToSize()
Trims the capacity of this list to be the list's current size.
Vector.trimToSize()
public void ensureCapacity(int minCapacity)
Increases the capacity of this list, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument.
minCapacity
- the desired minimum capacityVector.ensureCapacity(int)
public void setSize(int newSize)
Sets the size of this list.
newSize
- the new size of this listVector.setSize(int)
public int capacity()
Returns the current capacity of this list.
Vector.capacity()
public int size()
Returns the number of components in this list.
Vector.size()
public boolean isEmpty()
Tests whether this list has any components.
true
if and only if this list has no components, that is, its size is zero; false
otherwiseVector.isEmpty()
public Enumeration<E> elements()
Returns an enumeration of the components of this list.
Vector.elements()
public boolean contains(Object elem)
Tests whether the specified object is a component in this list.
elem
- an objecttrue
if the specified object is the same as a component in this listVector.contains(Object)
public int indexOf(Object elem)
Searches for the first occurrence of elem
.
elem
- an object-1
if the object is not foundVector.indexOf(Object)
public int indexOf(Object elem, int index)
Searches for the first occurrence of elem
, beginning the search at index
.
elem
- an desired componentindex
- the index from which to begin searchingelem
is found after index
; returns -1
if the elem
is not found in the listVector.indexOf(Object,int)
public int lastIndexOf(Object elem)
Returns the index of the last occurrence of elem
.
elem
- the desired componentelem
in the list; returns -1
if the object is not foundVector.lastIndexOf(Object)
public int lastIndexOf(Object elem, int index)
Searches backwards for elem
, starting from the specified index, and returns an index to it.
elem
- the desired componentindex
- the index to start searching fromelem
in this list at position less than index
; returns -1
if the object is not foundVector.lastIndexOf(Object,int)
public E elementAt(int index)
Returns the component at the specified index. Throws an ArrayIndexOutOfBoundsException
if the index is negative or not less than the size of the list.
Note: Although this method is not deprecated, the preferred method to use is get(int), which implements the List interface defined in the 1.2 Collections framework.
index
- an index into this listget(int)
, Vector.elementAt(int)
public E firstElement()
Returns the first component of this list. Throws a NoSuchElementException
if this vector has no components.
Vector.firstElement()
public E lastElement()
Returns the last component of the list. Throws a NoSuchElementException
if this vector has no components.
Vector.lastElement()
public void setElementAt(E element, int index)
Sets the component at the specified index
of this list to be the specified element. The previous component at that position is discarded.
Throws an ArrayIndexOutOfBoundsException
if the index is invalid.
Note: Although this method is not deprecated, the preferred method to use is set(int,Object), which implements the List interface defined in the 1.2 Collections framework.
element
- what the component is to be set toindex
- the specified indexset(int,Object)
, Vector.setElementAt(Object,int)
public void removeElementAt(int index)
Deletes the component at the specified index.
Throws an ArrayIndexOutOfBoundsException
if the index is invalid.
Note: Although this method is not deprecated, the preferred method to use is remove(int), which implements the List interface defined in the 1.2 Collections framework.
index
- the index of the object to removeremove(int)
, Vector.removeElementAt(int)
public void insertElementAt(E element, int index)
Inserts the specified element as a component in this list at the specified index
.
Throws an ArrayIndexOutOfBoundsException
if the index is invalid.
Note: Although this method is not deprecated, the preferred method to use is add(int,Object), which implements the List interface defined in the 1.2 Collections framework.
element
- the component to insertindex
- where to insert the new componentArrayIndexOutOfBoundsException
- if the index was invalidadd(int,Object)
, Vector.insertElementAt(Object,int)
public void addElement(E element)
Adds the specified component to the end of this list.
element
- the component to be addedVector.addElement(Object)
public boolean removeElement(Object obj)
Removes the first (lowest-indexed) occurrence of the argument from this list.
obj
- the component to be removedtrue
if the argument was a component of this list; false
otherwiseVector.removeElement(Object)
public void removeAllElements()
Removes all components from this list and sets its size to zero.
Note: Although this method is not deprecated, the preferred method to use is clear, which implements the List interface defined in the 1.2 Collections framework.
clear()
, Vector.removeAllElements()
public String toString()
Returns a string that displays and identifies this object's properties.
public Object[] toArray()
Returns an array containing all of the elements in this list in the correct order.
Vector.toArray()
public E get(int index)
Returns the element at the specified position in this list.
Throws an ArrayIndexOutOfBoundsException
if the index is out of range (index < 0 || index >= size()
).
index
- index of element to returnpublic E set(int index, E element)
Replaces the element at the specified position in this list with the specified element.
Throws an ArrayIndexOutOfBoundsException
if the index is out of range (index < 0 || index >= size()
).
index
- index of element to replaceelement
- element to be stored at the specified positionpublic void add(int index, E element)
Inserts the specified element at the specified position in this list.
Throws an ArrayIndexOutOfBoundsException
if the index is out of range (index < 0 || index > size()
).
index
- index at which the specified element is to be insertedelement
- element to be insertedpublic E remove(int index)
Removes the element at the specified position in this list. Returns the element that was removed from the list.
Throws an ArrayIndexOutOfBoundsException
if the index is out of range (index < 0 || index >= size()
).
index
- the index of the element to removedpublic void clear()
Removes all of the elements from this list. The list will be empty after this call returns (unless it throws an exception).
public void removeRange(int fromIndex, int toIndex)
Deletes the components at the specified range of indexes. The removal is inclusive, so specifying a range of (1,5) removes the component at index 1 and the component at index 5, as well as all components in between.
Throws an ArrayIndexOutOfBoundsException
if the index was invalid. Throws an IllegalArgumentException
if fromIndex > toIndex
.
fromIndex
- the index of the lower end of the rangetoIndex
- the index of the upper end of the rangeremove(int)
© 1993–2017, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.