The Date Selector is a fully featured Swing based component for allowing graphical selection of dates and/or ranges of dates via a GUI component. It supports all of the calendar systems currently defined in Joda Time, all standard time zones and Locales.
Factory Methods
Both classes have factory methods for creating dialogs, popups and for registering
popup-on-hover functionality on the owning component. Additionally the components which ultimately
extend JPanel can be embedded directly in to a GUI.
Usage
The following examples all refer to the DateSelector class but are equally applicable to the IntervalSelector class.
Creating a Basic Date Selector Dialog
Displaying a basic DateSelector dialog is as simple as follows:
DateTime d = DateSelector.show(component, date);
This will create a modal dialog to display a DateSelector object initially displaying the date specified in the 'date' variable. The DateSelector will display the calendar system (chronology) used by the 'date' DateTime object. The dialog will display relative to the location of the component specified in the 'component' variable. If the dialog is closed through the 'OK' button the currently selected date is returned. If the 'Cancel' button is pressed 'null' is returned.
To specify the Locale of the DateSelector use the show method that also takes a Locale argument otherwise the default Locale is used.
Creating a Basic Date Selector Popup
Displaying a basic DateSelector popup is as simple as follows:
PopupDateSelector popup = DateSelector.createPopup(component, date); popup.setVisible(true); DateTime d = popup.getSelectedDate();
This will create a modal undecorated dialog to display a DateSelector object initially displaying the date specified in the 'date' variable. The DateSelector will display the calendar system (chronology) used by the 'date' DateTime object. The dialog will display relative to the location of the component specified in the 'component' variable. Once a date is selected the popup will close. If the 'Escape' key is pressed the panel will close and getSelectedDate will return null.
Creating a Basic Popup on Hover Date Selector
The popup on hover functionality registers a listener with the given component to determine when the
mouse has hovered on the component. When this is detected a DateSelector popup will automatically appear.
Registering a component for 'popup on hover' is as simple as follows:
listener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { selectedDate = ((DateSelector)e.getSource()).getSelectedDate(); ... your code here ... } }); DateSelector.setPopupOnHoverForComponent(component, date, listener)
This will create a modal undecorated dialog to display a DateSelector object initially displaying the date specified in the 'date' variable. The DateSelector will display the calendar system (chronology) used by the 'date' DateTime object. The dialog will display relative to the location of the component specified in the 'component' variable. Once a date is selected the popup will close and the listener will be called. If the 'Escape' key is pressed the panel will close but the listener will not be called.
To remove the popup on hover functionality call the removePopupOnHoverFromComponent with the component it was originally register with ie:
DateSelector.removePopupOnHoverFromComponent(component)
Creating an Embedded Date Selector Panel
DateSelector ultimately extends JPanel so an object of type DateSelector can simply be instantiated
and added to a GUI in exactly the same way as you would add any other Swing component.
Embedding a DateSelector panel is as simple as follows:
DateSelector datePanel = new DateSelector(date, false); myGUI.add(datePanel);
This will create a basic DateSelector object initially displaying the date specified in the 'date' variable. The DateSelector will display the calendar system (chronology) used by the 'date' DateTime object.
Configuring a Date Selector Panel
The DateSelector can be configured to change the detail displayed such as switching off
week numbers, day names, leading and trailing days etc. Listeners can also be added to
receive event notification when the date changes and/or when a day button is selected.
The show(..) and createPopup(..) factory methods are provided that directly take a DateSelector object to allow easy configuration before creating the display Window.
An example of configuring a DateSelector object follows:
DateSelector ds = new DateSelector(date, true); ds.setShowWeekNums(showWeekNumbers); ds.setShowDayNames(showDayNames); ds.setShowSurroundingDays(showOtherDays); ds.setFont(font); panel.addDateChangedListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { // a new date has been selected selectedDate = ((DateSelector)e.getSource()).getSelectedDate(); ... your code here ... } });