Class DefaultListModel
- All Implemented Interfaces:
Serializable,ListModel
- See Also:
-
Field Summary
Fields inherited from class javax.swing.AbstractListModel
listenerList -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidInserts the specified element at the specified position in this list.voidaddElement(Object obj) Adds the specified component to the end of this list.intcapacity()Returns the current capacity of this list.voidclear()Removes all of the elements from this list.booleanTests whether the specified object is a component in this list.voidCopies the components of this list into the specified array.elementAt(int index) Returns the component at the specified index.elements()Returns an enumeration of the components of this list.voidensureCapacity(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.Returns the first component of this list.get(int index) Returns the element at the specified position in this list.getElementAt(int index) Returns the component at the specified index.intgetSize()Returns the number of components in this list.intSearches for the first occurrence ofelem.intSearches for the first occurrence ofelem, beginning the search atindex.voidinsertElementAt(Object obj, int index) Inserts the specified object as a component in this list at the specifiedindex.booleanisEmpty()Tests whether this list has any components.Returns the last component of the list.intlastIndexOf(Object elem) Returns the index of the last occurrence ofelem.intlastIndexOf(Object elem, int index) Searches backwards forelem, starting from the specified index, and returns an index to it.remove(int index) Removes the element at the specified position in this list.voidRemoves all components from this list and sets its size to zero.booleanremoveElement(Object obj) Removes the first (lowest-indexed) occurrence of the argument from this list.voidremoveElementAt(int index) Deletes the component at the specified index.voidremoveRange(int fromIndex, int toIndex) Deletes the components at the specified range of indexes.Replaces the element at the specified position in this list with the specified element.voidsetElementAt(Object obj, int index) Sets the component at the specifiedindexof this list to be the specified object.voidsetSize(int newSize) Sets the size of this list.intsize()Returns the number of components in this list.Object[]toArray()Returns an array containing all of the elements in this list in the correct order.toString()Returns a string that displays and identifies this object's properties.voidTrims the capacity of this list to be the list's current size.Methods inherited from class javax.swing.AbstractListModel
addListDataListener, fireContentsChanged, fireIntervalAdded, fireIntervalRemoved, getListDataListeners, getListeners, removeListDataListener
-
Constructor Details
-
DefaultListModel
-
DefaultListModel
public DefaultListModel()
-
-
Method Details
-
getSize
public int getSize()Returns the number of components in this list.This method is identical to
size, which implements theListinterface defined in the 1.2 Collections framework. This method exists in conjunction withsetSizeso thatsizeis identifiable as a JavaBean property.- Returns:
- the number of components in this list
- See Also:
-
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 theListinterface defined in the 1.2 Collections framework.- Parameters:
index- an index into this list- Returns:
- the component at the specified index
- Throws:
ArrayIndexOutOfBoundsException- if theindexis negative or greater than the current size of this list- See Also:
-
copyInto
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 anIndexOutOfBoundsExceptionis thrown.- Parameters:
anArray- the array into which the components get copied- See Also:
-
trimToSize
public void trimToSize()Trims the capacity of this list to be the list's current size.- See Also:
-
ensureCapacity
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- the desired minimum capacity- See Also:
-
setSize
public void setSize(int newSize) Sets the size of this list.- Parameters:
newSize- the new size of this list- See Also:
-
capacity
public int capacity()Returns the current capacity of this list.- Returns:
- the current capacity
- See Also:
-
size
public int size()Returns the number of components in this list.- Returns:
- the number of components in this list
- See Also:
-
isEmpty
public boolean isEmpty()Tests whether this list has any components.- Returns:
trueif and only if this list has no components, that is, its size is zero;falseotherwise- See Also:
-
elements
Returns an enumeration of the components of this list.- Returns:
- an enumeration of the components of this list
- See Also:
-
contains
Tests whether the specified object is a component in this list.- Parameters:
elem- an object- Returns:
trueif the specified object is the same as a component in this list- See Also:
-
indexOf
Searches for the first occurrence ofelem.- Parameters:
elem- an object- Returns:
- the index of the first occurrence of the argument in this
list; returns
-1if the object is not found - See Also:
-
indexOf
Searches for the first occurrence ofelem, beginning the search atindex.- Parameters:
elem- an desired componentindex- the index from which to begin searching- Returns:
- the index where the first occurrence of
elemis found afterindex; returns-1if theelemis not found in the list - See Also:
-
lastIndexOf
Returns the index of the last occurrence ofelem.- Parameters:
elem- the desired component- Returns:
- the index of the last occurrence of
elemin the list; returns-1if the object is not found - See Also:
-
lastIndexOf
Searches backwards forelem, starting from the specified index, and returns an index to it.- Parameters:
elem- the desired componentindex- the index to start searching from- Returns:
- the index of the last occurrence of the
elemin this list at position less thanindex; returns-1if the object is not found - See Also:
-
elementAt
Returns the component at the specified index. Throws anArrayIndexOutOfBoundsExceptionif 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 theListinterface defined in the 1.2 Collections framework.- Parameters:
index- an index into this list- Returns:
- the component at the specified index
- See Also:
-
firstElement
Returns the first component of this list. Throws aNoSuchElementExceptionif this vector has no components.- Returns:
- the first component of this list
- See Also:
-
lastElement
Returns the last component of the list. Throws aNoSuchElementExceptionif this vector has no components.- Returns:
- the last component of the list
- See Also:
-
setElementAt
Sets the component at the specifiedindexof this list to be the specified object. The previous component at that position is discarded.Throws an
ArrayIndexOutOfBoundsExceptionif the index is invalid.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.- Parameters:
obj- what the component is to be set toindex- the specified index- See Also:
-
removeElementAt
public void removeElementAt(int index) Deletes the component at the specified index.Throws an
ArrayIndexOutOfBoundsExceptionif the index is invalid.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.- Parameters:
index- the index of the object to remove- See Also:
-
insertElementAt
Inserts the specified object as a component in this list at the specifiedindex.Throws an
ArrayIndexOutOfBoundsExceptionif the index is invalid.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.- Parameters:
obj- the component to insertindex- where to insert the new component- Throws:
ArrayIndexOutOfBoundsException- if the index was invalid- See Also:
-
addElement
Adds the specified component to the end of this list.- Parameters:
obj- the component to be added- See Also:
-
removeElement
Removes the first (lowest-indexed) occurrence of the argument from this list.- Parameters:
obj- the component to be removed- Returns:
trueif the argument was a component of this list;falseotherwise- See Also:
-
removeAllElements
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 theListinterface defined in the 1.2 Collections framework.- See Also:
-
toString
Returns a string that displays and identifies this object's properties. -
toArray
Returns an array containing all of the elements in this list in the correct order.- Returns:
- an array containing the elements of the list
- See Also:
-
get
Returns the element at the specified position in this list.Throws an
ArrayIndexOutOfBoundsExceptionif the index is out of range (index < 0 || index >= size()).- Parameters:
index- index of element to return
-
set
Replaces the element at the specified position in this list with the specified element.Throws an
ArrayIndexOutOfBoundsExceptionif the index is out of range (index < 0 || index >= size()).- Parameters:
index- index of element to replaceelement- element to be stored at the specified position- Returns:
- the element previously at the specified position
-
add
Inserts the specified element at the specified position in this list.Throws an
ArrayIndexOutOfBoundsExceptionif the index is out of range (index < 0 || index > size()).- Parameters:
index- index at which the specified element is to be insertedelement- element to be inserted
-
remove
Removes the element at the specified position in this list. Returns the element that was removed from the list.Throws an
ArrayIndexOutOfBoundsExceptionif the index is out of range (index < 0 || index >= size()).- Parameters:
index- the index of the element to removed
-
clear
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). -
removeRange
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
ArrayIndexOutOfBoundsExceptionif the index was invalid. Throws anIllegalArgumentExceptioniffromIndex > toIndex.- Parameters:
fromIndex- the index of the lower end of the rangetoIndex- the index of the upper end of the range- See Also:
-