javax.swing
Class SpinnerListModel

java.lang.Object
  |
  +--javax.swing.AbstractSpinnerModel
        |
        +--javax.swing.SpinnerListModel
All Implemented Interfaces:
Serializable, SpinnerModel

public class SpinnerListModel
extends AbstractSpinnerModel
implements Serializable

Safe: 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
Version:
1.8 12/03/01
Author:
Hans Muller
See Also:
JSpinner, SpinnerModel, AbstractSpinnerModel, SpinnerNumberModel, SpinnerDateModel, Serialized Form

Field Summary
private  int index
           
private  List list
           
 
Fields inherited from class javax.swing.AbstractSpinnerModel
listenerList
 
Constructor Summary
SpinnerListModel()
          Enabled: Constructs an effectively empty SpinnerListModel.
SpinnerListModel(List values)
          Enabled: Constructs a SpinnerModel whose sequence of values is defined by the specified List.
SpinnerListModel(Object[] values)
          Enabled: Constructs a SpinnerModel whose sequence of values is defined by the specified array.
 
Method Summary
(package private)  Object findNextMatch(String substring)
          Returns the next object that starts with substring.
 List getList()
          Enabled: Returns the List that defines the sequence for this model.
 Object getNextValue()
          Enabled: Returns the next legal value of the underlying sequence or null if value is already the last element.
 Object getPreviousValue()
          Enabled: Returns the previous element of the underlying sequence or null if value is already the first element.
 Object getValue()
          Enabled: Returns the current element of the sequence.
 void setList(List list)
          Enabled: Changes the list that defines this sequence and resets the index of the models value to zero.
 void setValue(Object elt)
          Enabled: Changes the current element of the sequence and notifies ChangeListeners.
 
Methods inherited from class javax.swing.AbstractSpinnerModel
addChangeListener, fireStateChanged, getChangeListeners, getListeners, removeChangeListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

list

private List list

index

private int index
Constructor Detail

SpinnerListModel

public SpinnerListModel(List values)
Enabled: 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:
IllegalArugmentException - if values is null or zero size

SpinnerListModel

public SpinnerListModel(Object[] values)
Enabled: 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 IllegalArugmentException is thrown.

Parameters:
values - the sequence this model represents
Throws:
IllegalArugmentException - if values is null or zero length

SpinnerListModel

public SpinnerListModel()
Enabled: Constructs an effectively empty SpinnerListModel. The model's list will contain a single "empty" string element.

Method Detail

getList

public List getList()
Enabled: Returns the List that defines the sequence for this model.

Returns:
the value of the list property
See Also:
setList(java.util.List)

setList

public void setList(List list)
Enabled: 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 - if list is null or zero length
See Also:
getList()

getValue

public Object getValue()
Enabled: Returns the current element of the sequence.

Specified by:
getValue in interface SpinnerModel
Returns:
the value property
See Also:
SpinnerModel.getValue(), setValue(java.lang.Object)

setValue

public void setValue(Object elt)
Enabled: 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 in interface SpinnerModel
Parameters:
elt - the sequence element that will be model's current value
Throws:
IllegalArgumentException - if the specified value isn't allowed
See Also:
SpinnerModel.setValue(java.lang.Object), getValue()

getNextValue

public Object getNextValue()
Enabled: Returns the next legal value of the underlying sequence or null if value is already the last element.

Specified by:
getNextValue in interface SpinnerModel
Returns:
the next legal value of the underlying sequence or null if value is already the last element
See Also:
SpinnerModel.getNextValue(), getPreviousValue()

getPreviousValue

public Object getPreviousValue()
Enabled: Returns the previous element of the underlying sequence or null if value is already the first element.

Specified by:
getPreviousValue in interface SpinnerModel
Returns:
the previous element of the underlying sequence or null if value is already the first element
See Also:
SpinnerModel.getPreviousValue(), getNextValue()

findNextMatch

Object findNextMatch(String substring)
Returns the next object that starts with substring.

Parameters:
substring - the string to be matched
Returns:
the match


comments?