public class SpinnerListModel extends AbstractSpinnerModel implements Serializable
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 ChangeListener
s are notified whenever the
model's value
or list
properties changes.
JSpinner
,
SpinnerModel
,
AbstractSpinnerModel
,
SpinnerNumberModel
,
SpinnerDateModel
listenerList
Constructor and Description |
---|
SpinnerListModel()
Constructs an effectively empty
SpinnerListModel . |
SpinnerListModel(List<?> values)
Constructs a
SpinnerModel whose sequence of
values is defined by the specified List . |
SpinnerListModel(Object[] values)
Constructs a
SpinnerModel whose sequence of values
is defined by the specified array. |
Modifier and Type | Method and Description |
---|---|
List<?> |
getList()
Returns the
List that defines the sequence for this model. |
Object |
getNextValue()
Returns the next legal value of the underlying sequence or
null if value is already the last element. |
Object |
getPreviousValue()
Returns the previous element of the underlying sequence or
null if value is already the first element. |
Object |
getValue()
Returns the current element of the sequence.
|
void |
setList(List<?> list)
Changes the list that defines this sequence and resets the index
of the models
value to zero. |
void |
setValue(Object elt)
Changes the current element of the sequence and notifies
ChangeListeners . |
addChangeListener, fireStateChanged, getChangeListeners, getListeners, removeChangeListener
public SpinnerListModel(List<?> values)
SpinnerModel
whose sequence of
values is defined by the specified List
.
The initial value (current element)
of the model will be values.get(0)
.
If values
is null
or has zero
size, an IllegalArugmentException
is thrown.values
- the sequence this model representsIllegalArgumentException
- if values
is
null
or zero sizepublic SpinnerListModel(Object[] values)
SpinnerModel
whose sequence of values
is defined by the specified array. The initial value of the model
will be values[0]
. If values
is
null
or has zero length, an
IllegalArgumentException
is thrown.values
- the sequence this model representsIllegalArgumentException
- if values
is
null
or zero lengthpublic SpinnerListModel()
SpinnerListModel
.
The model's list will contain a single
"empty"
string element.public List<?> getList()
List
that defines the sequence for this model.list
propertysetList(java.util.List<?>)
public void setList(List<?> list)
value
to zero. Note that list
is not copied, the model just stores a reference to it.
This method fires a ChangeEvent
if list
is
not equal to the current list.
list
- the sequence that this model representsIllegalArgumentException
- if list
is
null
or zero lengthgetList()
public Object getValue()
getValue
in interface SpinnerModel
value
propertySpinnerModel.getValue()
,
setValue(java.lang.Object)
public void setValue(Object elt)
ChangeListeners
. If the specified
value is not equal to an element of the underlying sequence
then an IllegalArgumentException
is thrown.
In the following example the setValue
call
would cause an exception to be thrown:
String[] values = {"one", "two", "free", "four"}; SpinnerModel model = new SpinnerListModel(values); model.setValue("TWO");
setValue
in interface SpinnerModel
elt
- the sequence element that will be model's current valueIllegalArgumentException
- if the specified value isn't allowedSpinnerModel.setValue(java.lang.Object)
,
getValue()
public Object getNextValue()
null
if value is already the last element.getNextValue
in interface SpinnerModel
null
if value is already the last elementSpinnerModel.getNextValue()
,
getPreviousValue()
public Object getPreviousValue()
null
if value is already the first element.getPreviousValue
in interface SpinnerModel
null
if value is already the first elementSpinnerModel.getPreviousValue()
,
getNextValue()
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2016, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.