- java.lang.Object
-
- javax.swing.AbstractSpinnerModel
-
- javax.swing.SpinnerNumberModel
-
- All Implemented Interfaces:
- Serializable, SpinnerModel
public class SpinnerNumberModel extends AbstractSpinnerModel implements Serializable
ASpinnerModel
for sequences of numbers. The upper and lower bounds of the sequence are defined by properties calledminimum
andmaximum
. The size of the increase or decrease computed by thenextValue
andpreviousValue
methods is defined by a property calledstepSize
. Theminimum
andmaximum
properties can benull
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
andComparable
, so that all Java numeric types may be accommodated. Internally, there's only support for values whose type is one of the primitiveNumber
types:Double
,Float
,Long
,Integer
,Short
, orByte
.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
. TheChangeListeners
are notified whenever the model'svalue
,stepSize
,minimum
, ormaximum
properties changes.- Since:
- 1.4
- See Also:
JSpinner
,SpinnerModel
,AbstractSpinnerModel
,SpinnerListModel
,SpinnerDateModel
-
-
Field Summary
-
Fields inherited from class javax.swing.AbstractSpinnerModel
listenerList
-
-
Constructor Summary
Constructors Constructor and Description SpinnerNumberModel()
Constructs aSpinnerNumberModel
with nominimum
ormaximum
value,stepSize
equal to one, and an initial value of zero.SpinnerNumberModel(double value, double minimum, double maximum, double stepSize)
Constructs aSpinnerNumberModel
with the specifiedvalue
,minimum
/maximum
bounds, andstepSize
.SpinnerNumberModel(int value, int minimum, int maximum, int stepSize)
Constructs aSpinnerNumberModel
with the specifiedvalue
,minimum
/maximum
bounds, andstepSize
.SpinnerNumberModel(Number value, Comparable minimum, Comparable maximum, Number stepSize)
Constructs aSpinnerModel
that represents a closed sequence of numbers fromminimum
tomaximum
.
-
Method Summary
Methods Modifier and Type Method and Description Comparable
getMaximum()
Returns the last number in the sequence.Comparable
getMinimum()
Returns the first number in this sequence.Object
getNextValue()
Returns the next number in the sequence.Number
getNumber()
Returns the value of the current element of the sequence.Object
getPreviousValue()
Returns the previous number in the sequence.Number
getStepSize()
Returns the size of the value change computed by thegetNextValue
andgetPreviousValue
methods.Object
getValue()
Returns the value of the current element of the sequence.void
setMaximum(Comparable maximum)
Changes the upper bound for numbers in this sequence.void
setMinimum(Comparable minimum)
Changes the lower bound for numbers in this sequence.void
setStepSize(Number stepSize)
Changes the size of the value change computed by thegetNextValue
andgetPreviousValue
methods.void
setValue(Object value)
Sets the current value for this sequence.-
Methods inherited from class javax.swing.AbstractSpinnerModel
addChangeListener, fireStateChanged, getChangeListeners, getListeners, removeChangeListener
-
-
-
-
Constructor Detail
-
SpinnerNumberModel
public SpinnerNumberModel(Number value, Comparable minimum, Comparable maximum, Number stepSize)
Constructs aSpinnerModel
that represents a closed sequence of numbers fromminimum
tomaximum
. ThenextValue
andpreviousValue
methods compute elements of the sequence by adding or subtractingstepSize
respectively. All of the parameters must be mutuallyComparable
,value
andstepSize
must be instances ofInteger
Long
,Float
, orDouble
.The
minimum
andmaximum
parameters can benull
to indicate that the range doesn't have an upper or lower bound. Ifvalue
orstepSize
isnull
, or if bothminimum
andmaximum
are specified andmininum > maximum
then anIllegalArgumentException
is thrown. Similarly if(minimum <= value <= maximum
) is false, anIllegalArgumentException
is thrown.- Parameters:
value
- the current (nonnull
) value of the modelminimum
- the first number in the sequence ornull
maximum
- the last number in the sequence ornull
stepSize
- the difference between elements of the sequence- Throws:
IllegalArgumentException
- if stepSize or value isnull
or if the following expression is false:minimum <= value <= maximum
-
SpinnerNumberModel
public SpinnerNumberModel(int value, int minimum, int maximum, int stepSize)
Constructs aSpinnerNumberModel
with the specifiedvalue
,minimum
/maximum
bounds, andstepSize
.- Parameters:
value
- the current value of the modelminimum
- the first number in the sequencemaximum
- the last number in the sequencestepSize
- the difference between elements of the sequence- Throws:
IllegalArgumentException
- if the following expression is false:minimum <= value <= maximum
-
SpinnerNumberModel
public SpinnerNumberModel(double value, double minimum, double maximum, double stepSize)
Constructs aSpinnerNumberModel
with the specifiedvalue
,minimum
/maximum
bounds, andstepSize
.- Parameters:
value
- the current value of the modelminimum
- the first number in the sequencemaximum
- the last number in the sequencestepSize
- the difference between elements of the sequence- Throws:
IllegalArgumentException
- if the following expression is false:minimum <= value <= maximum
-
SpinnerNumberModel
public SpinnerNumberModel()
Constructs aSpinnerNumberModel
with nominimum
ormaximum
value,stepSize
equal to one, and an initial value of zero.
-
-
Method Detail
-
setMinimum
public void setMinimum(Comparable minimum)
Changes the lower bound for numbers in this sequence. Ifminimum
isnull
, then there is no lower bound. No bounds checking is done here; the newminimum
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 thegetNextValue
,getPreviousValue
, orsetValue
methods.Typically this property is a
Number
of the same type as thevalue
however it's possible to use anyComparable
with acompareTo
method for aNumber
with the same type as the value. For example if value was aLong
,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 theminimum
has changed.- Parameters:
minimum
- aComparable
that has acompareTo
method forNumber
s with the same type asvalue
- See Also:
getMinimum()
,setMaximum(java.lang.Comparable)
,SpinnerModel.addChangeListener(javax.swing.event.ChangeListener)
-
getMinimum
public Comparable getMinimum()
Returns the first number in this sequence.- Returns:
- the value of the
minimum
property - See Also:
setMinimum(java.lang.Comparable)
-
setMaximum
public void setMaximum(Comparable maximum)
Changes the upper bound for numbers in this sequence. Ifmaximum
isnull
, then there is no upper bound. No bounds checking is done here; the newmaximum
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 thenext
,previous
, orsetValue
methods.Typically this property is a
Number
of the same type as thevalue
however it's possible to use anyComparable
with acompareTo
method for aNumber
with the same type as the value. SeesetMinimum
for an example.This method fires a
ChangeEvent
if themaximum
has changed.- Parameters:
maximum
- aComparable
that has acompareTo
method forNumber
s with the same type asvalue
- See Also:
getMaximum()
,setMinimum(java.lang.Comparable)
,SpinnerModel.addChangeListener(javax.swing.event.ChangeListener)
-
getMaximum
public Comparable getMaximum()
Returns the last number in the sequence.- Returns:
- the value of the
maximum
property - See Also:
setMaximum(java.lang.Comparable)
-
setStepSize
public void setStepSize(Number stepSize)
Changes the size of the value change computed by thegetNextValue
andgetPreviousValue
methods. AnIllegalArgumentException
is thrown ifstepSize
isnull
.This method fires a
ChangeEvent
if thestepSize
has changed.- Parameters:
stepSize
- the size of the value change computed by thegetNextValue
andgetPreviousValue
methods- See Also:
getNextValue()
,getPreviousValue()
,getStepSize()
,SpinnerModel.addChangeListener(javax.swing.event.ChangeListener)
-
getStepSize
public Number getStepSize()
Returns the size of the value change computed by thegetNextValue
andgetPreviousValue
methods.- Returns:
- the value of the
stepSize
property - See Also:
setStepSize(java.lang.Number)
-
getNextValue
public Object getNextValue()
Returns the next number in the sequence.- Specified by:
getNextValue
in interfaceSpinnerModel
- Returns:
value + stepSize
ornull
if the sum exceedsmaximum
.- See Also:
SpinnerModel.getNextValue()
,getPreviousValue()
,setStepSize(java.lang.Number)
-
getPreviousValue
public Object getPreviousValue()
Returns the previous number in the sequence.- Specified by:
getPreviousValue
in interfaceSpinnerModel
- Returns:
value - stepSize
, ornull
if the sum is less thanminimum
.- See Also:
SpinnerModel.getPreviousValue()
,getNextValue()
,setStepSize(java.lang.Number)
-
getNumber
public Number getNumber()
Returns the value of the current element of the sequence.- Returns:
- the value property
- See Also:
setValue(java.lang.Object)
-
getValue
public Object getValue()
Returns the value of the current element of the sequence.- Specified by:
getValue
in interfaceSpinnerModel
- Returns:
- the value property
- See Also:
setValue(java.lang.Object)
,getNumber()
-
setValue
public void setValue(Object value)
Sets the current value for this sequence. Ifvalue
isnull
, or not aNumber
, anIllegalArgumentException
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 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
, orsetValue
methods.This method fires a
ChangeEvent
if the value has changed.- Specified by:
setValue
in interfaceSpinnerModel
- Parameters:
value
- the current (nonnull
)Number
for this sequence- Throws:
IllegalArgumentException
- ifvalue
isnull
or not aNumber
- See Also:
getNumber()
,getValue()
,SpinnerModel.addChangeListener(javax.swing.event.ChangeListener)
-
-
Traduction non disponible
Les API Java ne sont pas encore traduites en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Version en cache
05/11/2024 17:20:57 Cette version de la page est en cache (à la date du 05/11/2024 17:20:57) afin d'accélérer le traitement. Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la dernère version de la page.Document créé le 31/08/2006, dernière modification le 04/03/2020
Source du document imprimé : https://www.gaudry.be/java-api-rf-javax/swing/SpinnerNumberModel.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.