SpinnerListModel (Java Platform SE 8 ) (original) (raw)
- javax.swing.AbstractSpinnerModel
- javax.swing.SpinnerListModel
All Implemented Interfaces:
Serializable, SpinnerModel
public class SpinnerListModel
extends AbstractSpinnerModel
implements Serializable
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 callingfireStateChanged
.
This model inherits a ChangeListener
. The ChangeListener
s are notified whenever the model's value
or list
properties changes.
Since:
1.4
See Also:
JSpinner, SpinnerModel, AbstractSpinnerModel, SpinnerNumberModel, SpinnerDateModel
Field Summary
* ### Fields inherited from class javax.swing.[AbstractSpinnerModel](../../javax/swing/AbstractSpinnerModel.html "class in javax.swing") `[listenerList](../../javax/swing/AbstractSpinnerModel.html#listenerList)`
Constructor Summary
Constructors
Constructor 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. Method Summary
All Methods Instance Methods Concrete Methods
Modifier and Type Method Description List<?> getList() Returns the List that defines the sequence for this model. Object getNextValue() Returns the next legal value of the underlying sequence ornull if value is already the last element. Object getPreviousValue() Returns the previous element of the underlying sequence ornull 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 notifiesChangeListeners. * ### Methods inherited from class javax.swing.[AbstractSpinnerModel](../../javax/swing/AbstractSpinnerModel.html "class in javax.swing") `[addChangeListener](../../javax/swing/AbstractSpinnerModel.html#addChangeListener-javax.swing.event.ChangeListener-), [fireStateChanged](../../javax/swing/AbstractSpinnerModel.html#fireStateChanged--), [getChangeListeners](../../javax/swing/AbstractSpinnerModel.html#getChangeListeners--), [getListeners](../../javax/swing/AbstractSpinnerModel.html#getListeners-java.lang.Class-), [removeChangeListener](../../javax/swing/AbstractSpinnerModel.html#removeChangeListener-javax.swing.event.ChangeListener-)` * ### Methods inherited from class java.lang.[Object](../../java/lang/Object.html "class in java.lang") `[clone](../../java/lang/Object.html#clone--), [equals](../../java/lang/Object.html#equals-java.lang.Object-), [finalize](../../java/lang/Object.html#finalize--), [getClass](../../java/lang/Object.html#getClass--), [hashCode](../../java/lang/Object.html#hashCode--), [notify](../../java/lang/Object.html#notify--), [notifyAll](../../java/lang/Object.html#notifyAll--), [toString](../../java/lang/Object.html#toString--), [wait](../../java/lang/Object.html#wait--), [wait](../../java/lang/Object.html#wait-long-), [wait](../../java/lang/Object.html#wait-long-int-)`
Constructor Detail
* #### SpinnerListModel public SpinnerListModel([List](../../java/util/List.html "interface in java.util")<?> values) Constructs a `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. Parameters: `values` \- the sequence this model represents Throws: `[IllegalArgumentException](../../java/lang/IllegalArgumentException.html "class in java.lang")` \- if `values` is`null` or zero size * #### SpinnerListModel public SpinnerListModel([Object](../../java/lang/Object.html "class in java.lang")[] values) Constructs a `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. Parameters: `values` \- the sequence this model represents Throws: `[IllegalArgumentException](../../java/lang/IllegalArgumentException.html "class in java.lang")` \- if `values` is`null` or zero length * #### SpinnerListModel public SpinnerListModel() Constructs an effectively empty `SpinnerListModel`. The model's list will contain a single`"empty"` string element.
Method Detail
* #### getList public [List](../../java/util/List.html "interface in java.util")<?> getList() Returns the `List` that defines the sequence for this model. Returns: the value of the `list` property See Also: [setList(java.util.List<?>)](../../javax/swing/SpinnerListModel.html#setList-java.util.List-) * #### setList public void setList([List](../../java/util/List.html "interface in java.util")<?> list) Changes the list that defines this sequence and resets the index of the models `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. Parameters: `list` \- the sequence that this model represents Throws: `[IllegalArgumentException](../../java/lang/IllegalArgumentException.html "class in java.lang")` \- if `list` is`null` or zero length See Also: [getList()](../../javax/swing/SpinnerListModel.html#getList--) * #### getValue public [Object](../../java/lang/Object.html "class in java.lang") getValue() Returns the current element of the sequence. Specified by: `[getValue](../../javax/swing/SpinnerModel.html#getValue--)` in interface `[SpinnerModel](../../javax/swing/SpinnerModel.html "interface in javax.swing")` Returns: the `value` property See Also: [SpinnerModel.getValue()](../../javax/swing/SpinnerModel.html#getValue--), [setValue(java.lang.Object)](../../javax/swing/SpinnerListModel.html#setValue-java.lang.Object-) * #### setValue public void setValue([Object](../../java/lang/Object.html "class in java.lang") elt) Changes the current element of the sequence and notifies`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"); Specified by: `[setValue](../../javax/swing/SpinnerModel.html#setValue-java.lang.Object-)` in interface `[SpinnerModel](../../javax/swing/SpinnerModel.html "interface in javax.swing")` Parameters: `elt` \- the sequence element that will be model's current value Throws: `[IllegalArgumentException](../../java/lang/IllegalArgumentException.html "class in java.lang")` \- if the specified value isn't allowed See Also: [SpinnerModel.setValue(java.lang.Object)](../../javax/swing/SpinnerModel.html#setValue-java.lang.Object-), [getValue()](../../javax/swing/SpinnerListModel.html#getValue--) * #### getNextValue public [Object](../../java/lang/Object.html "class in java.lang") getNextValue() Returns the next legal value of the underlying sequence or`null` if value is already the last element. Specified by: `[getNextValue](../../javax/swing/SpinnerModel.html#getNextValue--)` in interface `[SpinnerModel](../../javax/swing/SpinnerModel.html "interface in javax.swing")` Returns: the next legal value of the underlying sequence or`null` if value is already the last element See Also: [SpinnerModel.getNextValue()](../../javax/swing/SpinnerModel.html#getNextValue--), [getPreviousValue()](../../javax/swing/SpinnerListModel.html#getPreviousValue--) * #### getPreviousValue public [Object](../../java/lang/Object.html "class in java.lang") getPreviousValue() Returns the previous element of the underlying sequence or`null` if value is already the first element. Specified by: `[getPreviousValue](../../javax/swing/SpinnerModel.html#getPreviousValue--)` in interface `[SpinnerModel](../../javax/swing/SpinnerModel.html "interface in javax.swing")` Returns: the previous element of the underlying sequence or`null` if value is already the first element See Also: [SpinnerModel.getPreviousValue()](../../javax/swing/SpinnerModel.html#getPreviousValue--), [getNextValue()](../../javax/swing/SpinnerListModel.html#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, 2025, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.