- All Implemented Interfaces:
- Serializable,- SpinnerModel
public class SpinnerNumberModel extends AbstractSpinnerModel implements Serializable
SpinnerModel for sequences of numbers.
 The upper and lower bounds of the sequence are defined
 by properties called minimum and
 maximum. The size of the increase or decrease
 computed by the nextValue and
 previousValue methods is defined by a property called
 stepSize.  The minimum and
 maximum properties can be null
 to indicate that the sequence has no lower or upper limit.
 All of the properties in this class are defined in terms of two
 generic types: Number and
 Comparable, so that all Java numeric types
 may be accommodated.  Internally, there's only support for
 values whose type is one of the primitive Number types:
 Double, Float, Long,
 Integer, Short, or Byte.
 
 To create a SpinnerNumberModel for the integer
 range zero to one hundred, with
 fifty as the initial value, one could write:
 
Integer value = Integer.valueOf(50); Integer min = Integer.valueOf(0); Integer max = Integer.valueOf(100); Integer step = Integer.valueOf(1); SpinnerNumberModel model = new SpinnerNumberModel(value, min, max, step); int fifty = model.getNumber().intValue();
Spinners for integers and doubles are common, so special constructors for these cases are provided. For example to create the model in the previous example, one could also write:
SpinnerNumberModel model = new SpinnerNumberModel(50, 0, 100, 1);
 This model inherits a ChangeListener.
 The ChangeListeners are notified
 whenever the model's value, stepSize,
 minimum, or maximum properties changes.
- Since:
- 1.4
- See Also:
- JSpinner,- SpinnerModel,- AbstractSpinnerModel,- SpinnerListModel,- SpinnerDateModel
- 
Field Summary
- 
Constructor SummaryConstructors Constructor Description SpinnerNumberModel()Constructs aSpinnerNumberModelwith nominimumormaximumvalue,stepSizeequal to one, and an initial value of zero.SpinnerNumberModel(double value, double minimum, double maximum, double stepSize)Constructs aSpinnerNumberModelwith the specifiedvalue,minimum/maximumbounds, andstepSize.SpinnerNumberModel(int value, int minimum, int maximum, int stepSize)Constructs aSpinnerNumberModelwith the specifiedvalue,minimum/maximumbounds, andstepSize.SpinnerNumberModel(Number value, Comparable<?> minimum, Comparable<?> maximum, Number stepSize)Constructs aSpinnerModelthat represents a closed sequence of numbers fromminimumtomaximum.
- 
Method SummaryModifier and Type Method Description Comparable<?>getMaximum()Returns the last number in the sequence.Comparable<?>getMinimum()Returns the first number in this sequence.ObjectgetNextValue()Returns the next number in the sequence.NumbergetNumber()Returns the value of the current element of the sequence.ObjectgetPreviousValue()Returns the previous number in the sequence.NumbergetStepSize()Returns the size of the value change computed by thegetNextValueandgetPreviousValuemethods.ObjectgetValue()Returns the value of the current element of the sequence.voidsetMaximum(Comparable<?> maximum)Changes the upper bound for numbers in this sequence.voidsetMinimum(Comparable<?> minimum)Changes the lower bound for numbers in this sequence.voidsetStepSize(Number stepSize)Changes the size of the value change computed by thegetNextValueandgetPreviousValuemethods.voidsetValue(Object value)Sets the current value for this sequence.Methods declared in class javax.swing.AbstractSpinnerModeladdChangeListener, fireStateChanged, getChangeListeners, getListeners, removeChangeListener
- 
Constructor Details- 
SpinnerNumberModelpublic SpinnerNumberModel(Number value, Comparable<?> minimum, Comparable<?> maximum, Number stepSize)Constructs aSpinnerModelthat represents a closed sequence of numbers fromminimumtomaximum. ThenextValueandpreviousValuemethods compute elements of the sequence by adding or subtractingstepSizerespectively. All of the parameters must be mutuallyComparable,valueandstepSizemust be instances ofIntegerLong,Float, orDouble.The minimumandmaximumparameters can benullto indicate that the range doesn't have an upper or lower bound. IfvalueorstepSizeisnull, or if bothminimumandmaximumare specified andminimum > maximumthen anIllegalArgumentExceptionis thrown. Similarly if(minimum <= value <= maximum) is false, anIllegalArgumentExceptionis thrown.- Parameters:
- value- the current (non- null) value of the model
- minimum- the first number in the sequence or- null
- maximum- the last number in the sequence or- null
- stepSize- the difference between elements of the sequence
- Throws:
- IllegalArgumentException- if stepSize or value is- nullor if the following expression is false:- minimum <= value <= maximum
 
- 
SpinnerNumberModelpublic SpinnerNumberModel(int value, int minimum, int maximum, int stepSize)Constructs aSpinnerNumberModelwith the specifiedvalue,minimum/maximumbounds, andstepSize.- Parameters:
- value- the current value of the model
- minimum- the first number in the sequence
- maximum- the last number in the sequence
- stepSize- the difference between elements of the sequence
- Throws:
- IllegalArgumentException- if the following expression is false:- minimum <= value <= maximum
 
- 
SpinnerNumberModelpublic SpinnerNumberModel(double value, double minimum, double maximum, double stepSize)Constructs aSpinnerNumberModelwith the specifiedvalue,minimum/maximumbounds, andstepSize.- Parameters:
- value- the current value of the model
- minimum- the first number in the sequence
- maximum- the last number in the sequence
- stepSize- the difference between elements of the sequence
- Throws:
- IllegalArgumentException- if the following expression is false:- minimum <= value <= maximum
 
- 
SpinnerNumberModelpublic SpinnerNumberModel()Constructs aSpinnerNumberModelwith nominimumormaximumvalue,stepSizeequal to one, and an initial value of zero.
 
- 
- 
Method Details- 
setMinimumChanges the lower bound for numbers in this sequence. Ifminimumisnull, then there is no lower bound. No bounds checking is done here; the newminimumvalue may invalidate the(minimum <= value <= maximum)invariant enforced by the constructors. This is to simplify updating the model, naturally one should ensure that the invariant is true before calling thegetNextValue,getPreviousValue, orsetValuemethods.Typically this property is a Numberof the same type as thevaluehowever it's possible to use anyComparablewith acompareTomethod for aNumberwith the same type as the value. For example if value was aLong,minimummight be a Date subclass defined like this:MyDate extends Date { // Date already implements Comparable public int compareTo(Long o) { long t = getTime(); return (t < o.longValue() ? -1 : (t == o.longValue() ? 0 : 1)); } }This method fires a ChangeEventif theminimumhas changed.- Parameters:
- minimum- a- Comparablethat has a- compareTomethod for- Numbers with the same type as- value
- See Also:
- getMinimum(),- setMaximum(java.lang.Comparable<?>),- SpinnerModel.addChangeListener(javax.swing.event.ChangeListener)
 
- 
getMinimumReturns the first number in this sequence.- Returns:
- the value of the minimumproperty
- See Also:
- setMinimum(java.lang.Comparable<?>)
 
- 
setMaximumChanges the upper bound for numbers in this sequence. Ifmaximumisnull, then there is no upper bound. No bounds checking is done here; the newmaximumvalue may invalidate the(minimum <= value < maximum)invariant enforced by the constructors. This is to simplify updating the model, naturally one should ensure that the invariant is true before calling thenext,previous, orsetValuemethods.Typically this property is a Numberof the same type as thevaluehowever it's possible to use anyComparablewith acompareTomethod for aNumberwith the same type as the value. SeesetMinimum(Comparable)for an example.This method fires a ChangeEventif themaximumhas changed.- Parameters:
- maximum- a- Comparablethat has a- compareTomethod for- Numbers with the same type as- value
- See Also:
- getMaximum(),- setMinimum(java.lang.Comparable<?>),- SpinnerModel.addChangeListener(javax.swing.event.ChangeListener)
 
- 
getMaximumReturns the last number in the sequence.- Returns:
- the value of the maximumproperty
- See Also:
- setMaximum(java.lang.Comparable<?>)
 
- 
setStepSizeChanges the size of the value change computed by thegetNextValueandgetPreviousValuemethods. AnIllegalArgumentExceptionis thrown ifstepSizeisnull.This method fires a ChangeEventif thestepSizehas changed.- Parameters:
- stepSize- the size of the value change computed by the- getNextValueand- getPreviousValuemethods
- See Also:
- getNextValue(),- getPreviousValue(),- getStepSize(),- SpinnerModel.addChangeListener(javax.swing.event.ChangeListener)
 
- 
getStepSizeReturns the size of the value change computed by thegetNextValueandgetPreviousValuemethods.- Returns:
- the value of the stepSizeproperty
- See Also:
- setStepSize(java.lang.Number)
 
- 
getNextValueReturns the next number in the sequence.- Specified by:
- getNextValuein interface- SpinnerModel
- Returns:
- value + stepSizeor- nullif the sum exceeds- maximum.
- See Also:
- SpinnerModel.getNextValue(),- getPreviousValue(),- setStepSize(java.lang.Number)
 
- 
getPreviousValueReturns the previous number in the sequence.- Specified by:
- getPreviousValuein interface- SpinnerModel
- Returns:
- value - stepSize, or- nullif the sum is less than- minimum.
- See Also:
- SpinnerModel.getPreviousValue(),- getNextValue(),- setStepSize(java.lang.Number)
 
- 
getNumberReturns the value of the current element of the sequence.- Returns:
- the value property
- See Also:
- setValue(java.lang.Object)
 
- 
getValueReturns the value of the current element of the sequence.- Specified by:
- getValuein interface- SpinnerModel
- Returns:
- the value property
- See Also:
- setValue(java.lang.Object),- getNumber()
 
- 
setValueSets the current value for this sequence. Ifvalueisnull, or not aNumber, anIllegalArgumentExceptionis thrown. No bounds checking is done here; the new value may invalidate the(minimum <= value <= maximum)invariant enforced by the constructors. It's also possible to set the value to be something that wouldn't naturally occur in the sequence, i.e. a value that's not modulo thestepSize. This is to simplify updating the model, and to accommodate spinners that don't want to restrict values that have been directly entered by the user. Naturally, one should ensure that the(minimum <= value <= maximum)invariant is true before calling thenext,previous, orsetValuemethods.This method fires a ChangeEventif the value has changed.- Specified by:
- setValuein interface- SpinnerModel
- Parameters:
- value- the current (non- null)- Numberfor this sequence
- Throws:
- IllegalArgumentException- if- valueis- nullor not a- Number
- See Also:
- getNumber(),- getValue(),- SpinnerModel.addChangeListener(javax.swing.event.ChangeListener)
 
 
-