- java.lang.Object
-
- javax.swing.AbstractSpinnerModel
-
- javax.swing.SpinnerDateModel
-
- All Implemented Interfaces:
- Serializable, SpinnerModel
public class SpinnerDateModel extends AbstractSpinnerModel implements Serializable
ASpinnerModel
for sequences ofDate
s. The upper and lower bounds of the sequence are defined by properties calledstart
andend
and the size of the increase or decrease computed by thenextValue
andpreviousValue
methods is defined by a property calledcalendarField
. Thestart
andend
properties can benull
to indicate that the sequence has no lower or upper limit.The value of the
calendarField
property must be one of thejava.util.Calendar
constants that specify a field within aCalendar
. ThegetNextValue
andgetPreviousValue
methods change the date forward or backwards by this amount. For example, ifcalendarField
isCalendar.DAY_OF_WEEK
, thennextValue
produces aDate
that's 24 hours after the currentvalue
, andpreviousValue
produces aDate
that's 24 hours earlier.The legal values for
calendarField
are:Calendar.ERA
Calendar.YEAR
Calendar.MONTH
Calendar.WEEK_OF_YEAR
Calendar.WEEK_OF_MONTH
Calendar.DAY_OF_MONTH
Calendar.DAY_OF_YEAR
Calendar.DAY_OF_WEEK
Calendar.DAY_OF_WEEK_IN_MONTH
Calendar.AM_PM
Calendar.HOUR
Calendar.HOUR_OF_DAY
Calendar.MINUTE
Calendar.SECOND
Calendar.MILLISECOND
This model inherits a
ChangeListener
. TheChangeListeners
are notified whenever the modelsvalue
,calendarField
,start
, orend
properties changes.- Since:
- 1.4
- See Also:
JSpinner
,SpinnerModel
,AbstractSpinnerModel
,SpinnerListModel
,SpinnerNumberModel
,Calendar.add(int, int)
-
-
Field Summary
-
Fields inherited from class javax.swing.AbstractSpinnerModel
listenerList
-
-
Constructor Summary
Constructors Constructor and Description SpinnerDateModel()
Constructs aSpinnerDateModel
whose initialvalue
is the current date,calendarField
is equal toCalendar.DAY_OF_MONTH
, and for which there are nostart
/end
limits.SpinnerDateModel(Date value, Comparable start, Comparable end, int calendarField)
Creates aSpinnerDateModel
that represents a sequence of dates betweenstart
andend
.
-
Method Summary
Methods Modifier and Type Method and Description int
getCalendarField()
Returns theCalendar
field that is added to or subtracted from by thenextValue
andpreviousValue
methods.Date
getDate()
Returns the current element in this sequence ofDate
s.Comparable
getEnd()
Returns the lastDate
in the sequence.Object
getNextValue()
Returns the nextDate
in the sequence, ornull
if the next date is afterend
.Object
getPreviousValue()
Returns the previousDate
in the sequence, ornull
if the previous date is beforestart
.Comparable
getStart()
Returns the firstDate
in the sequence.Object
getValue()
Returns the current element in this sequence ofDate
s.void
setCalendarField(int calendarField)
Changes the size of the date value change computed by thenextValue
andpreviousValue
methods.void
setEnd(Comparable end)
Changes the upper limit forDate
s in this sequence.void
setStart(Comparable start)
Changes the lower limit for Dates in this sequence.void
setValue(Object value)
Sets the currentDate
for this sequence.-
Methods inherited from class javax.swing.AbstractSpinnerModel
addChangeListener, fireStateChanged, getChangeListeners, getListeners, removeChangeListener
-
-
-
-
Constructor Detail
-
SpinnerDateModel
public SpinnerDateModel(Date value, Comparable start, Comparable end, int calendarField)
Creates aSpinnerDateModel
that represents a sequence of dates betweenstart
andend
. ThenextValue
andpreviousValue
methods compute elements of the sequence by advancing or reversing the current datevalue
by thecalendarField
time unit. For a precise description of what it means to increment or decrement aCalendar
field
, see theadd
method injava.util.Calendar
.The
start
andend
parameters can benull
to indicate that the range doesn't have an upper or lower bound. Ifvalue
orcalendarField
isnull
, or if bothstart
andend
are specified andmininum > maximum
then anIllegalArgumentException
is thrown. Similarly if(minimum <= value <= maximum)
is false, an IllegalArgumentException is thrown.- Parameters:
value
- the current (nonnull
) value of the modelstart
- the first date in the sequence ornull
end
- the last date in the sequence ornull
calendarField
- one ofCalendar.ERA
Calendar.YEAR
Calendar.MONTH
Calendar.WEEK_OF_YEAR
Calendar.WEEK_OF_MONTH
Calendar.DAY_OF_MONTH
Calendar.DAY_OF_YEAR
Calendar.DAY_OF_WEEK
Calendar.DAY_OF_WEEK_IN_MONTH
Calendar.AM_PM
Calendar.HOUR
Calendar.HOUR_OF_DAY
Calendar.MINUTE
Calendar.SECOND
Calendar.MILLISECOND
- Throws:
IllegalArgumentException
- ifvalue
orcalendarField
arenull
, ifcalendarField
isn't valid, or if the following expression is false:(start <= value <= end)
.- See Also:
Calendar.add(int, int)
,setValue(java.lang.Object)
,setStart(java.lang.Comparable)
,setEnd(java.lang.Comparable)
,setCalendarField(int)
-
SpinnerDateModel
public SpinnerDateModel()
Constructs aSpinnerDateModel
whose initialvalue
is the current date,calendarField
is equal toCalendar.DAY_OF_MONTH
, and for which there are nostart
/end
limits.
-
-
Method Detail
-
setStart
public void setStart(Comparable start)
Changes the lower limit for Dates in this sequence. Ifstart
isnull
, then there is no lower limit. No bounds checking is done here: the new start value may invalidate the(start <= value <= end)
invariant enforced by the constructors. This is to simplify updating the model. Naturally one should ensure that the invariant is true before calling thenextValue
,previousValue
, orsetValue
methods.Typically this property is a
Date
however it's possible to use aComparable
with acompareTo
method for Dates. For examplestart
might be an instance of a class like this:MyStartDate implements Comparable { long t = 12345; public int compareTo(Date d) { return (t < d.getTime() ? -1 : (t == d.getTime() ? 0 : 1)); } public int compareTo(Object o) { return compareTo((Date)o); } }
Note that the above example will throw aClassCastException
if theObject
passed tocompareTo(Object)
is not aDate
.This method fires a
ChangeEvent
if thestart
has changed.- Parameters:
start
- defines the first date in the sequence- See Also:
getStart()
,setEnd(java.lang.Comparable)
,AbstractSpinnerModel.addChangeListener(javax.swing.event.ChangeListener)
-
getStart
public Comparable getStart()
Returns the firstDate
in the sequence.- Returns:
- the value of the
start
property - See Also:
setStart(java.lang.Comparable)
-
setEnd
public void setEnd(Comparable end)
Changes the upper limit forDate
s in this sequence. Ifstart
isnull
, then there is no upper limit. No bounds checking is done here: the new start value may invalidate the(start <= value <= end)
invariant enforced by the constructors. This is to simplify updating the model. Naturally, one should ensure that the invariant is true before calling thenextValue
,previousValue
, orsetValue
methods.Typically this property is a
Date
however it's possible to useComparable
with acompareTo
method forDate
s. SeesetStart
for an example.This method fires a
ChangeEvent
if theend
has changed.- Parameters:
end
- defines the last date in the sequence- See Also:
getEnd()
,setStart(java.lang.Comparable)
,AbstractSpinnerModel.addChangeListener(javax.swing.event.ChangeListener)
-
getEnd
public Comparable getEnd()
Returns the lastDate
in the sequence.- Returns:
- the value of the
end
property - See Also:
setEnd(java.lang.Comparable)
-
setCalendarField
public void setCalendarField(int calendarField)
Changes the size of the date value change computed by thenextValue
andpreviousValue
methods. ThecalendarField
parameter must be one of theCalendar
field constants likeCalendar.MONTH
orCalendar.MINUTE
. ThenextValue
andpreviousValue
methods simply move the specifiedCalendar
field forward or backward by one unit with theCalendar.add
method. You should use this method with care as some UIs may set the calendarField before commiting the edit to spin the field under the cursor. If you only want one field to spin you can subclass and ignore the setCalendarField calls.- Parameters:
calendarField
- one ofCalendar.ERA
Calendar.YEAR
Calendar.MONTH
Calendar.WEEK_OF_YEAR
Calendar.WEEK_OF_MONTH
Calendar.DAY_OF_MONTH
Calendar.DAY_OF_YEAR
Calendar.DAY_OF_WEEK
Calendar.DAY_OF_WEEK_IN_MONTH
Calendar.AM_PM
Calendar.HOUR
Calendar.HOUR_OF_DAY
Calendar.MINUTE
Calendar.SECOND
Calendar.MILLISECOND
This method fires a
ChangeEvent
if thecalendarField
has changed.- See Also:
getCalendarField()
,getNextValue()
,getPreviousValue()
,Calendar.add(int, int)
,AbstractSpinnerModel.addChangeListener(javax.swing.event.ChangeListener)
-
getCalendarField
public int getCalendarField()
Returns theCalendar
field that is added to or subtracted from by thenextValue
andpreviousValue
methods.- Returns:
- the value of the
calendarField
property - See Also:
setCalendarField(int)
-
getNextValue
public Object getNextValue()
Returns the nextDate
in the sequence, ornull
if the next date is afterend
.- Specified by:
getNextValue
in interfaceSpinnerModel
- Returns:
- the next
Date
in the sequence, ornull
if the next date is afterend
. - See Also:
SpinnerModel.getNextValue()
,getPreviousValue()
,setCalendarField(int)
-
getPreviousValue
public Object getPreviousValue()
Returns the previousDate
in the sequence, ornull
if the previous date is beforestart
.- Specified by:
getPreviousValue
in interfaceSpinnerModel
- Returns:
- the previous
Date
in the sequence, ornull
if the previous date is beforestart
- See Also:
SpinnerModel.getPreviousValue()
,getNextValue()
,setCalendarField(int)
-
getDate
public Date getDate()
Returns the current element in this sequence ofDate
s. This method is equivalent to(Date)getValue
.- Returns:
- the
value
property - See Also:
setValue(java.lang.Object)
-
getValue
public Object getValue()
Returns the current element in this sequence ofDate
s.- Specified by:
getValue
in interfaceSpinnerModel
- Returns:
- the
value
property - See Also:
setValue(java.lang.Object)
,getDate()
-
setValue
public void setValue(Object value)
Sets the currentDate
for this sequence. Ifvalue
isnull
, anIllegalArgumentException
is thrown. No bounds checking is done here: the new value may invalidate the(start <= value < end)
invariant enforced by the constructors. Naturally, one should ensure that the(start <= value <= maximum)
invariant is true before calling thenextValue
,previousValue
, orsetValue
methods.This method fires a
ChangeEvent
if thevalue
has changed.- Specified by:
setValue
in interfaceSpinnerModel
- Parameters:
value
- the current (nonnull
)Date
for this sequence- Throws:
IllegalArgumentException
- if value isnull
or not aDate
- See Also:
getDate()
,getValue()
,AbstractSpinnerModel.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 16:38:16 Cette version de la page est en cache (à la date du 05/11/2024 16:38:16) 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 29/08/2006, dernière modification le 04/03/2020
Source du document imprimé : https://www.gaudry.be/java-api-rf-javax/swing/SpinnerDateModel.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.