The JDatePicker is a component that can edit a date and also select it using a popup calendar. It inherits all the date editing features from JDateEditComponent. Here are the features this component provides:
Editable or non-editable behavior.
Customization of the popup calendar.
Possibility to use it as a JTable cell editor for Date objects.
This section uses the application shown below to explore all these capabilities. Not all editing features are presented here as they are presented in the date editing section.
Try this:
Run DatePickerDemo using Java Web Start or consult the source code for yourself.
Click the date picker component to display the popup calendar. Then click on a date from the calendar. The date will be displayed by the component.
Press the DOWN arrow key on the date picker component to display the popup calendar. Now, use the arrows keys (LEFT, RIGHT, UP, DOWN) to select a date and then press ENTER. The last selected date will be displayed by the component.
Click the Editable check box to make the date picker editable. Then click on the date picker, type 11112004 and press ENTER; November 11, 2004 will be selected.
Click on the Foreground button and select a color. The dates from the popup calendar will be painted using this color.
Click on the Date Format combo box and select Short. The date selected by the date picker will be displayed using this format.
In the bottom table, click on one of the cells from the Start column. Use the date picker cell editor to select a different date.
There are applications where users prefer to use the keyboard rather than the mouse. JDatePicker can act like a regular date edit component but only if activated using the setEditable method. It is not active by default. More about the date editing features is presented in the JDateEditComponent tutorial section.
The following line of code turns the date picker into an editable one:
datePicker.setEditable(true);
Setting the background color for JDatePicker is easy to do. Just use the setBackground method and specify your color.
If the date picker is editable, the background color for its date field is not changed. You have to register the JDatePicker.backgroundOnEditable client property in order for the background color to be set on the date field too. The following line of code shows how the background color can be set:
datePicker.putClientProperty("JDatePicker.backgroundOnEditable", Boolean.TRUE);
datePicker.setBackground(Color.red);This might seem very complicated but we are trying to preserve the JComboBox behavior. JDatePicker uses a modified JComboBox so when you specify a background color, it is in fact set on the supporting JComboBox.
If the combo box is editable, the background color is not set on the editing component (Swing implementation bug?). JDatePicker uses the JDatePicker.backgroundOnEditable client property to correct this behavior.
The How to Configure UI Delegates API section explains why we use client properties to configure the components.
The popup calendar is an important part of the date picker because it allows users to select dates easily. But its default look might not be what you need so we've made it easy to configure its appearance.
To configure the popup calendar, you must use the date picker Popup object. Here's what it helps you to do:
show or hide it
access the calendar (ONLY to configure it)
register your own popup listener in order to know when the visibility of the popup changes or is about to change
Let's see how you can do that. The following code can be used to show or hide the popup:
datePicker.getPopup().setVisible(visible);
You can access the calendar using the popup. But you should access the calendar only to change its visual properties like colors, week numbers visibility, today/none visibility, etc. You should not use it to change the selection model because it uses the one from JDatePicker. To following code shows how to make week numbers visible:
datePicker.getPopup().getMonthView().setWeekNumbersVisible(visible);
The third option from the above list is to register your own popup listener. This might be useful if you want to configure the popup before it is displayed. For instance, the following code shows how to display two months instead of one:
datePicker.getPopup().addPopupListener(new PopupListener() {
public void popupWillBecomeVisible(PopupEvent e) {
//display 2 months on a certain condition
if (condition) {
datePicker.getPopup().getMonthView().setColumns(2);
} else {
datePicker.getPopup().getMonthView().setColumns(1);
}
}
public void popupWillBecomeInvisible(PopupEvent e) {
}
public void visibleChanged(PopupEvent evt) {
}
});Like any other JDateComponent, the JDatePicker can be localized for various languages and regions. The localization acts upon the calendar used by the date picker and on how it displays the selected date.
By default, the system's locale is used but it can be configured to use any locale. To change the locale, use the setLocale method.
The following line of code changes the locale to Spanish:
datePicker.setLocale(new Locale("es", "ES"));
datePicker.setDateFormat(DateFormat.FULL);JDatePicker can be used with any look and feel. The default ones are: Metal, Motif, Windows and Aqua but others can be supported as this section explains.
The JDatePicker implementation uses a modified JComboBox to represent the popup calendar. Actually, what is modified is its implementation, ComboBoxUI, for which the popup is changed. Let us see how this is done for a custom look and feel like JGoodies Plastic.
First of all, create a class that extends the PlasticComboBoxUI and implements ComboBoxUIExt (this interface is used internally by JDatePicker to change the popup when the look and feel changes).
We assume that the respective ComboBoxUI can be extended and it is not final.
import com.jgoodies.looks.plastic.PlasticComboBoxUI;
import com.standbysoft.component.util.swing.ComboBoxUIExt;
public class PlasticComboBoxUIExt extends PlasticComboBoxUI implements ComboBoxUIExt {
public void setComboPopup(ComboPopup popup) {
}
}Then, the popup set by the setComboPopup method must be returned by the createPopup method (overridden from BasicComboBoxUI).
import com.jgoodies.looks.plastic.PlasticComboBoxUI;
import com.standbysoft.component.util.swing.ComboBoxUIExt;
public class PlasticComboBoxUIExt extends PlasticComboBoxUI implements ComboBoxUIExt {
private ComboPopup popup;
protected ComboPopup createPopup() {
if (popup == null) {
return super.createPopup();
} else {
return popup;
}
}
public void setComboPopup(ComboPopup popup) {
this.popup = popup;
}
}Once you have created an extended ComboBoxUI for a certain look and feel, just register it with the date picker. This is done in the main code of the application, before the application is started.
But make sure the new ComboBoxUI can be instantiated. It must be public and if it is an inner class it also must be static.
import com.standbysoft.component.date.swing.plaf.basic.BasicDatePickerUI;
...
BasicDatePickerUI.registerDateComboBoxUI("com.jgoodies.looks.plastic.Plastic3DLookAndFeel", PlasticComboBoxUIExt.class);Setting a date picker as a table cell editor is simple, as the following example shows. The code sets up a date picker as the editor for Date objects.
JTable table = new JTable();
...
JDatePicker datePicker = new JDatePicker();
datePicker.setDateFormat("MMMM/dd yyyy");
table.setDefaultEditor(Date.class, JDatePicker.createTableCellEditor(datePicker));You can use createTableCellEditor() or createTableCellEditor(JDatePicker) to create table cell editor. The second method can be used to create a table cell editor with a configured date picker.