javax.swing

Class SpinnerListModel

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

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.
ObjectgetNextValue()
Returns the next value from the list, which is the same as the element stored at the current index + 1.
ObjectgetPreviousValue()
Returns the previous value from the list, which is the same as the element stored at the current index - 1.
ObjectgetValue()
Returns the current value of the model.
voidsetList(List<?> list)
Changes the backing list for this model.
voidsetValue(Object value)
Sets the current value of the model to be the one supplied.

Constructor Detail

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.

See Also: getNextValue getValue

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: (Object[]) getNextValue getValue

Method Detail

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.

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.

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().

Returns: The current value.

See Also: getPreviousValue getNextValue

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.

Parameters: value The requested new value of the list.

Throws: IllegalArgumentException if the supplied value does not exist in the backing list.

See Also: getPreviousValue getNextValue