- All Implemented Interfaces:
Serializable,SpinnerModel
SpinnerModel for sequences of Dates.
The upper and lower bounds of the sequence are defined by properties called
start and end and the size
of the increase or decrease computed by the nextValue
and previousValue methods is defined by a property
called calendarField. The start
and end properties can be null to
indicate that the sequence has no lower or upper limit.
The value of the calendarField property must be one of the
java.util.Calendar constants that specify a field
within a Calendar. The getNextValue
and getPreviousValue
methods change the date forward or backwards by this amount.
For example, if calendarField is Calendar.DAY_OF_WEEK,
then nextValue produces a Date that's 24
hours after the current value, and previousValue
produces a Date that's 24 hours earlier.
The legal values for calendarField are:
Calendar.ERACalendar.YEARCalendar.MONTHCalendar.WEEK_OF_YEARCalendar.WEEK_OF_MONTHCalendar.DAY_OF_MONTHCalendar.DAY_OF_YEARCalendar.DAY_OF_WEEKCalendar.DAY_OF_WEEK_IN_MONTHCalendar.AM_PMCalendar.HOURCalendar.HOUR_OF_DAYCalendar.MINUTECalendar.SECONDCalendar.MILLISECOND
This model inherits a ChangeListener. The
ChangeListeners are notified whenever the models
value, calendarField,
start, or end properties changes.
- Since:
- 1.4
- See Also:
-
Field Summary
Fields declared in class javax.swing.AbstractSpinnerModel
listenerList -
Constructor Summary
ConstructorsConstructorDescriptionConstructs aSpinnerDateModelwhose initialvalueis the current date,calendarFieldis equal toCalendar.DAY_OF_MONTH, and for which there are nostart/endlimits.SpinnerDateModel(Date value, Comparable<Date> start, Comparable<Date> end, int calendarField) Creates aSpinnerDateModelthat represents a sequence of dates betweenstartandend. -
Method Summary
Modifier and TypeMethodDescriptionintReturns theCalendarfield that is added to or subtracted from by thenextValueandpreviousValuemethods.getDate()Returns the current element in this sequence ofDates.getEnd()Returns the lastDatein the sequence.Returns the nextDatein the sequence, ornullif the next date is afterend.Returns the previousDatein the sequence, ornullif the previous date is beforestart.getStart()Returns the firstDatein the sequence.getValue()Returns the current element in this sequence ofDates.voidsetCalendarField(int calendarField) Changes the size of the date value change computed by thenextValueandpreviousValuemethods.voidsetEnd(Comparable<Date> end) Changes the upper limit forDates in this sequence.voidsetStart(Comparable<Date> start) Changes the lower limit for Dates in this sequence.voidSets the currentDatefor this sequence.Methods declared in class javax.swing.AbstractSpinnerModel
addChangeListener, fireStateChanged, getChangeListeners, getListeners, removeChangeListener
-
Constructor Details
-
SpinnerDateModel
public SpinnerDateModel(Date value, Comparable<Date> start, Comparable<Date> end, int calendarField) Creates aSpinnerDateModelthat represents a sequence of dates betweenstartandend. ThenextValueandpreviousValuemethods compute elements of the sequence by advancing or reversing the current datevalueby thecalendarFieldtime unit. For a precise description of what it means to increment or decrement aCalendarfield, see theaddmethod injava.util.Calendar.The
startandendparameters can benullto indicate that the range doesn't have an upper or lower bound. IfvalueorcalendarFieldisnull, or if bothstartandendare specified andminimum > maximumthen anIllegalArgumentExceptionis 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 ornullend- the last date in the sequence ornullcalendarField- one ofCalendar.ERACalendar.YEARCalendar.MONTHCalendar.WEEK_OF_YEARCalendar.WEEK_OF_MONTHCalendar.DAY_OF_MONTHCalendar.DAY_OF_YEARCalendar.DAY_OF_WEEKCalendar.DAY_OF_WEEK_IN_MONTHCalendar.AM_PMCalendar.HOURCalendar.HOUR_OF_DAYCalendar.MINUTECalendar.SECONDCalendar.MILLISECOND
- Throws:
IllegalArgumentException- ifvalueorcalendarFieldarenull, ifcalendarFieldisn't valid, or if the following expression is false:(start <= value <= end).- See Also:
-
SpinnerDateModel
public SpinnerDateModel()Constructs aSpinnerDateModelwhose initialvalueis the current date,calendarFieldis equal toCalendar.DAY_OF_MONTH, and for which there are nostart/endlimits.
-
-
Method Details
-
setStart
Changes the lower limit for Dates in this sequence. Ifstartisnull, 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, orsetValuemethods.Typically this property is a
Datehowever it's possible to use aComparablewith acompareTomethod for Dates. For examplestartmight 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 aClassCastExceptionif theObjectpassed tocompareTo(Object)is not aDate.This method fires a
ChangeEventif thestarthas changed.- Parameters:
start- defines the first date in the sequence- See Also:
-
getStart
Returns the firstDatein the sequence.- Returns:
- the value of the
startproperty - See Also:
-
setEnd
Changes the upper limit forDates in this sequence. Ifstartisnull, 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, orsetValuemethods.Typically this property is a
Datehowever it's possible to useComparablewith acompareTomethod forDates. SeesetStartfor an example.This method fires a
ChangeEventif theendhas changed.- Parameters:
end- defines the last date in the sequence- See Also:
-
getEnd
Returns the lastDatein the sequence.- Returns:
- the value of the
endproperty - See Also:
-
setCalendarField
public void setCalendarField(int calendarField) Changes the size of the date value change computed by thenextValueandpreviousValuemethods. ThecalendarFieldparameter must be one of theCalendarfield constants likeCalendar.MONTHorCalendar.MINUTE. ThenextValueandpreviousValuemethods simply move the specifiedCalendarfield forward or backward by one unit with theCalendar.addmethod. You should use this method with care as some UIs may set the calendarField before committing 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.ERACalendar.YEARCalendar.MONTHCalendar.WEEK_OF_YEARCalendar.WEEK_OF_MONTHCalendar.DAY_OF_MONTHCalendar.DAY_OF_YEARCalendar.DAY_OF_WEEKCalendar.DAY_OF_WEEK_IN_MONTHCalendar.AM_PMCalendar.HOURCalendar.HOUR_OF_DAYCalendar.MINUTECalendar.SECONDCalendar.MILLISECOND
This method fires a
ChangeEventif thecalendarFieldhas changed.- See Also:
-
getCalendarField
public int getCalendarField()Returns theCalendarfield that is added to or subtracted from by thenextValueandpreviousValuemethods.- Returns:
- the value of the
calendarFieldproperty - See Also:
-
getNextValue
Returns the nextDatein the sequence, ornullif the next date is afterend.- Specified by:
getNextValuein interfaceSpinnerModel- Returns:
- the next
Datein the sequence, ornullif the next date is afterend. - See Also:
-
getPreviousValue
Returns the previousDatein the sequence, ornullif the previous date is beforestart.- Specified by:
getPreviousValuein interfaceSpinnerModel- Returns:
- the previous
Datein the sequence, ornullif the previous date is beforestart - See Also:
-
getDate
Returns the current element in this sequence ofDates. This method is equivalent to(Date)getValue.- Returns:
- the
valueproperty - See Also:
-
getValue
Returns the current element in this sequence ofDates.- Specified by:
getValuein interfaceSpinnerModel- Returns:
- the
valueproperty - See Also:
-
setValue
Sets the currentDatefor this sequence. Ifvalueisnull, anIllegalArgumentExceptionis 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, orsetValuemethods.This method fires a
ChangeEventif thevaluehas changed.- Specified by:
setValuein interfaceSpinnerModel- Parameters:
value- the current (nonnull)Datefor this sequence- Throws:
IllegalArgumentException- if value isnullor not aDate- See Also:
-