Class DatePicker

All Implemented Interfaces:
IsEditor<LeafValueEditor<Date>>, HasAttachHandlers, HasHighlightHandlers<Date>, HasShowRangeHandlers<Date>, HasValueChangeHandlers<Date>, HasHandlers, EventListener, TakesValue<Date>, HasValue<Date>, HasVisibility, IsRenderable, IsWidget

Standard GWT date picker.

CSS Style Rules

  • .gwt-DatePicker { }
  • .datePickerMonthSelector { the month selector widget }
  • .datePickerMonth { the month in the month selector widget }
  • .datePickerYear { the year in the month selector widget }
  • .datePickerPreviousButton { the previous month button }
  • .datePickerNextButton { the next month button }
  • .datePickerPreviousYearButton { the previous year button }
  • .datePickerNextYearButton { the next year button }
  • .datePickerDays { the portion of the picker that shows the days }
  • .datePickerWeekdayLabel { the label over weekdays }
  • .datePickerWeekendLabel { the label over weekends }
  • .datePickerDay { a single day }
  • .datePickerDayIsToday { today's date }
  • .datePickerDayIsWeekend { a weekend day }
  • .datePickerDayIsFiller { a day in another month }
  • .datePickerDayIsValue { the selected day }
  • .datePickerDayIsDisabled { a disabled day }
  • .datePickerDayIsHighlighted { the currently highlighted day }
  • .datePickerDayIsValueAndHighlighted { the highlighted day if it is also selected }

Example

public class DatePickerExample implements EntryPoint {

  public void onModuleLoad() {
    // Create a date picker
    DatePicker datePicker = new DatePicker();
    final Label text = new Label();

    // Set the value in the text box when the user selects a date
    datePicker.addValueChangeHandler(new ValueChangeHandler<Date>() {
      public void onValueChange(ValueChangeEvent<Date> event) {
        Date date = event.getValue();
        String dateString = DateTimeFormat.getMediumDateFormat().format(date);
        text.setText(dateString);
      }
    });

    // Set the default value
    datePicker.setValue(new Date(), true);
    
    // Add the widgets to the page
    RootPanel.get().add(text);
    RootPanel.get().add(datePicker);
  }
}

  • Constructor Details

    • DatePicker

      public DatePicker()
      Create a new date picker.
    • DatePicker

      protected DatePicker(MonthSelector monthAndYearSelector, CalendarView view, CalendarModel model)
      Creates a new date picker.
      Parameters:
      monthAndYearSelector - the month selector
      view - the view
      model - the model
  • Method Details

    • addHighlightHandler

      public HandlerRegistration addHighlightHandler(HighlightHandler<Date> handler)
      Description copied from interface: HasHighlightHandlers
      Adds a HighlightEvent handler.
      Specified by:
      addHighlightHandler in interface HasHighlightHandlers<Date>
      Parameters:
      handler - the handler
      Returns:
      the registration for the event
    • addShowRangeHandler

      public HandlerRegistration addShowRangeHandler(ShowRangeHandler<Date> handler)
      Description copied from interface: HasShowRangeHandlers
      Adds a ShowRangeEvent handler.
      Specified by:
      addShowRangeHandler in interface HasShowRangeHandlers<Date>
      Parameters:
      handler - the handler
      Returns:
      the registration for the event
    • addShowRangeHandlerAndFire

      public HandlerRegistration addShowRangeHandlerAndFire(ShowRangeHandler<Date> handler)
      Adds a show range handler and immediately activate the handler on the current view.
      Parameters:
      handler - the handler
      Returns:
      the handler registration
    • addStyleToDates

      public void addStyleToDates(String styleName, Date date)
      Add a style name to the given dates.
    • addStyleToDates

      public void addStyleToDates(String styleName, Date date, Date... moreDates)
      Add a style name to the given dates.
    • addStyleToDates

      public void addStyleToDates(String styleName, Iterable<Date> dates)
      Add a style name to the given dates.
    • addTransientStyleToDates

      public void addTransientStyleToDates(String styleName, Date date)
      Adds the given style name to the specified dates, which must be visible. This is only set until the next time the DatePicker is refreshed.
    • addTransientStyleToDates

      public final void addTransientStyleToDates(String styleName, Date date, Date... moreDates)
      Adds the given style name to the specified dates, which must be visible. This is only set until the next time the DatePicker is refreshed.
    • addTransientStyleToDates

      public final void addTransientStyleToDates(String styleName, Iterable<Date> dates)
      Adds the given style name to the specified dates, which must be visible. This is only set until the next time the DatePicker is refreshed.
    • addValueChangeHandler

      public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Date> handler)
      Description copied from interface: HasValueChangeHandlers
      Adds a ValueChangeEvent handler.
      Specified by:
      addValueChangeHandler in interface HasValueChangeHandlers<Date>
      Parameters:
      handler - the handler
      Returns:
      the registration for the event
    • asEditor

      public LeafValueEditor<Date> asEditor()
      Returns a TakesValueEditor backed by the DatePicker.
      Specified by:
      asEditor in interface IsEditor<LeafValueEditor<Date>>
      Returns:
      an Editor of type E
    • getCurrentMonth

      public Date getCurrentMonth()
      Gets the current month the date picker is showing.

      A datepicker may show days not in the current month. It must show all days in the current month.

      Returns:
      the current month
    • getFirstDate

      public final Date getFirstDate()
      Returns the first shown date.
      Returns:
      the first date.
    • getHighlightedDate

      public final Date getHighlightedDate()
      Gets the highlighted date (the one the mouse is hovering over), if any.
      Returns:
      the highlighted date
    • getLastDate

      public final Date getLastDate()
      Returns the last shown date.
      Returns:
      the last date.
    • getVisibleYearCount

      public int getVisibleYearCount()
      Returns the number of year to display in the years selection dropdown.
    • getStyleOfDate

      public String getStyleOfDate(Date date)
      Gets the style associated with a date (does not include styles set via addTransientStyleToDates(java.lang.String, java.util.Date)).
      Parameters:
      date - the date
      Returns:
      the styles associated with this date
    • getValue

      public final Date getValue()
      Returns the selected date, or null if none is selected.
      Specified by:
      getValue in interface HasValue<Date>
      Specified by:
      getValue in interface TakesValue<Date>
      Returns:
      the selected date, or null
      See Also:
    • isDateEnabled

      public boolean isDateEnabled(Date date)
      Is the visible date enabled?
      Parameters:
      date - the date, which must be visible
      Returns:
      is the date enabled?
    • isDateVisible

      public boolean isDateVisible(Date date)
      Is the date currently shown in the date picker?
      Parameters:
      date -
      Returns:
      is the date currently shown
    • isYearArrowsVisible

      public boolean isYearArrowsVisible()
      Can the user navigate through the years?
      Returns:
      is the year navigation is enabled
    • isYearAndMonthDropdownVisible

      public boolean isYearAndMonthDropdownVisible()
      Is the year and month selectable via a dropdown?
    • onLoad

      public void onLoad()
      Description copied from class: Widget
      This method is called immediately after a widget becomes attached to the browser's document.
      Overrides:
      onLoad in class Widget
    • removeStyleFromDates

      public void removeStyleFromDates(String styleName, Date date)
      Removes the styleName from the given dates (even if it is transient).
    • removeStyleFromDates

      public void removeStyleFromDates(String styleName, Date date, Date... moreDates)
      Removes the styleName from the given dates (even if it is transient).
    • removeStyleFromDates

      public void removeStyleFromDates(String styleName, Iterable<Date> dates)
      Removes the styleName from the given dates (even if it is transient).
    • setCurrentMonth

      public void setCurrentMonth(Date month)
      Sets the date picker to show the given month, use getFirstDate() and getLastDate() to access the exact date range the date picker chose to display.

      A datepicker may show days not in the current month. It must show all days in the current month.

      Parameters:
      month - the month to show
    • setVisibleYearCount

      public void setVisibleYearCount(int numberOfYears)
      Set the number of years to display in the years selection dropdown. The range of years will be centered on the selected date.
    • setYearArrowsVisible

      public void setYearArrowsVisible(boolean yearArrowsVisible)
      Set if the user can navigate through the years via a set of backward and forward buttons.
    • setYearAndMonthDropdownVisible

      public void setYearAndMonthDropdownVisible(boolean dropdownVisible)
      If the dropdownVisible is equal to true, the user will be able to change the current month and the current year of the date picker via two dropdown lists.
    • setStyleName

      public void setStyleName(String styleName)
      Sets the date picker style name.
      Overrides:
      setStyleName in class UIObject
      Parameters:
      styleName - the new style name
      See Also:
    • setTransientEnabledOnDates

      public final void setTransientEnabledOnDates(boolean enabled, Date date)
      Sets a visible date to be enabled or disabled. This is only set until the next time the DatePicker is refreshed.
    • setTransientEnabledOnDates

      public final void setTransientEnabledOnDates(boolean enabled, Date date, Date... moreDates)
      Sets a visible date to be enabled or disabled. This is only set until the next time the DatePicker is refreshed.
    • setTransientEnabledOnDates

      public final void setTransientEnabledOnDates(boolean enabled, Iterable<Date> dates)
      Sets a group of visible dates to be enabled or disabled. This is only set until the next time the DatePicker is refreshed.
    • setValue

      public final void setValue(Date newValue)
      Sets the DatePicker's value.
      Specified by:
      setValue in interface HasValue<Date>
      Specified by:
      setValue in interface TakesValue<Date>
      Parameters:
      newValue - the new value
      See Also:
    • setValue

      public final void setValue(Date newValue, boolean fireEvents)
      Sets the DatePicker's value.
      Specified by:
      setValue in interface HasValue<Date>
      Parameters:
      newValue - the new value for this date picker
      fireEvents - should events be fired.
    • getModel

      protected final CalendarModel getModel()
      Gets the CalendarModel associated with this date picker.
      Returns:
      the model
    • getMonthSelector

      protected final MonthSelector getMonthSelector()
      Gets the MonthSelector associated with this date picker.
      Returns:
      the month selector
    • getView

      protected final CalendarView getView()
      Gets the CalendarView associated with this date picker.
      Returns:
      the view
    • refreshAll

      protected final void refreshAll()
      Refreshes all components of this date picker.
    • setup

      protected void setup()
      Sets up the date picker.
    • css

      Gets the css associated with this date picker for use by extended month and cell grids.
      Returns:
      the css.
    • setHighlightedDate

      void setHighlightedDate(Date highlighted)
      Sets the highlighted date.
      Parameters:
      highlighted - highlighted date