APIdia Launch Partner
Top Description Fields Constructors Methods
javax.swing

public Class SpinnerDateModel

extends AbstractSpinnerModel
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Annotations
@SuppressWarnings:serial
Imports
java.util.*, java.io.Serializable

A 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:

However 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.

This model inherits a ChangeListener. The ChangeListeners are notified whenever the models value, calendarField, start, or end properties changes.

Author
Hans Muller
Since
1.4
See Also
JSpinner, SpinnerModel, AbstractSpinnerModel, SpinnerListModel, SpinnerNumberModel, Calendar#add

Field Summary

Modifier and TypeField and Description
private int
private Comparable<Date>
private Comparable<Date>
private Calendar
Inherited from javax.swing.AbstractSpinnerModel:
listenerList

Constructor Summary

AccessConstructor and Description
public
SpinnerDateModel(Date
the current (non null) value of the model
value
,
Comparable<Date>
the first date in the sequence or null
start
,
Comparable<Date>
the last date in the sequence or null
end
,
int
one of
  • 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
calendarField
)

Creates a SpinnerDateModel that represents a sequence of dates between start and end.

public
SpinnerDateModel()

Constructs a SpinnerDateModel whose initial value is the current date, calendarField is equal to Calendar.DAY_OF_MONTH, and for which there are no start/end limits.

Method Summary

Modifier and TypeMethod and Description
private boolean
calendarFieldOK(int calendarField)

public int

Returns:

the value of the calendarField property
getCalendarField
()

Returns the Calendar field that is added to or subtracted from by the nextValue and previousValue methods.

public Date

Returns:

the value property
getDate
()

Returns the current element in this sequence of Dates.

public Comparable<Date>

Returns:

the value of the end property
getEnd
()

Returns the last Date in the sequence.

public Object

Returns:

the next Date in the sequence, or null if the next date is after end.
getNextValue
()

Implements javax.swing.SpinnerModel.getNextValue.

Returns the next Date in the sequence, or null if the next date is after end.

public Object

Returns:

the previous Date in the sequence, or null if the previous date is before start
getPreviousValue
()

Implements javax.swing.SpinnerModel.getPreviousValue.

Returns the previous Date in the sequence, or null if the previous date is before start.

public Comparable<Date>

Returns:

the value of the start property
getStart
()

Returns the first Date in the sequence.

public Object

Returns:

the value property
getValue
()

Implements javax.swing.SpinnerModel.getValue.

Returns the current element in this sequence of Dates.

public void
setCalendarField(int
one of
  • 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 method fires a ChangeEvent if the calendarField has changed.

calendarField
)

Changes the size of the date value change computed by the nextValue and previousValue methods.

public void
setEnd(Comparable<Date>
defines the last date in the sequence
end
)

Changes the upper limit for Dates in this sequence.

public void
setStart(Comparable<Date>
defines the first date in the sequence
start
)

Changes the lower limit for Dates in this sequence.

public void
setValue(Object
the current (non null) Date for this sequence
value
)

Implements javax.swing.SpinnerModel.setValue.

Sets the current Date for this sequence.

Inherited from javax.swing.AbstractSpinnerModel:
addChangeListenerfireStateChangedgetChangeListenersgetListenersremoveChangeListener

Field Detail

calendarFieldback to summary
private int calendarField
endback to summary
private Comparable<Date> end
startback to summary
private Comparable<Date> start
valueback to summary
private Calendar value

Constructor Detail

SpinnerDateModelback to summary
public SpinnerDateModel(Date value, Comparable<Date> start, Comparable<Date> end, int calendarField)

Creates a SpinnerDateModel that represents a sequence of dates between start and end. The nextValue and previousValue methods compute elements of the sequence by advancing or reversing the current date value by the calendarField time unit. For a precise description of what it means to increment or decrement a Calendar field, see the add method in java.util.Calendar.

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

Parameters
value:Date

the current (non null) value of the model

start:Comparable<Date>

the first date in the sequence or null

end:Comparable<Date>

the last date in the sequence or null

calendarField:int

one of

  • 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
Exceptions
IllegalArgumentException:
if value or calendarField are null, if calendarField isn't valid, or if the following expression is false: (start <= value <= end).
See Also
Calendar#add, setValue, setStart, setEnd, setCalendarField
SpinnerDateModelback to summary
public SpinnerDateModel()

Constructs a SpinnerDateModel whose initial value is the current date, calendarField is equal to Calendar.DAY_OF_MONTH, and for which there are no start/end limits.

Method Detail

calendarFieldOKback to summary
private boolean calendarFieldOK(int calendarField)
getCalendarFieldback to summary
public int getCalendarField()

Returns the Calendar field that is added to or subtracted from by the nextValue and previousValue methods.

Returns:int

the value of the calendarField property

See Also
setCalendarField
getDateback to summary
public Date getDate()

Returns the current element in this sequence of Dates. This method is equivalent to (Date)getValue.

Returns:Date

the value property

See Also
setValue
getEndback to summary
public Comparable<Date> getEnd()

Returns the last Date in the sequence.

Returns:Comparable<Date>

the value of the end property

See Also
setEnd
getNextValueback to summary
public Object getNextValue()

Implements javax.swing.SpinnerModel.getNextValue.

Returns the next Date in the sequence, or null if the next date is after end.

Returns:Object

the next Date in the sequence, or null if the next date is after end.

See Also
SpinnerModel#getNextValue, getPreviousValue, setCalendarField
getPreviousValueback to summary
public Object getPreviousValue()

Implements javax.swing.SpinnerModel.getPreviousValue.

Returns the previous Date in the sequence, or null if the previous date is before start.

Returns:Object

the previous Date in the sequence, or null if the previous date is before start

See Also
SpinnerModel#getPreviousValue, getNextValue, setCalendarField
getStartback to summary
public Comparable<Date> getStart()

Returns the first Date in the sequence.

Returns:Comparable<Date>

the value of the start property

See Also
setStart
getValueback to summary
public Object getValue()

Implements javax.swing.SpinnerModel.getValue.

Returns the current element in this sequence of Dates.

Returns:Object

the value property

See Also
setValue, getDate
setCalendarFieldback to summary
public void setCalendarField(int calendarField)

Changes the size of the date value change computed by the nextValue and previousValue methods. The calendarField parameter must be one of the Calendar field constants like Calendar.MONTH or Calendar.MINUTE. The nextValue and previousValue methods simply move the specified Calendar field forward or backward by one unit with the Calendar.add method. 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:int

one of

  • 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 method fires a ChangeEvent if the calendarField has changed.

See Also
getCalendarField, getNextValue, getPreviousValue, Calendar#add, addChangeListener
setEndback to summary
public void setEnd(Comparable<Date> end)

Changes the upper limit for Dates in this sequence. If start is null, 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 the nextValue, previousValue, or setValue methods.

Typically this property is a Date however it's possible to use Comparable with a compareTo method for Dates. See setStart for an example.

This method fires a ChangeEvent if the end has changed.

Parameters
end:Comparable<Date>

defines the last date in the sequence

See Also
getEnd, setStart, addChangeListener
setStartback to summary
public void setStart(Comparable<Date> start)

Changes the lower limit for Dates in this sequence. If start is null, 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 the nextValue, previousValue, or setValue methods.

Typically this property is a Date however it's possible to use a Comparable with a compareTo method for Dates. For example start 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 a ClassCastException if the Object passed to compareTo(Object) is not a Date.

This method fires a ChangeEvent if the start has changed.

Parameters
start:Comparable<Date>

defines the first date in the sequence

See Also
getStart, setEnd, addChangeListener
setValueback to summary
public void setValue(Object value)

Implements javax.swing.SpinnerModel.setValue.

Sets the current Date for this sequence. If value is null, an IllegalArgumentException 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 the nextValue, previousValue, or setValue methods.

This method fires a ChangeEvent if the value has changed.

Parameters
value:Object

the current (non null) Date for this sequence

Exceptions
IllegalArgumentException:
if value is null or not a Date
See Also
getDate, getValue, addChangeListener