- java.lang.Object
-
- javax.swing.AbstractListModel<E>
-
- javax.swing.DefaultListModel<E>
-
- Type Parameters:
E
- the type of the elements of this model
- All Implemented Interfaces:
- Serializable, ListModel<E>
public class DefaultListModel<E> extends AbstractListModel<E>
This class loosely implements thejava.util.Vector
API, in that it implements the 1.1.x version ofjava.util.Vector
, has no collection class support, and notifies theListDataListener
s when changes occur. Presently it delegates to aVector
, 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 JavaBeansTM has been added to the
java.beans
package. Please seeXMLEncoder
.
-
-
Field Summary
-
Fields inherited from class javax.swing.AbstractListModel
listenerList
-
-
Constructor Summary
Constructors Constructor and Description DefaultListModel()
-
Method Summary
Methods Modifier and Type Method and Description void
add(int index, E element)
Inserts the specified element at the specified position in this list.void
addElement(E element)
Adds the specified component to the end of this list.int
capacity()
Returns the current capacity of this list.void
clear()
Removes all of the elements from this list.boolean
contains(Object elem)
Tests whether the specified object is a component in this list.void
copyInto(Object[] anArray)
Copies the components of this list into the specified array.E
elementAt(int index)
Returns the component at the specified index.Enumeration<E>
elements()
Returns an enumeration of the components of this list.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.E
firstElement()
Returns the first component of this list.E
get(int index)
Returns the element at the specified position in this list.E
getElementAt(int index)
Returns the component at the specified index.int
getSize()
Returns the number of components in this list.int
indexOf(Object elem)
Searches for the first occurrence ofelem
.int
indexOf(Object elem, int index)
Searches for the first occurrence ofelem
, beginning the search atindex
.void
insertElementAt(E element, int index)
Inserts the specified element as a component in this list at the specifiedindex
.boolean
isEmpty()
Tests whether this list has any components.E
lastElement()
Returns the last component of the list.int
lastIndexOf(Object elem)
Returns the index of the last occurrence ofelem
.int
lastIndexOf(Object elem, int index)
Searches backwards forelem
, starting from the specified index, and returns an index to it.E
remove(int index)
Removes the element at the specified position in this list.void
removeAllElements()
Removes all components from this list and sets its size to zero.boolean
removeElement(Object obj)
Removes the first (lowest-indexed) occurrence of the argument from this list.void
removeElementAt(int index)
Deletes the component at the specified index.void
removeRange(int fromIndex, int toIndex)
Deletes the components at the specified range of indexes.E
set(int index, E element)
Replaces the element at the specified position in this list with the specified element.void
setElementAt(E element, int index)
Sets the component at the specifiedindex
of this list to be the specified element.void
setSize(int newSize)
Sets the size of this list.int
size()
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.String
toString()
Returns a string that displays and identifies this object's properties.void
trimToSize()
Trims 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
-
-
-
-
Method Detail
-
getSize
public int getSize()
Returns the number of components in this list.This method is identical to
size
, which implements theList
interface defined in the 1.2 Collections framework. This method exists in conjunction withsetSize
so thatsize
is identifiable as a JavaBean property.- Returns:
- the number of components in this list
- See Also:
size()
-
getElementAt
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 theList
interface defined in the 1.2 Collections framework.- Parameters:
index
- an index into this list- Returns:
- the component at the specified index
- Throws:
ArrayIndexOutOfBoundsException
- if theindex
is negative or greater than the current size of this list- See Also:
get(int)
-
copyInto
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 anIndexOutOfBoundsException
is thrown.- Parameters:
anArray
- the array into which the components get copied- See Also:
Vector.copyInto(Object[])
-
trimToSize
public void trimToSize()
Trims the capacity of this list to be the list's current size.- See Also:
Vector.trimToSize()
-
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:
Vector.ensureCapacity(int)
-
setSize
public void setSize(int newSize)
Sets the size of this list.- Parameters:
newSize
- the new size of this list- See Also:
Vector.setSize(int)
-
capacity
public int capacity()
Returns the current capacity of this list.- Returns:
- the current capacity
- See Also:
Vector.capacity()
-
size
public int size()
Returns the number of components in this list.- Returns:
- the number of components in this list
- See Also:
Vector.size()
-
isEmpty
public boolean isEmpty()
Tests whether this list has any components.- Returns:
true
if and only if this list has no components, that is, its size is zero;false
otherwise- See Also:
Vector.isEmpty()
-
elements
public Enumeration<E> elements()
Returns an enumeration of the components of this list.- Returns:
- an enumeration of the components of this list
- See Also:
Vector.elements()
-
contains
public boolean contains(Object elem)
Tests whether the specified object is a component in this list.- Parameters:
elem
- an object- Returns:
true
if the specified object is the same as a component in this list- See Also:
Vector.contains(Object)
-
indexOf
public int indexOf(Object elem)
Searches for the first occurrence ofelem
.- Parameters:
elem
- an object- Returns:
- 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)
-
indexOf
public int indexOf(Object elem, int index)
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
elem
is found afterindex
; returns-1
if theelem
is not found in the list - See Also:
Vector.indexOf(Object,int)
-
lastIndexOf
public int lastIndexOf(Object elem)
Returns the index of the last occurrence ofelem
.- Parameters:
elem
- the desired component- Returns:
- the index of the last occurrence of
elem
in the list; returns-1
if the object is not found - See Also:
Vector.lastIndexOf(Object)
-
lastIndexOf
public int lastIndexOf(Object elem, int index)
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
elem
in this list at position less thanindex
; returns-1
if the object is not found - See Also:
Vector.lastIndexOf(Object,int)
-
elementAt
public E elementAt(int index)
Returns the component at the specified index. Throws anArrayIndexOutOfBoundsException
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 theList
interface defined in the 1.2 Collections framework.- Parameters:
index
- an index into this list- Returns:
- the component at the specified index
- See Also:
get(int)
,Vector.elementAt(int)
-
firstElement
public E firstElement()
Returns the first component of this list. Throws aNoSuchElementException
if this vector has no components.- Returns:
- the first component of this list
- See Also:
Vector.firstElement()
-
lastElement
public E lastElement()
Returns the last component of the list. Throws aNoSuchElementException
if this vector has no components.- Returns:
- the last component of the list
- See Also:
Vector.lastElement()
-
setElementAt
public void setElementAt(E element, int index)
Sets the component at the specifiedindex
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 theList
interface defined in the 1.2 Collections framework.- Parameters:
element
- what the component is to be set toindex
- the specified index- See Also:
set(int,Object)
,Vector.setElementAt(Object,int)
-
removeElementAt
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 theList
interface defined in the 1.2 Collections framework.- Parameters:
index
- the index of the object to remove- See Also:
remove(int)
,Vector.removeElementAt(int)
-
insertElementAt
public void insertElementAt(E element, int index)
Inserts the specified element as a component in this list at the specifiedindex
.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 theList
interface defined in the 1.2 Collections framework.- Parameters:
element
- the component to insertindex
- where to insert the new component- Throws:
ArrayIndexOutOfBoundsException
- if the index was invalid- See Also:
add(int,Object)
,Vector.insertElementAt(Object,int)
-
addElement
public void addElement(E element)
Adds the specified component to the end of this list.- Parameters:
element
- the component to be added- See Also:
Vector.addElement(Object)
-
removeElement
public boolean removeElement(Object obj)
Removes the first (lowest-indexed) occurrence of the argument from this list.- Parameters:
obj
- the component to be removed- Returns:
true
if the argument was a component of this list;false
otherwise- See Also:
Vector.removeElement(Object)
-
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 theList
interface defined in the 1.2 Collections framework.- See Also:
clear()
,Vector.removeAllElements()
-
toString
public String toString()
Returns a string that displays and identifies this object's properties.
-
toArray
public Object[] 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:
Vector.toArray()
-
get
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()
).- Parameters:
index
- index of element to return
-
set
public 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()
).- Parameters:
index
- index of element to replaceelement
- element to be stored at the specified position- Returns:
- the element previously at the specified position
-
add
public 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()
).- Parameters:
index
- index at which the specified element is to be insertedelement
- element to be inserted
-
remove
public 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()
).- Parameters:
index
- the index of the element to removed- Returns:
- the element previously at the specified position
-
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
ArrayIndexOutOfBoundsException
if the index was invalid. Throws anIllegalArgumentException
iffromIndex > toIndex
.- Parameters:
fromIndex
- the index of the lower end of the rangetoIndex
- the index of the upper end of the range- See Also:
remove(int)
-
-
Traduction non disponible
Les API Java ne sont pas encore traduites en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Version en cache
05/11/2024 15:47:27 Cette version de la page est en cache (à la date du 05/11/2024 15:47:27) afin d'accélérer le traitement. Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la dernère version de la page.Document créé le 30/08/2006, dernière modification le 04/03/2020
Source du document imprimé : https://www.gaudry.be/java-api-rf-javax/swing/DefaultListModel.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.