javax.swing
Class SpinnerDateModel

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by javax.swing.AbstractSpinnerModel sample code for javax.swing.AbstractSpinnerModel definition code for javax.swing.AbstractSpinnerModel 
      extended by javax.swing.SpinnerDateModel
All Implemented Interfaces:
Serializable sample code for java.io.Serializable definition code for java.io.Serializable , SpinnerModel sample code for javax.swing.SpinnerModel definition code for javax.swing.SpinnerModel

public class SpinnerDateModel
extends AbstractSpinnerModel sample code for javax.swing.AbstractSpinnerModel definition code for javax.swing.AbstractSpinnerModel
implements Serializable sample code for java.io.Serializable definition code for 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 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.

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

Since:
1.4
See Also:
JSpinner sample code for javax.swing.JSpinner definition code for javax.swing.JSpinner , SpinnerModel sample code for javax.swing.SpinnerModel definition code for javax.swing.SpinnerModel , AbstractSpinnerModel sample code for javax.swing.AbstractSpinnerModel definition code for javax.swing.AbstractSpinnerModel , SpinnerListModel sample code for javax.swing.SpinnerListModel definition code for javax.swing.SpinnerListModel , SpinnerNumberModel sample code for javax.swing.SpinnerNumberModel definition code for javax.swing.SpinnerNumberModel , Calendar.add(int, int) sample code for java.util.Calendar.add(int, int) definition code for java.util.Calendar.add(int, int) , Serialized Form

Field Summary
 
Fields inherited from class javax.swing.AbstractSpinnerModel sample code for javax.swing.AbstractSpinnerModel definition code for javax.swing.AbstractSpinnerModel
listenerList sample code for javax.swing.AbstractSpinnerModel.listenerList definition code for javax.swing.AbstractSpinnerModel.listenerList
 
Constructor Summary
SpinnerDateModel sample code for javax.swing.SpinnerDateModel.SpinnerDateModel() definition code for javax.swing.SpinnerDateModel.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.
SpinnerDateModel sample code for javax.swing.SpinnerDateModel.SpinnerDateModel(java.util.Date, java.lang.Comparable, java.lang.Comparable, int) definition code for javax.swing.SpinnerDateModel.SpinnerDateModel(java.util.Date, java.lang.Comparable, java.lang.Comparable, int) (Date sample code for java.util.Date definition code for java.util.Date  value, Comparable sample code for java.lang.Comparable definition code for java.lang.Comparable  start, Comparable sample code for java.lang.Comparable definition code for java.lang.Comparable  end, int calendarField)
          Creates a SpinnerDateModel that represents a sequence of dates between start and end.
 
Method Summary
 int getCalendarField sample code for javax.swing.SpinnerDateModel.getCalendarField() definition code for javax.swing.SpinnerDateModel.getCalendarField() ()
          Returns the Calendar field that is added to or subtracted from by the nextValue and previousValue methods.
 Date sample code for java.util.Date definition code for java.util.Date getDate sample code for javax.swing.SpinnerDateModel.getDate() definition code for javax.swing.SpinnerDateModel.getDate() ()
          Returns the current element in this sequence of Dates.
 Comparable sample code for java.lang.Comparable definition code for java.lang.Comparable getEnd sample code for javax.swing.SpinnerDateModel.getEnd() definition code for javax.swing.SpinnerDateModel.getEnd() ()
          Returns the last Date in the sequence.
 Object sample code for java.lang.Object definition code for java.lang.Object getNextValue sample code for javax.swing.SpinnerDateModel.getNextValue() definition code for javax.swing.SpinnerDateModel.getNextValue() ()
          Returns the next Date in the sequence, or null if the next date is after end.
 Object sample code for java.lang.Object definition code for java.lang.Object getPreviousValue sample code for javax.swing.SpinnerDateModel.getPreviousValue() definition code for javax.swing.SpinnerDateModel.getPreviousValue() ()
          Returns the previous Date in the sequence, or null if the previous date is before start.
 Comparable sample code for java.lang.Comparable definition code for java.lang.Comparable getStart sample code for javax.swing.SpinnerDateModel.getStart() definition code for javax.swing.SpinnerDateModel.getStart() ()
          Returns the first Date in the sequence.
 Object sample code for java.lang.Object definition code for java.lang.Object getValue sample code for javax.swing.SpinnerDateModel.getValue() definition code for javax.swing.SpinnerDateModel.getValue() ()
          Returns the current element in this sequence of Dates.
 void setCalendarField sample code for javax.swing.SpinnerDateModel.setCalendarField(int) definition code for javax.swing.SpinnerDateModel.setCalendarField(int) (int calendarField)
          Changes the size of the date value change computed by the nextValue and previousValue methods.
 void setEnd sample code for javax.swing.SpinnerDateModel.setEnd(java.lang.Comparable) definition code for javax.swing.SpinnerDateModel.setEnd(java.lang.Comparable) (Comparable sample code for java.lang.Comparable definition code for java.lang.Comparable  end)
          Changes the upper limit for Dates in this sequence.
 void setStart sample code for javax.swing.SpinnerDateModel.setStart(java.lang.Comparable) definition code for javax.swing.SpinnerDateModel.setStart(java.lang.Comparable) (Comparable sample code for java.lang.Comparable definition code for java.lang.Comparable  start)
          Changes the lower limit for Dates in this sequence.
 void setValue sample code for javax.swing.SpinnerDateModel.setValue(java.lang.Object) definition code for javax.swing.SpinnerDateModel.setValue(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  value)
          Sets the current Date for this sequence.
 
Methods inherited from class javax.swing.AbstractSpinnerModel sample code for javax.swing.AbstractSpinnerModel definition code for javax.swing.AbstractSpinnerModel
addChangeListener sample code for javax.swing.AbstractSpinnerModel.addChangeListener(javax.swing.event.ChangeListener) definition code for javax.swing.AbstractSpinnerModel.addChangeListener(javax.swing.event.ChangeListener) , fireStateChanged sample code for javax.swing.AbstractSpinnerModel.fireStateChanged() definition code for javax.swing.AbstractSpinnerModel.fireStateChanged() , getChangeListeners sample code for javax.swing.AbstractSpinnerModel.getChangeListeners() definition code for javax.swing.AbstractSpinnerModel.getChangeListeners() , getListeners sample code for javax.swing.AbstractSpinnerModel.getListeners(java.lang.Class) definition code for javax.swing.AbstractSpinnerModel.getListeners(java.lang.Class) , removeChangeListener sample code for javax.swing.AbstractSpinnerModel.removeChangeListener(javax.swing.event.ChangeListener) definition code for javax.swing.AbstractSpinnerModel.removeChangeListener(javax.swing.event.ChangeListener)
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Constructor Detail

SpinnerDateModel sample code for javax.swing.SpinnerDateModel(java.util.Date, java.lang.Comparable, java.lang.Comparable, int) definition code for javax.swing.SpinnerDateModel(java.util.Date, java.lang.Comparable, java.lang.Comparable, int)

public SpinnerDateModel(Date sample code for java.util.Date definition code for java.util.Date  value,
                        Comparable sample code for java.lang.Comparable definition code for java.lang.Comparable  start,
                        Comparable sample code for java.lang.Comparable definition code for java.lang.Comparable  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 mininum > maximum then an IllegalArgumentException is thrown. Similarly if (minimum <= value <= maximum) is false, an IllegalArgumentException is thrown.

Parameters:
value - the current (non null) value of the model
start - the first date in the sequence or null
end - the last date in the sequence or null
calendarField - 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
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.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(int, int) sample code for java.util.Calendar.add(int, int) definition code for java.util.Calendar.add(int, int) , setValue(java.lang.Object) sample code for javax.swing.SpinnerDateModel.setValue(java.lang.Object) definition code for javax.swing.SpinnerDateModel.setValue(java.lang.Object) , setStart(java.lang.Comparable) sample code for javax.swing.SpinnerDateModel.setStart(java.lang.Comparable) definition code for javax.swing.SpinnerDateModel.setStart(java.lang.Comparable) , setEnd(java.lang.Comparable) sample code for javax.swing.SpinnerDateModel.setEnd(java.lang.Comparable) definition code for javax.swing.SpinnerDateModel.setEnd(java.lang.Comparable) , setCalendarField(int) sample code for javax.swing.SpinnerDateModel.setCalendarField(int) definition code for javax.swing.SpinnerDateModel.setCalendarField(int)

SpinnerDateModel sample code for javax.swing.SpinnerDateModel() definition code for javax.swing.SpinnerDateModel()

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

setStart sample code for javax.swing.SpinnerDateModel.setStart(java.lang.Comparable) definition code for javax.swing.SpinnerDateModel.setStart(java.lang.Comparable)

public void setStart(Comparable sample code for java.lang.Comparable definition code for java.lang.Comparable  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 - defines the first date in the sequence
See Also:
getStart() sample code for javax.swing.SpinnerDateModel.getStart() definition code for javax.swing.SpinnerDateModel.getStart() , setEnd(java.lang.Comparable) sample code for javax.swing.SpinnerDateModel.setEnd(java.lang.Comparable) definition code for javax.swing.SpinnerDateModel.setEnd(java.lang.Comparable) , AbstractSpinnerModel.addChangeListener(javax.swing.event.ChangeListener) sample code for javax.swing.AbstractSpinnerModel.addChangeListener(javax.swing.event.ChangeListener) definition code for javax.swing.AbstractSpinnerModel.addChangeListener(javax.swing.event.ChangeListener)

getStart sample code for javax.swing.SpinnerDateModel.getStart() definition code for javax.swing.SpinnerDateModel.getStart()

public Comparable sample code for java.lang.Comparable definition code for java.lang.Comparable  getStart()
Returns the first Date in the sequence.

Returns:
the value of the start property
See Also:
setStart(java.lang.Comparable) sample code for javax.swing.SpinnerDateModel.setStart(java.lang.Comparable) definition code for javax.swing.SpinnerDateModel.setStart(java.lang.Comparable)

setEnd sample code for javax.swing.SpinnerDateModel.setEnd(java.lang.Comparable) definition code for javax.swing.SpinnerDateModel.setEnd(java.lang.Comparable)

public void setEnd(Comparable sample code for java.lang.Comparable definition code for java.lang.Comparable  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 - defines the last date in the sequence
See Also:
getEnd() sample code for javax.swing.SpinnerDateModel.getEnd() definition code for javax.swing.SpinnerDateModel.getEnd() , setStart(java.lang.Comparable) sample code for javax.swing.SpinnerDateModel.setStart(java.lang.Comparable) definition code for javax.swing.SpinnerDateModel.setStart(java.lang.Comparable) , AbstractSpinnerModel.addChangeListener(javax.swing.event.ChangeListener) sample code for javax.swing.AbstractSpinnerModel.addChangeListener(javax.swing.event.ChangeListener) definition code for javax.swing.AbstractSpinnerModel.addChangeListener(javax.swing.event.ChangeListener)

getEnd sample code for javax.swing.SpinnerDateModel.getEnd() definition code for javax.swing.SpinnerDateModel.getEnd()

public Comparable sample code for java.lang.Comparable definition code for java.lang.Comparable  getEnd()
Returns the last Date in the sequence.

Returns:
the value of the end property
See Also:
setEnd(java.lang.Comparable) sample code for javax.swing.SpinnerDateModel.setEnd(java.lang.Comparable) definition code for javax.swing.SpinnerDateModel.setEnd(java.lang.Comparable)

setCalendarField sample code for javax.swing.SpinnerDateModel.setCalendarField(int) definition code for javax.swing.SpinnerDateModel.setCalendarField(int)

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 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 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() sample code for javax.swing.SpinnerDateModel.getCalendarField() definition code for javax.swing.SpinnerDateModel.getCalendarField() , getNextValue() sample code for javax.swing.SpinnerDateModel.getNextValue() definition code for javax.swing.SpinnerDateModel.getNextValue() , getPreviousValue() sample code for javax.swing.SpinnerDateModel.getPreviousValue() definition code for javax.swing.SpinnerDateModel.getPreviousValue() , Calendar.add(int, int) sample code for java.util.Calendar.add(int, int) definition code for java.util.Calendar.add(int, int) , AbstractSpinnerModel.addChangeListener(javax.swing.event.ChangeListener) sample code for javax.swing.AbstractSpinnerModel.addChangeListener(javax.swing.event.ChangeListener) definition code for javax.swing.AbstractSpinnerModel.addChangeListener(javax.swing.event.ChangeListener)

getCalendarField sample code for javax.swing.SpinnerDateModel.getCalendarField() definition code for javax.swing.SpinnerDateModel.getCalendarField()

public int getCalendarField()
Returns the Calendar field that is added to or subtracted from by the nextValue and previousValue methods.

Returns:
the value of the calendarField property
See Also:
setCalendarField(int) sample code for javax.swing.SpinnerDateModel.setCalendarField(int) definition code for javax.swing.SpinnerDateModel.setCalendarField(int)

getNextValue sample code for javax.swing.SpinnerDateModel.getNextValue() definition code for javax.swing.SpinnerDateModel.getNextValue()

public Object sample code for java.lang.Object definition code for java.lang.Object  getNextValue()
Returns the next Date in the sequence, or null if the next date is after end.

Specified by:
getNextValue sample code for javax.swing.SpinnerModel.getNextValue() definition code for javax.swing.SpinnerModel.getNextValue() in interface SpinnerModel sample code for javax.swing.SpinnerModel definition code for javax.swing.SpinnerModel
Returns:
the next Date in the sequence, or null if the next date is after end.
See Also:
SpinnerModel.getNextValue() sample code for javax.swing.SpinnerModel.getNextValue() definition code for javax.swing.SpinnerModel.getNextValue() , getPreviousValue() sample code for javax.swing.SpinnerDateModel.getPreviousValue() definition code for javax.swing.SpinnerDateModel.getPreviousValue() , setCalendarField(int) sample code for javax.swing.SpinnerDateModel.setCalendarField(int) definition code for javax.swing.SpinnerDateModel.setCalendarField(int)

getPreviousValue sample code for javax.swing.SpinnerDateModel.getPreviousValue() definition code for javax.swing.SpinnerDateModel.getPreviousValue()

public Object sample code for java.lang.Object definition code for java.lang.Object  getPreviousValue()
Returns the previous Date in the sequence, or null if the previous date is before start.

Specified by:
getPreviousValue sample code for javax.swing.SpinnerModel.getPreviousValue() definition code for javax.swing.SpinnerModel.getPreviousValue() in interface SpinnerModel sample code for javax.swing.SpinnerModel definition code for javax.swing.SpinnerModel
Returns:
the previous Date in the sequence, or null if the previous date is before start
See Also:
SpinnerModel.getPreviousValue() sample code for javax.swing.SpinnerModel.getPreviousValue() definition code for javax.swing.SpinnerModel.getPreviousValue() , getNextValue() sample code for javax.swing.SpinnerDateModel.getNextValue() definition code for javax.swing.SpinnerDateModel.getNextValue() , setCalendarField(int) sample code for javax.swing.SpinnerDateModel.setCalendarField(int) definition code for javax.swing.SpinnerDateModel.setCalendarField(int)

getDate sample code for javax.swing.SpinnerDateModel.getDate() definition code for javax.swing.SpinnerDateModel.getDate()

public Date sample code for java.util.Date definition code for java.util.Date  getDate()
Returns the current element in this sequence of Dates. This method is equivalent to (Date)getValue.

Returns:
the value property
See Also:
setValue(java.lang.Object) sample code for javax.swing.SpinnerDateModel.setValue(java.lang.Object) definition code for javax.swing.SpinnerDateModel.setValue(java.lang.Object)

getValue sample code for javax.swing.SpinnerDateModel.getValue() definition code for javax.swing.SpinnerDateModel.getValue()

public Object sample code for java.lang.Object definition code for java.lang.Object  getValue()
Returns the current element in this sequence of Dates.

Specified by:
getValue sample code for javax.swing.SpinnerModel.getValue() definition code for javax.swing.SpinnerModel.getValue() in interface SpinnerModel sample code for javax.swing.SpinnerModel definition code for javax.swing.SpinnerModel
Returns:
the value property
See Also:
setValue(java.lang.Object) sample code for javax.swing.SpinnerDateModel.setValue(java.lang.Object) definition code for javax.swing.SpinnerDateModel.setValue(java.lang.Object) , getDate() sample code for javax.swing.SpinnerDateModel.getDate() definition code for javax.swing.SpinnerDateModel.getDate()

setValue sample code for javax.swing.SpinnerDateModel.setValue(java.lang.Object) definition code for javax.swing.SpinnerDateModel.setValue(java.lang.Object)

public void setValue(Object sample code for java.lang.Object definition code for java.lang.Object  value)
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.

Specified by:
setValue sample code for javax.swing.SpinnerModel.setValue(java.lang.Object) definition code for javax.swing.SpinnerModel.setValue(java.lang.Object) in interface SpinnerModel sample code for javax.swing.SpinnerModel definition code for javax.swing.SpinnerModel
Parameters:
value - the current (non null) Date for this sequence
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if value is null or not a Date
See Also:
getDate() sample code for javax.swing.SpinnerDateModel.getDate() definition code for javax.swing.SpinnerDateModel.getDate() , getValue() sample code for javax.swing.SpinnerDateModel.getValue() definition code for javax.swing.SpinnerDateModel.getValue() , AbstractSpinnerModel.addChangeListener(javax.swing.event.ChangeListener) sample code for javax.swing.AbstractSpinnerModel.addChangeListener(javax.swing.event.ChangeListener) definition code for javax.swing.AbstractSpinnerModel.addChangeListener(javax.swing.event.ChangeListener)