FileDocCategorySizeDatePackage
DefaultListModel.javaAPI DocJava SE 5 API16801Fri Aug 26 14:57:54 BST 2005javax.swing

DefaultListModel

public class DefaultListModel extends AbstractListModel
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 JavaBeansTM has been added to the java.beans package. Please see {@link java.beans.XMLEncoder}.

version
1.35 05/05/04
author
Hans Muller

Fields Summary
private Vector
delegate
Constructors Summary
Methods Summary
public voidadd(int index, java.lang.Object 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()).

param
index index at which the specified element is to be inserted
param
element element to be inserted

	delegate.insertElementAt(element, index);
	fireIntervalAdded(this, index, index);
    
public voidaddElement(java.lang.Object obj)
Adds the specified component to the end of this list.

param
obj the component to be added
see
Vector#addElement(Object)

	int index = delegate.size();
	delegate.addElement(obj);
	fireIntervalAdded(this, index, index);
    
public intcapacity()
Returns the current capacity of this list.

return
the current capacity
see
Vector#capacity()

	return delegate.capacity();
    
public voidclear()
Removes all of the elements from this list. The list will be empty after this call returns (unless it throws an exception).

	int index1 = delegate.size()-1;
	delegate.removeAllElements();
	if (index1 >= 0) {
	    fireIntervalRemoved(this, 0, index1);
	}
    
public booleancontains(java.lang.Object elem)
Tests whether the specified object is a component in this list.

param
elem an object
return
true if the specified object is the same as a component in this list
see
Vector#contains(Object)

	return delegate.contains(elem);
    
public voidcopyInto(java.lang.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.

param
anArray the array into which the components get copied
see
Vector#copyInto(Object[])

	delegate.copyInto(anArray);
    
public java.lang.ObjectelementAt(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.

param
index an index into this list
return
the component at the specified index
see
#get(int)
see
Vector#elementAt(int)

	return delegate.elementAt(index);
    
public java.util.Enumerationelements()
Returns an enumeration of the components of this list.

return
an enumeration of the components of this list
see
Vector#elements()

	return delegate.elements();
    
public 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.

param
minCapacity the desired minimum capacity
see
Vector#ensureCapacity(int)

	delegate.ensureCapacity(minCapacity);
    
public java.lang.ObjectfirstElement()
Returns the first component of this list. Throws a NoSuchElementException if this vector has no components.

return
the first component of this list
see
Vector#firstElement()

	return delegate.firstElement();
    
public java.lang.Objectget(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()).

param
index index of element to return

	return delegate.elementAt(index);
    
public java.lang.ObjectgetElementAt(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.

param
index an index into this list
return
the component at the specified index
exception
ArrayIndexOutOfBoundsException if the index is negative or greater than the current size of this list
see
#get(int)

	return delegate.elementAt(index);
    
public intgetSize()
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.

return
the number of components in this list
see
#size()


                                                               
       
	return delegate.size();
    
public intindexOf(java.lang.Object elem)
Searches for the first occurrence of elem.

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

	return delegate.indexOf(elem);
    
public intindexOf(java.lang.Object elem, int index)
Searches for the first occurrence of elem, beginning the search at index.

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

	return delegate.indexOf(elem, index);
    
public voidinsertElementAt(java.lang.Object obj, int index)
Inserts the specified object 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.

param
obj the component to insert
param
index where to insert the new component
exception
ArrayIndexOutOfBoundsException if the index was invalid
see
#add(int,Object)
see
Vector#insertElementAt(Object,int)

	delegate.insertElementAt(obj, index);
	fireIntervalAdded(this, index, index);
    
public booleanisEmpty()
Tests whether this list has any components.

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

	return delegate.isEmpty();
    
public java.lang.ObjectlastElement()
Returns the last component of the list. Throws a NoSuchElementException if this vector has no components.

return
the last component of the list
see
Vector#lastElement()

	return delegate.lastElement();
    
public intlastIndexOf(java.lang.Object elem)
Returns the index of the last occurrence of elem.

param
elem the desired component
return
the index of the last occurrence of elem in the list; returns -1 if the object is not found
see
Vector#lastIndexOf(Object)

	return delegate.lastIndexOf(elem);
    
public intlastIndexOf(java.lang.Object elem, int index)
Searches backwards for elem, starting from the specified index, and returns an index to it.

param
elem the desired component
param
index the index to start searching from
return
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
Vector#lastIndexOf(Object,int)

	return delegate.lastIndexOf(elem, index);
    
public java.lang.Objectremove(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()).

param
index the index of the element to removed

	Object rv = delegate.elementAt(index);
	delegate.removeElementAt(index);
	fireIntervalRemoved(this, index, index);
	return rv;
    
public voidremoveAllElements()
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
#clear()
see
Vector#removeAllElements()

	int index1 = delegate.size()-1;
	delegate.removeAllElements();
	if (index1 >= 0) {
	    fireIntervalRemoved(this, 0, index1);
	}
    
public booleanremoveElement(java.lang.Object obj)
Removes the first (lowest-indexed) occurrence of the argument from this list.

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

	int index = indexOf(obj);
	boolean rv = delegate.removeElement(obj);
	if (index >= 0) {
	    fireIntervalRemoved(this, index, index);
	}
	return rv;
    
public voidremoveElementAt(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.

param
index the index of the object to remove
see
#remove(int)
see
Vector#removeElementAt(int)

	delegate.removeElementAt(index);
	fireIntervalRemoved(this, index, index);
    
public voidremoveRange(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.

param
fromIndex the index of the lower end of the range
param
toIndex the index of the upper end of the range
see
#remove(int)

	if (fromIndex > toIndex) {
	    throw new IllegalArgumentException("fromIndex must be <= toIndex");
	}
	for(int i = toIndex; i >= fromIndex; i--) {
	    delegate.removeElementAt(i);
	}
	fireIntervalRemoved(this, fromIndex, toIndex);
    
public java.lang.Objectset(int index, java.lang.Object 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()).

param
index index of element to replace
param
element element to be stored at the specified position
return
the element previously at the specified position

	Object rv = delegate.elementAt(index);
	delegate.setElementAt(element, index);
	fireContentsChanged(this, index, index);
	return rv;
    
public voidsetElementAt(java.lang.Object obj, int index)
Sets the component at the specified index of this list to be the specified object. 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.

param
obj what the component is to be set to
param
index the specified index
see
#set(int,Object)
see
Vector#setElementAt(Object,int)

	delegate.setElementAt(obj, index);
	fireContentsChanged(this, index, index);
    
public voidsetSize(int newSize)
Sets the size of this list.

param
newSize the new size of this list
see
Vector#setSize(int)

	int oldSize = delegate.size();
	delegate.setSize(newSize);
	if (oldSize > newSize) {
	    fireIntervalRemoved(this, newSize, oldSize-1);
	}
	else if (oldSize < newSize) {
	    fireIntervalAdded(this, oldSize, newSize-1);
	}
    
public intsize()
Returns the number of components in this list.

return
the number of components in this list
see
Vector#size()

	return delegate.size();
    
public java.lang.Object[]toArray()
Returns an array containing all of the elements in this list in the correct order.

return
an array containing the elements of the list
see
Vector#toArray()

	Object[] rv = new Object[delegate.size()];
	delegate.copyInto(rv);
	return rv;
    
public java.lang.StringtoString()
Returns a string that displays and identifies this object's properties.

return
a String representation of this object

	return delegate.toString();
    
public voidtrimToSize()
Trims the capacity of this list to be the list's current size.

see
Vector#trimToSize()

	delegate.trimToSize();