FileDocCategorySizeDatePackage
SpinnerNumberModel.javaAPI DocJava SE 5 API15389Fri Aug 26 14:57:58 BST 2005javax.swing

SpinnerNumberModel

public class SpinnerNumberModel extends AbstractSpinnerModel implements Serializable
A 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 = new Integer(50);
Integer min = new Integer(0);
Integer max = new Integer(100);
Integer step = new Integer(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.

see
JSpinner
see
SpinnerModel
see
AbstractSpinnerModel
see
SpinnerListModel
see
SpinnerDateModel
version
1.12 05/12/04
author
Hans Muller
since
1.4

Fields Summary
private Number
stepSize
private Number
value
private Comparable
minimum
private Comparable
maximum
Constructors Summary
public SpinnerNumberModel(Number value, Comparable minimum, Comparable maximum, Number stepSize)
Constructs a SpinnerModel that represents a closed sequence of numbers from minimum to maximum. The nextValue and previousValue methods compute elements of the sequence by adding or subtracting stepSize respectively. All of the parameters must be mutually Comparable, value and stepSize must be instances of Integer Long, Float, or Double.

The minimum and maximum parameters can be null to indicate that the range doesn't have an upper or lower bound. If value or stepSize is null, or if both minimum and maximum are specified and mininum > maximum then an IllegalArgumentException is thrown. Similarly if (minimum <= value <= maximum) is false, an IllegalArgumentException is thrown.

param
value the current (non null) value of the model
param
minimum the first number in the sequence or null
param
maximum the last number in the sequence or null
param
stepSize the difference between elements of the sequence
throws
IllegalArgumentException if stepSize or value is null or if the following expression is false: minimum <= value <= maximum

	if ((value == null) || (stepSize == null)) {
	    throw new IllegalArgumentException("value and stepSize must be non-null");
	}
	if (!(((minimum == null) || (minimum.compareTo(value) <= 0)) && 
	      ((maximum == null) || (maximum.compareTo(value) >= 0)))) {
	    throw new IllegalArgumentException("(minimum <= value <= maximum) is false");
	}
	this.value = value;
	this.minimum = minimum;
	this.maximum = maximum;
	this.stepSize = stepSize;
    
public SpinnerNumberModel(int value, int minimum, int maximum, int stepSize)
Constructs a SpinnerNumberModel with the specified value, minimum/maximum bounds, and stepSize.

param
value the current value of the model
param
minimum the first number in the sequence
param
maximum the last number in the sequence
param
stepSize the difference between elements of the sequence
throws
IllegalArgumentException if the following expression is false: minimum <= value <= maximum

	this(new Integer(value), new Integer(minimum), new Integer(maximum), new Integer(stepSize));
    
public SpinnerNumberModel(double value, double minimum, double maximum, double stepSize)
Constructs a SpinnerNumberModel with the specified value, minimum/maximum bounds, and stepSize.

param
value the current value of the model
param
minimum the first number in the sequence
param
maximum the last number in the sequence
param
stepSize the difference between elements of the sequence
throws
IllegalArgumentException if the following expression is false: minimum <= value <= maximum

	this(new Double(value), new Double(minimum), new Double(maximum), new Double(stepSize));
    
public SpinnerNumberModel()
Constructs a SpinnerNumberModel with no minimum or maximum value, stepSize equal to one, and an initial value of zero.

	this(new Integer(0), null, null, new Integer(1));
    
Methods Summary
public java.lang.ComparablegetMaximum()
Returns the last number in the sequence.

return
the value of the maximum property
see
#setMaximum

	return maximum;
    
public java.lang.ComparablegetMinimum()
Returns the first number in this sequence.

return
the value of the minimum property
see
#setMinimum

	return minimum;
    
public java.lang.ObjectgetNextValue()
Returns the next number in the sequence.

return
value + stepSize or null if the sum exceeds maximum.
see
SpinnerModel#getNextValue
see
#getPreviousValue
see
#setStepSize

	return incrValue(+1);
    
public java.lang.NumbergetNumber()
Returns the value of the current element of the sequence.

return
the value property
see
#setValue

	return value;
    
public java.lang.ObjectgetPreviousValue()
Returns the previous number in the sequence.

return
value - stepSize, or null if the sum is less than minimum.
see
SpinnerModel#getPreviousValue
see
#getNextValue
see
#setStepSize

	return incrValue(-1);
    
public java.lang.NumbergetStepSize()
Returns the size of the value change computed by the getNextValue and getPreviousValue methods.

return
the value of the stepSize property
see
#setStepSize

	return stepSize;
    
public java.lang.ObjectgetValue()
Returns the value of the current element of the sequence.

return
the value property
see
#setValue
see
#getNumber

	return value;
    
private java.lang.NumberincrValue(int dir)

	Number newValue;
	if ((value instanceof Float) || (value instanceof Double)) {
	    double v = value.doubleValue() + (stepSize.doubleValue() * (double)dir);
	    if (value instanceof Double) {
		newValue = new Double(v);
	    }
	    else {
		newValue = new Float(v);
	    }
	}
	else {
	    long v = value.longValue() + (stepSize.longValue() * (long)dir);

	    if (value instanceof Long) {
		newValue = new Long(v);
	    }
	    else if (value instanceof Integer) {
		newValue = new Integer((int)v);
	    }
	    else if (value instanceof Short) {
		newValue = new Short((short)v);
	    }
	    else {
		newValue = new Byte((byte)v);
	    }
	}

	if ((maximum != null) && (maximum.compareTo(newValue) < 0)) {
	    return null;
	}
	if ((minimum != null) && (minimum.compareTo(newValue) > 0)) {
	    return null;
	}
	else {
	    return newValue;
	}
    
public voidsetMaximum(java.lang.Comparable maximum)
Changes the upper bound for numbers in this sequence. If maximum is null, then there is no upper bound. No bounds checking is done here; the new maximum value 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 the next, previous, or setValue methods.

Typically this property is a Number of the same type as the value however it's possible to use any Comparable with a compareTo method for a Number with the same type as the value. See setMinimum for an example.

This method fires a ChangeEvent if the maximum has changed.

param
maximum a Comparable that has a compareTo method for Numbers with the same type as value
see
#getMaximum
see
#setMinimum
see
SpinnerModel#addChangeListener

	if ((maximum == null) ? (this.maximum != null) : !maximum.equals(this.maximum)) {
	    this.maximum = maximum;
	    fireStateChanged();
	}
    
public voidsetMinimum(java.lang.Comparable minimum)
Changes the lower bound for numbers in this sequence. If minimum is null, then there is no lower bound. No bounds checking is done here; the new minimum value 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 the getNextValue, getPreviousValue, or setValue methods.

Typically this property is a Number of the same type as the value however it's possible to use any Comparable with a compareTo method for a Number with the same type as the value. For example if value was a Long, minimum might 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 ChangeEvent if the minimum has changed.

param
minimum a Comparable that has a compareTo method for Numbers with the same type as value
see
#getMinimum
see
#setMaximum
see
SpinnerModel#addChangeListener

	if ((minimum == null) ? (this.minimum != null) : !minimum.equals(this.minimum)) {
	    this.minimum = minimum;
	    fireStateChanged();
	}
    
public voidsetStepSize(java.lang.Number stepSize)
Changes the size of the value change computed by the getNextValue and getPreviousValue methods. An IllegalArgumentException is thrown if stepSize is null.

This method fires a ChangeEvent if the stepSize has changed.

param
stepSize the size of the value change computed by the getNextValue and getPreviousValue methods
see
#getNextValue
see
#getPreviousValue
see
#getStepSize
see
SpinnerModel#addChangeListener

	if (stepSize == null) {
	    throw new IllegalArgumentException("null stepSize");
	}
	if (!stepSize.equals(this.stepSize)) {
	    this.stepSize = stepSize;
	    fireStateChanged();
	}
    
public voidsetValue(java.lang.Object value)
Sets the current value for this sequence. If value is null, or not a Number, an IllegalArgumentException is 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 the stepSize. 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 the next, previous, or setValue methods.

This method fires a ChangeEvent if the value has changed.

param
value the current (non null) Number for this sequence
throws
IllegalArgumentException if value is null or not a Number
see
#getNumber
see
#getValue
see
SpinnerModel#addChangeListener

	if ((value == null) || !(value instanceof Number)) {
	    throw new IllegalArgumentException("illegal value");
	}
	if (!value.equals(this.value)) {
	    this.value = (Number)value;
	    fireStateChanged();
	}