Class 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>
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
.
- Since:
- 1.2
Field Summary
Fields declared in class javax.swing.AbstractListModel
listenerList
Constructor Summary
Constructor | Description |
---|---|
DefaultListModel() |
Constructs a DefaultListModel . |
Method Summary
Modifier and Type | Method | Description |
---|---|---|
void |
add |
Inserts the specified element at the specified position in this list. |
void |
addAll |
Adds all of the elements present in the collection, starting from the specified index. |
void |
addAll |
Adds all of the elements present in the collection to the list. |
void |
addElement |
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 |
Tests whether the specified object is a component in this list. |
void |
copyInto |
Copies the components of this list into the specified array. |
E |
elementAt |
Returns the component at the specified index. |
Enumeration<E> |
elements() |
Returns an enumeration of the components of this list. |
void |
ensureCapacity |
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 |
Returns the element at the specified position in this list. |
E |
getElementAt |
Returns the component at the specified index. |
int |
getSize() |
Returns the number of components in this list. |
int |
indexOf |
Searches for the first occurrence of elem . |
int |
indexOf |
Searches for the first occurrence of elem , beginning the search at index . |
void |
insertElementAt |
Inserts the specified element as a component in this list at the specified index . |
boolean |
isEmpty() |
Tests whether this list has any components. |
E |
lastElement() |
Returns the last component of the list. |
int |
lastIndexOf |
Returns the index of the last occurrence of elem . |
int |
lastIndexOf |
Searches backwards for elem , starting from the specified index, and returns an index to it. |
E |
remove |
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 |
Removes the first (lowest-indexed) occurrence of the argument from this list. |
void |
removeElementAt |
Deletes the component at the specified index. |
void |
removeRange |
Deletes the components at the specified range of indexes. |
E |
set |
Replaces the element at the specified position in this list with the specified element. |
void |
setElementAt |
Sets the component at the specified index of this list to be the specified element. |
void |
setSize |
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 declared in class javax.swing.AbstractListModel
addListDataListener, fireContentsChanged, fireIntervalAdded, fireIntervalRemoved, getListDataListeners, getListeners, removeListDataListener
Constructor Details
DefaultListModel
public DefaultListModel()
DefaultListModel
.Method Details
getSize
public int getSize()
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:
- the number of components in this list
- See Also:
getElementAt
public E getElementAt(int index)
Note: Although this method is not deprecated, the preferred method to use isget(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:
copyInto
public void copyInto(Object[] anArray)
IndexOutOfBoundsException
is thrown.- Parameters:
-
anArray
- the array into which the components get copied - See Also:
trimToSize
public void trimToSize()
- See Also:
ensureCapacity
public void ensureCapacity(int minCapacity)
- Parameters:
-
minCapacity
- the desired minimum capacity - See Also:
setSize
public void setSize(int newSize)
- Parameters:
-
newSize
- the new size of this list - See Also:
capacity
public int capacity()
- Returns:
- the current capacity
- See Also:
size
public int size()
- Returns:
- the number of components in this list
- See Also:
isEmpty
public boolean isEmpty()
- Returns:
-
true
if and only if this list has no components, that is, its size is zero;false
otherwise - See Also:
elements
public Enumeration<E> elements()
- Returns:
- an enumeration of the components of this list
- See Also:
contains
public boolean contains(Object elem)
- Parameters:
-
elem
- an object - Returns:
-
true
if the specified object is the same as a component in this list - See Also:
indexOf
public int indexOf(Object elem)
elem
.- 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:
indexOf
public int indexOf(Object elem, int index)
elem
, beginning the search at index
.- Parameters:
-
elem
- the desired component -
index
- 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:
lastIndexOf
public int lastIndexOf(Object elem)
elem
.- Parameters:
-
elem
- the desired component - Returns:
- the index of the last occurrence of
elem
in the list; returnselem
if the object is not found - See Also:
lastIndexOf
public int lastIndexOf(Object elem, int index)
elem
, starting from the specified index, and returns an index to it.- Parameters:
-
elem
- the desired component -
index
- 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:
elementAt
public E elementAt(int index)
Note: Although this method is not deprecated, the preferred method to use isget(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 the index is negative or not less than the size of the list - See Also:
firstElement
public E firstElement()
- Returns:
- the first component of this list
- Throws:
-
NoSuchElementException
- if this vector has no components - See Also:
lastElement
public E lastElement()
- Returns:
- the last component of the list
- Throws:
-
NoSuchElementException
- if this vector has no components - See Also:
setElementAt
public void setElementAt(E element, int index)
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 isset(int,Object)
, which implements theList
interface defined in the 1.2 Collections framework.
- Parameters:
-
element
- what the component is to be set to -
index
- the specified index - Throws:
-
ArrayIndexOutOfBoundsException
- if the index is invalid - See Also:
removeElementAt
public void removeElementAt(int index)
Note: Although this method is not deprecated, the preferred method to use isremove(int)
, which implements theList
interface defined in the 1.2 Collections framework.
- Parameters:
-
index
- the index of the object to remove - Throws:
-
ArrayIndexOutOfBoundsException
- if the index is invalid - See Also:
insertElementAt
public void insertElementAt(E element, int index)
index
. Note: Although this method is not deprecated, the preferred method to use isadd(int,Object)
, which implements theList
interface defined in the 1.2 Collections framework.
- Parameters:
-
element
- the component to insert -
index
- where to insert the new component - Throws:
-
ArrayIndexOutOfBoundsException
- if the index was invalid - See Also:
addElement
public void addElement(E element)
- Parameters:
-
element
- the component to be added - See Also:
removeElement
public boolean removeElement(Object obj)
- Parameters:
-
obj
- the component to be removed - Returns:
-
true
if the argument was a component of this list;false
otherwise - See Also:
removeAllElements
public void removeAllElements()
Note: Although this method is not deprecated, the preferred method to use isclear
, which implements theList
interface defined in the 1.2 Collections framework.
- See Also:
toString
public String toString()
toArray
public Object[] toArray()
- Returns:
- an array containing the elements of the list
- See Also:
get
public E get(int index)
- Parameters:
-
index
- index of element to return - Returns:
- the element at the specified position in this list
- Throws:
-
ArrayIndexOutOfBoundsException
- if the index is out of range (index < 0 || index >= size()
)
set
public E set(int index, E element)
- Parameters:
-
index
- index of element to replace -
element
- element to be stored at the specified position - Returns:
- the element previously at the specified position
- Throws:
-
ArrayIndexOutOfBoundsException
- if the index is out of range (index < 0 || index >= size()
)
add
public void add(int index, E element)
- Parameters:
-
index
- index at which the specified element is to be inserted -
element
- element to be inserted - Throws:
-
ArrayIndexOutOfBoundsException
- if the index is out of range (index < 0 || index > size()
)
remove
public E remove(int index)
- Parameters:
-
index
- the index of the element to removed - Returns:
- the element previously at the specified position
- Throws:
-
ArrayIndexOutOfBoundsException
- if the index is out of range (index < 0 || index >= size()
)
clear
public void clear()
removeRange
public void removeRange(int fromIndex, int toIndex)
- Parameters:
-
fromIndex
- the index of the lower end of the range -
toIndex
- the index of the upper end of the range - Throws:
-
ArrayIndexOutOfBoundsException
- if the index was invalid -
IllegalArgumentException
- iffromIndex > toIndex
- See Also:
addAll
public void addAll(Collection<? extends E> c)
- Parameters:
-
c
- the collection which contains the elements to add - Throws:
-
NullPointerException
- ifc
is null
addAll
public void addAll(int index, Collection<? extends E> c)
- Parameters:
-
index
- index at which to insert the first element from the specified collection -
c
- the collection which contains the elements to add - Throws:
-
ArrayIndexOutOfBoundsException
- ifindex
does not fall within the range of number of elements currently held -
NullPointerException
- ifc
is null
© 1993, 2021, 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.
https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/javax/swing/DefaultListModel.html