Top Description Fields Constructors Methods
javax.swing

public Class DefaultListModel<E>

extends AbstractListModel<E>
Class Inheritance
Known Direct Subclasses
javax.swing.text.html.OptionListModel
Annotations
@SuppressWarnings:serial
Type Parameters
<E>
the type of the elements of this model
Imports
java.util.Vector, .Collection, .Enumeration

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 ListDataListeners 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 java.beans.XMLEncoder.

Author
Hans Muller
Since
1.2

Field Summary

Modifier and TypeField and Description
private Vector<E>
Inherited from javax.swing.AbstractListModel:
listenerList

Constructor Summary

AccessConstructor and Description
public
DefaultListModel()

Constructs a DefaultListModel.

Method Summary

Modifier and TypeMethod and Description
public void
add(int
index at which the specified element is to be inserted
index
,
E
element to be inserted
element
)

Inserts the specified element at the specified position in this list.

public void
addAll(Collection<? extends E>
the collection which contains the elements to add
c
)

Adds all of the elements present in the collection to the list.

public void
addAll(int
index at which to insert the first element from the specified collection
index
,
Collection<? extends E>
the collection which contains the elements to add
c
)

Adds all of the elements present in the collection, starting from the specified index.

public void
addElement(E
the component to be added
element
)

Adds the specified component to the end of this list.

public int

Returns:

the current capacity
capacity
()

Returns the current capacity of this list.

public void
clear()

Removes all of the elements from this list.

public boolean

Returns:

true if the specified object is the same as a component in this list
contains
(Object
an object
elem
)

Tests whether the specified object is a component in this list.

public void
copyInto(Object[]
the array into which the components get copied
anArray
)

Copies the components of this list into the specified array.

public E

Returns:

the component at the specified index
elementAt
(int
an index into this list
index
)

Returns the component at the specified index.

public Enumeration<E>

Returns:

an enumeration of the components of this list
elements
()

Returns an enumeration of the components of this list.

public void
ensureCapacity(int
the desired minimum capacity
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.

public E

Returns:

the first component of this list
firstElement
()

Returns the first component of this list.

public E

Returns:

the element at the specified position in this list
get
(int
index of element to return
index
)

Returns the element at the specified position in this list.

public E

Returns:

the component at the specified index
getElementAt
(int
an index into this list
index
)

Implements javax.swing.ListModel.getElementAt.

Returns the component at the specified index.
public int

Returns:

the number of components in this list
getSize
()

Implements javax.swing.ListModel.getSize.

Returns the number of components in this list.
public int

Returns:

the index of the first occurrence of the argument in this list; returns -1 if the object is not found
indexOf
(Object
an object
elem
)

Searches for the first occurrence of elem.

public int

Returns:

the index where the first occurrence of elem is found after index; returns -1 if the elem is not found in the list
indexOf
(Object
the desired component
elem
,
int
the index from which to begin searching
index
)

Searches for the first occurrence of elem, beginning the search at index.

public void
insertElementAt(E
the component to insert
element
,
int
where to insert the new component
index
)

Inserts the specified element as a component in this list at the specified index.

public boolean

Returns:

true if and only if this list has no components, that is, its size is zero; false otherwise
isEmpty
()

Tests whether this list has any components.

public E

Returns:

the last component of the list
lastElement
()

Returns the last component of the list.

public int

Returns:

the index of the last occurrence of elem in the list; returns elem if the object is not found
lastIndexOf
(Object
the desired component
elem
)

Returns the index of the last occurrence of elem.

public int

Returns:

the index of the last occurrence of the elem in this list at position less than index; returns -1 if the object is not found
lastIndexOf
(Object
the desired component
elem
,
int
the index to start searching from
index
)

Searches backwards for elem, starting from the specified index, and returns an index to it.

public E

Returns:

the element previously at the specified position
remove
(int
the index of the element to removed
index
)

Removes the element at the specified position in this list.

public void
removeAllElements()

Removes all components from this list and sets its size to zero.

public boolean

Returns:

true if the argument was a component of this list; false otherwise
removeElement
(Object
the component to be removed
obj
)

Removes the first (lowest-indexed) occurrence of the argument from this list.

public void
removeElementAt(int
the index of the object to remove
index
)

Deletes the component at the specified index.

public void
removeRange(int
the index of the lower end of the range
fromIndex
,
int
the index of the upper end of the range
toIndex
)

Deletes the components at the specified range of indexes.

public E

Returns:

the element previously at the specified position
set
(int
index of element to replace
index
,
E
element to be stored at the specified position
element
)

Replaces the element at the specified position in this list with the specified element.

public void
setElementAt(E
what the component is to be set to
element
,
int
the specified index
index
)

Sets the component at the specified index of this list to be the specified element.

public void
setSize(int
the new size of this list
newSize
)

Sets the size of this list.

public int

Returns:

the number of components in this list
size
()

Returns the number of components in this list.

public Object[]

Returns:

an array containing the elements of the list
toArray
()

Returns an array containing all of the elements in this list in the correct order.

public String

Returns:

a String representation of this object
toString
()

Overrides java.lang.Object.toString.

Returns a string that displays and identifies this object's properties.
public void
trimToSize()

Trims the capacity of this list to be the list's current size.

Inherited from javax.swing.AbstractListModel:
addListDataListenerfireContentsChangedfireIntervalAddedfireIntervalRemovedgetListDataListenersgetListenersremoveListDataListener

Field Detail

delegateback to summary
private Vector<E> delegate

Constructor Detail

DefaultListModelback to summary
public DefaultListModel()

Constructs a DefaultListModel.

Method Detail

addback to summary
public void add(int index, E element)

Inserts the specified element at the specified position in this list.

Parameters
index:int

index at which the specified element is to be inserted

element:E

element to be inserted

Exceptions
ArrayIndexOutOfBoundsException:
if the index is out of range (index < 0 || index > size())
addAllback to summary
public void addAll(Collection<? extends E> c)

Adds all of the elements present in the collection to the list.

Parameters
c:Collection<? extends E>

the collection which contains the elements to add

Exceptions
NullPointerException:
if c is null
addAllback to summary
public void addAll(int index, Collection<? extends E> c)

Adds all of the elements present in the collection, starting from the specified index.

Parameters
index:int

index at which to insert the first element from the specified collection

c:Collection<? extends E>

the collection which contains the elements to add

Exceptions
ArrayIndexOutOfBoundsException:
if index does not fall within the range of number of elements currently held
NullPointerException:
if c is null
addElementback to summary
public void addElement(E element)

Adds the specified component to the end of this list.

Parameters
element:E

the component to be added

See Also
Vector#addElement(Object)
capacityback to summary
public int capacity()

Returns the current capacity of this list.

Returns:int

the current capacity

See Also
Vector#capacity()
clearback to summary
public void clear()

Removes all of the elements from this list. The list will be empty after this call returns (unless it throws an exception).

containsback to summary
public boolean contains(Object elem)

Tests whether the specified object is a component in this list.

Parameters
elem:Object

an object

Returns:boolean

true if the specified object is the same as a component in this list

See Also
Vector#contains(Object)
copyIntoback to summary
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.

Parameters
anArray:Object[]

the array into which the components get copied

See Also
Vector#copyInto(Object[])
elementAtback to summary
public E elementAt(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.

Parameters
index:int

an index into this list

Returns:E

the component at the specified index

Exceptions
ArrayIndexOutOfBoundsException:
if the index is negative or not less than the size of the list
See Also
get(int), Vector#elementAt(int)
elementsback to summary
public Enumeration<E> elements()

Returns an enumeration of the components of this list.

Returns:Enumeration<E>

an enumeration of the components of this list

See Also
Vector#elements()
ensureCapacityback to summary
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.

Parameters
minCapacity:int

the desired minimum capacity

See Also
Vector#ensureCapacity(int)
firstElementback to summary
public E firstElement()

Returns the first component of this list.

Returns:E

the first component of this list

Exceptions
NoSuchElementException:
if this vector has no components
See Also
Vector#firstElement()
getback to summary
public E get(int index)

Returns the element at the specified position in this list.

Parameters
index:int

index of element to return

Returns:E

the element at the specified position in this list

Exceptions
ArrayIndexOutOfBoundsException:
if the index is out of range (index < 0 || index >= size())
getElementAtback to summary
public E getElementAt(int index)

Implements javax.swing.ListModel.getElementAt.

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.

Parameters
index:int

an index into this list

Returns:E

the component at the specified index

Exceptions
ArrayIndexOutOfBoundsException:
if the index is negative or greater than the current size of this list
See Also
get(int)
getSizeback to summary
public int getSize()

Implements javax.swing.ListModel.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.

Returns:int

the number of components in this list

See Also
size()
indexOfback to summary
public int indexOf(Object elem)

Searches for the first occurrence of elem.

Parameters
elem:Object

an object

Returns:int

the index of the first occurrence of the argument in this list; returns -1 if the object is not found

See Also
Vector#indexOf(Object)
indexOfback to summary
public int indexOf(Object elem, int index)

Searches for the first occurrence of elem, beginning the search at index.

Parameters
elem:Object

the desired component

index:int

the index from which to begin searching

Returns:int

the index where the first occurrence of elem is found after index; returns -1 if the elem is not found in the list

See Also
Vector#indexOf(Object, int)
insertElementAtback to summary
public void insertElementAt(E element, int index)

Inserts the specified element as a component in this list at the specified index.

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.

Parameters
element:E

the component to insert

index:int

where to insert the new component

Exceptions
ArrayIndexOutOfBoundsException:
if the index was invalid
See Also
add(int, Object), Vector#insertElementAt(Object, int)
isEmptyback to summary
public boolean isEmpty()

Tests whether this list has any components.

Returns:boolean

true if and only if this list has no components, that is, its size is zero; false otherwise

See Also
Vector#isEmpty()
lastElementback to summary
public E lastElement()

Returns the last component of the list.

Returns:E

the last component of the list

Exceptions
NoSuchElementException:
if this vector has no components
See Also
Vector#lastElement()
lastIndexOfback to summary
public int lastIndexOf(Object elem)

Returns the index of the last occurrence of elem.

Parameters
elem:Object

the desired component

Returns:int

the index of the last occurrence of elem in the list; returns elem if the object is not found

See Also
Vector#lastIndexOf(Object)
lastIndexOfback to summary
public int lastIndexOf(Object elem, int index)

Searches backwards for elem, starting from the specified index, and returns an index to it.

Parameters
elem:Object

the desired component

index:int

the index to start searching from

Returns:int

the index of the last occurrence of the elem in this list at position less than index; returns -1 if the object is not found

See Also
Vector#lastIndexOf(Object, int)
removeback to summary
public E remove(int index)

Removes the element at the specified position in this list. Returns the element that was removed from the list

Parameters
index:int

the index of the element to removed

Returns:E

the element previously at the specified position

Exceptions
ArrayIndexOutOfBoundsException:
if the index is out of range (index < 0 || index >= size())
removeAllElementsback to summary
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.

See Also
clear(), Vector#removeAllElements()
removeElementback to summary
public boolean removeElement(Object obj)

Removes the first (lowest-indexed) occurrence of the argument from this list.

Parameters
obj:Object

the component to be removed

Returns:boolean

true if the argument was a component of this list; false otherwise

See Also
Vector#removeElement(Object)
removeElementAtback to summary
public void removeElementAt(int index)

Deletes the component at the specified index.

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.

Parameters
index:int

the index of the object to remove

Exceptions
ArrayIndexOutOfBoundsException:
if the index is invalid
See Also
remove(int), Vector#removeElementAt(int)
removeRangeback to summary
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.

Parameters
fromIndex:int

the index of the lower end of the range

toIndex:int

the index of the upper end of the range

Exceptions
ArrayIndexOutOfBoundsException:
if the index was invalid
IllegalArgumentException:
if fromIndex > toIndex
See Also
remove(int)
setback to summary
public E set(int index, E element)

Replaces the element at the specified position in this list with the specified element.

Parameters
index:int

index of element to replace

element:E

element to be stored at the specified position

Returns:E

the element previously at the specified position

Exceptions
ArrayIndexOutOfBoundsException:
if the index is out of range (index < 0 || index >= size())
setElementAtback to summary
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.

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.

Parameters
element:E

what the component is to be set to

index:int

the specified index

Exceptions
ArrayIndexOutOfBoundsException:
if the index is invalid
See Also
set(int, Object), Vector#setElementAt(Object, int)
setSizeback to summary
public void setSize(int newSize)

Sets the size of this list.

Parameters
newSize:int

the new size of this list

See Also
Vector#setSize(int)
sizeback to summary
public int size()

Returns the number of components in this list.

Returns:int

the number of components in this list

See Also
Vector#size()
toArrayback to summary
public Object[] toArray()

Returns an array containing all of the elements in this list in the correct order.

Returns:Object[]

an array containing the elements of the list

See Also
Vector#toArray()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a string that displays and identifies this object's properties.

Returns:String

a String representation of this object

trimToSizeback to summary
public void trimToSize()

Trims the capacity of this list to be the list's current size.

See Also
Vector#trimToSize()