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

DefaultComboBoxModel

public class DefaultComboBoxModel extends AbstractListModel implements Serializable, MutableComboBoxModel
The default model for combo boxes.
version
1.19 05/05/04
author
Arnaud Weber
author
Tom Santos

Fields Summary
Vector
objects
Object
selectedObject
Constructors Summary
public DefaultComboBoxModel()
Constructs an empty DefaultComboBoxModel object.

        objects = new Vector();
    
public DefaultComboBoxModel(Object[] items)
Constructs a DefaultComboBoxModel object initialized with an array of objects.

param
items an array of Object objects

        objects = new Vector();
        objects.ensureCapacity( items.length );

        int i,c;
        for ( i=0,c=items.length;i<c;i++ )
            objects.addElement(items[i]);

        if ( getSize() > 0 ) {
            selectedObject = getElementAt( 0 );
        }
    
public DefaultComboBoxModel(Vector v)
Constructs a DefaultComboBoxModel object initialized with a vector.

param
v a Vector object ...

        objects = v;

        if ( getSize() > 0 ) {
            selectedObject = getElementAt( 0 );
        }
    
Methods Summary
public voidaddElement(java.lang.Object anObject)

        objects.addElement(anObject);
        fireIntervalAdded(this,objects.size()-1, objects.size()-1);
        if ( objects.size() == 1 && selectedObject == null && anObject != null ) {
            setSelectedItem( anObject );
        }
    
public java.lang.ObjectgetElementAt(int index)

        if ( index >= 0 && index < objects.size() )
            return objects.elementAt(index);
        else
            return null;
    
public intgetIndexOf(java.lang.Object anObject)
Returns the index-position of the specified object in the list.

param
anObject
return
an int representing the index position, where 0 is the first position

        return objects.indexOf(anObject);
    
public java.lang.ObjectgetSelectedItem()

        return selectedObject;
    
public intgetSize()

        return objects.size();
    
public voidinsertElementAt(java.lang.Object anObject, int index)

        objects.insertElementAt(anObject,index);
        fireIntervalAdded(this, index, index);
    
public voidremoveAllElements()
Empties the list.

        if ( objects.size() > 0 ) {
            int firstIndex = 0;
            int lastIndex = objects.size() - 1;
            objects.removeAllElements();
	    selectedObject = null;
            fireIntervalRemoved(this, firstIndex, lastIndex);
        } else {
	    selectedObject = null;
	}
    
public voidremoveElement(java.lang.Object anObject)

        int index = objects.indexOf(anObject);
        if ( index != -1 ) {
            removeElementAt(index);
        }
    
public voidremoveElementAt(int index)

        if ( getElementAt( index ) == selectedObject ) {
            if ( index == 0 ) {
                setSelectedItem( getSize() == 1 ? null : getElementAt( index + 1 ) );
            }
            else {
                setSelectedItem( getElementAt( index - 1 ) );
            }
        }

        objects.removeElementAt(index);

        fireIntervalRemoved(this, index, index);
    
public voidsetSelectedItem(java.lang.Object anObject)
Set the value of the selected item. The selected item may be null.

param
anObject The combo box value or null for no selection.

        if ((selectedObject != null && !selectedObject.equals( anObject )) ||
	    selectedObject == null && anObject != null) {
	    selectedObject = anObject;
	    fireContentsChanged(this, -1, -1);
        }