Class SpinnerListModel
java.lang.Object
javax.swing.AbstractSpinnerModel
javax.swing.SpinnerListModel
- All Implemented Interfaces:
Serializable, SpinnerModel
A simple implementation of
SpinnerModel whose
values are defined by an array or a List.
For example to create a model defined by
an array of the names of the days of the week:
String[] days = new DateFormatSymbols().getWeekdays(); SpinnerModel model = new SpinnerListModel(Arrays.asList(days).subList(1, 8));This class only stores a reference to the array or
List
so if an element of the underlying sequence changes, it's up
to the application to notify the ChangeListeners by calling
fireStateChanged.
This model inherits a ChangeListener.
The ChangeListeners are notified whenever the
model's value or list properties changes.
- Since:
- 1.4
- See Also:
-
Field Summary
Fields declared in class AbstractSpinnerModel
listenerListModifier and TypeFieldDescriptionprotected EventListenerListThe list of ChangeListeners for this model. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an effectively emptySpinnerListModel.SpinnerListModel(Object[] values) Constructs aSpinnerModelwhose sequence of values is defined by the specified array.SpinnerListModel(List<?> values) Constructs aSpinnerModelwhose sequence of values is defined by the specifiedList. -
Method Summary
Modifier and TypeMethodDescriptionList<?> getList()Returns theListthat defines the sequence for this model.Returns the next legal value of the underlying sequence ornullif value is already the last element.Returns the previous element of the underlying sequence ornullif value is already the first element.getValue()Returns the current element of the sequence.voidChanges the list that defines this sequence and resets the index of the modelsvalueto zero.voidChanges the current element of the sequence and notifiesChangeListeners.Methods declared in class AbstractSpinnerModel
addChangeListener, fireStateChanged, getChangeListeners, getListeners, removeChangeListenerModifier and TypeMethodDescriptionvoidAdds a ChangeListener to the model's listener list.protected voidRun each ChangeListeners stateChanged() method.Returns an array of all theChangeListeners added to this AbstractSpinnerModel with addChangeListener().<T extends EventListener>
T[]getListeners(Class<T> listenerType) Return an array of all the listeners of the given type that were added to this model.voidRemoves a ChangeListener from the model's listener list.Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitModifier and TypeMethodDescriptionprotected Objectclone()Answers a new instance of the same class as the receiver, whose slots have been filled in with the values in the slots of the receiver.booleanCompares the argument to the receiver, and answers true if they represent the same object using a class specific comparison.protected voidfinalize()Deprecated, for removal: This API element is subject to removal in a future version.May cause performance issues, deadlocks and hangs.getClass()Answers the unique instance of java.lang.Class which represents the class of the receiver.inthashCode()Answers an integer hash code for the receiver.final voidnotify()Causes one thread which iswaiting on the receiver to be made ready to run.final voidCauses all threads which arewaiting on the receiver to be made ready to run.toString()Answers a string containing a concise, human-readable description of the receiver.final voidwait()Causes the thread which sent this message to be made not ready to run pending some change in the receiver (as indicated bynotifyornotifyAll).final voidwait(long time) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.final voidwait(long time, int frac) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.
-
Constructor Details
-
SpinnerListModel
Constructs aSpinnerModelwhose sequence of values is defined by the specifiedList. The initial value (current element) of the model will bevalues.get(0). Ifvaluesisnullor has zero size, anIllegalArgumentExceptionis thrown.- Parameters:
values- the sequence this model represents- Throws:
IllegalArgumentException- ifvaluesisnullor zero size
-
SpinnerListModel
Constructs aSpinnerModelwhose sequence of values is defined by the specified array. The initial value of the model will bevalues[0]. Ifvaluesisnullor has zero length, anIllegalArgumentExceptionis thrown.- Parameters:
values- the sequence this model represents- Throws:
IllegalArgumentException- ifvaluesisnullor zero length
-
SpinnerListModel
public SpinnerListModel()Constructs an effectively emptySpinnerListModel. The model's list will contain a single"empty"string element.
-
-
Method Details
-
getList
Returns theListthat defines the sequence for this model.- Returns:
- the value of the
listproperty - See Also:
-
setList
Changes the list that defines this sequence and resets the index of the modelsvalueto zero. Note thatlistis not copied, the model just stores a reference to it.This method fires a
ChangeEventiflistis not equal to the current list.- Parameters:
list- the sequence that this model represents- Throws:
IllegalArgumentException- iflistisnullor zero length- See Also:
-
getValue
Returns the current element of the sequence.- Specified by:
getValuein interfaceSpinnerModel- Returns:
- the
valueproperty - See Also:
-
setValue
Changes the current element of the sequence and notifiesChangeListeners. If the specified value is not equal to an element of the underlying sequence then anIllegalArgumentExceptionis thrown. In the following example thesetValuecall would cause an exception to be thrown:String[] values = {"one", "two", "free", "four"}; SpinnerModel model = new SpinnerListModel(values); model.setValue("TWO");- Specified by:
setValuein interfaceSpinnerModel- Parameters:
elt- the sequence element that will be model's current value- Throws:
IllegalArgumentException- if the specified value isn't allowed- See Also:
-
getNextValue
Returns the next legal value of the underlying sequence ornullif value is already the last element.- Specified by:
getNextValuein interfaceSpinnerModel- Returns:
- the next legal value of the underlying sequence or
nullif value is already the last element - See Also:
-
getPreviousValue
Returns the previous element of the underlying sequence ornullif value is already the first element.- Specified by:
getPreviousValuein interfaceSpinnerModel- Returns:
- the previous element of the underlying sequence or
nullif value is already the first element - See Also:
-