(Printer-friendly display)
DatePicker and DateEdit are two Swing components that let you easily add calendar functionality to your Java application. Both components extend JPanel so they can be incorporated into dialogs, frames and other Swing components that support JPanel components.
The following sample code illustrates how the DateEdit component is used in a dialog:
JDialog dlg = new JDialog(new Frame(), true);
DateField df = new DateField();
dlg.getContentPane().add(df);
dlg.pack();
dlg.show();
System.out.println(df.getDate().toString());
| When the dialog is initially displayed, the user sees the DateEdit component: |
When the user clicks on the button to the right of the text box, the DatePicker component becomes visible; the user clicks on the back and forward buttons to change the current month: |
When the user selects a date by clicking on a specific day in the calendar or clicking the Today button, the DatePicker component disappears and the selected date appears in the text box; in this example, the user clicked on 31: |
If the user clicks on the button again, the calendar is initialized to the date shown in the text box.
|
 |
 |
 |
 |
DatePicker can also be used independent of DateEdit by changing its default selection behavior:
JDialog dlg = new JDialog(new Frame(), true);
DatePicker dp = new DatePicker();
dp.setHideOnSelect(false);
dlg.getContentPane().add(dp);
dlg.pack();
dlg.show();
System.out.println(dp.getDate().toString());
DatePicker and DateEdit are part of the Java Date Picker project registered at SourceForge.
|