javax.swing

Class SpinnerListModel

Implemented Interfaces:
Serializable, SpinnerModel

public class SpinnerListModel
extends AbstractSpinnerModel
implements Serializable

An implementation of SpinnerModel which uses the values contained within a list or an array. The backing list or array is only stored as a reference within the class. As a result, changes made elsewhere to the members of the list or array are reflected by this model.

The model itself inherits a list of ChangeListeners from AbstractSpinnerModel. As this code is unaware of changes made to the backing list or array, it is the responsibility of the application using the model to invoke fireStateChanged(), in order to notify any ChangeListeners, when the list or array changes. The model handles notification when the reference itself is changed via setList() or when the current value is set directly using setValue().

Since:
1.4
See Also:
SpinnerModel, AbstractSpinnerModel, JSpinner, Serialized Form

Field Summary

Fields inherited from class javax.swing.AbstractSpinnerModel

listenerList

Constructor Summary

SpinnerListModel()
Constructs a default SpinnerListModel.
SpinnerListModel(List list)
Constructs a SpinnerListModel using the supplied list.
SpinnerListModel(Object[] array)
Constructs a SpinnerListModel using the supplied array.

Method Summary

List
getList()
Returns the backing list for this model.
Object
getNextValue()
Returns the next value from the list, which is the same as the element stored at the current index + 1.
Object
getPreviousValue()
Returns the previous value from the list, which is the same as the element stored at the current index - 1.
Object
getValue()
Returns the current value of the model.
void
setList(List list)
Changes the backing list for this model.
void
setValue(Object value)
Sets the current value of the model to be the one supplied.

Methods inherited from class javax.swing.AbstractSpinnerModel

addChangeListener, extends EventListener> T[] getListeners, fireStateChanged, getChangeListeners, removeChangeListener

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

SpinnerListModel

public SpinnerListModel()
Constructs a default SpinnerListModel. This is a model backed by a list containing only the single String element, "empty".

SpinnerListModel

public SpinnerListModel(List list)
Constructs a SpinnerListModel using the supplied list. The model maintains a reference to this list, and returns consecutive elements in response to calls to getNextValue(). The initial value is that at position 0, so an initial call to getValue() returns the same as list.get(0).
Parameters:
list - The list to use for this model.
Throws:
IllegalArgumentException - if the list is null or contains no elements.

SpinnerListModel

public SpinnerListModel(Object[] array)
Constructs a SpinnerListModel using the supplied array. The model stores a reference to the wrapper list returned by Arrays.asList(). The wrapper list reflects modifications in the underlying array, so these changes will also be reflected by the model. The model produces consecutive elements from the array in response to calls to getNextValue(). The initial value returned by getValue() is the same as array[0].
Parameters:
array - The array to use for this model.
Throws:
IllegalArgumentException - if the array is null or contains no elements.
See Also:
Arrays.asList(Object[]), getNextValue(), getValue()

Method Details

getList

public List getList()
Returns the backing list for this model.
Returns:
The backing list.

getNextValue

public Object getNextValue()
Returns the next value from the list, which is the same as the element stored at the current index + 1. Null is returned if there are no more values to be returned (the end of the list has been reached). An ambiguity can occur here, as null may also be returned as a valid list element. This operation does not change the current value.
Specified by:
getNextValue in interface SpinnerModel
Returns:
The next value from the list or null.

getPreviousValue

public Object getPreviousValue()
Returns the previous value from the list, which is the same as the element stored at the current index - 1. Null is returned if there are no more values to be returned (the start of the list has been reached). An ambiguity can occur here, as null may also be returned as a valid list element. This operation does not change the current value.
Specified by:
getPreviousValue in interface SpinnerModel
Returns:
The previous value from the list or null.

getValue

public Object getValue()
Returns the current value of the model. Initially, this will be the element at position 0. On later invocations, this will be the last element returned by getNextValue() or getPreviousValue().
Specified by:
getValue in interface SpinnerModel
Returns:
The current value.

setList

public void setList(List list)
Changes the backing list for this model. The model only stores a reference to the list, so any changes made to the list elsewhere will be reflected in the values returned by the model. A ChangeEvent is fired if the list being used actually changes (i.e. the new list is not referentially equal (!=) to the old one).
Parameters:
list - The new list to use.
Throws:
IllegalArgumentException - if the list is null or contains no elements.
See Also:
ChangeEvent

setValue

public void setValue(Object value)
Sets the current value of the model to be the one supplied. The value must exist within the backing list in order for the change to take place. Otherwise, an exception is thrown. The value used is the first occurrence of the value within the backing list. Listeners are notified of this change. Following the change, getNextValue() and getPreviousValue() return the objects following and prior to the supplied value, respectively.
Specified by:
setValue in interface SpinnerModel
Parameters:
value - The requested new value of the list.
Throws:
IllegalArgumentException - if the supplied value does not exist in the backing list.

SpinnerListModel.java -- A spinner model backed by a list or an array. Copyright (C) 2004, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.