Java Reference
In-Depth Information
Figure 16.8 The calendar application
contains the day numbers. It cannot be edited but its content changes depending
on the setting of month and year. The next combo box contains the month names.
It is not editable; we do not allow the user to invent new months. The third combo
box contains a list of years from 2004 to 2007. It is editable so the user can select
another year by entering its number.
The month box is filled by passing an array with the months' names to the
box in the constructor. The day and year boxes are filled in method initBoxes
by adding the items in for-loops. Also in this method the visible length of boxes
are set. For the day box the value is 10 so that a scroll bar will appear in the
list.
A listener class CalendarListener is defined as an internal class. A Calen-
darListener is associated with the combo boxes for year and month. If an entry
is selected in one of these boxes (or a new year is entered by the user) then the
listener's actionPerformed method is engaged. It has to adjust the days displayed
in the day combo box depending on the currently selected year and month. It does
so by first determining the year and month. From this the length of the month is
computed as one of 28, 29, 30 or 31. The actual adjustment of the day combo box
is done by method setDayListTo .Itcompares the current length of the list of the
day combo box with the desired one. If the list is too short the appropriate entries
are added; if it is too long the last entries are removed.
The method to adjust the length of February is only correct from 1901 to 2099.
If the user enters a number outside this range, then the listener sets the year to
2000. The code for CalendarPanel is listed below. The test program consists of
a frame ( ComboBoxFrame ) and a panel ( DisplayDatePanel ). A CalendarPanel is
centrally embedded into the frame and a DisplayDatePanel is embedded to the
south. The frame also has a button labelled 'OK' on the right (east). If clicked,
the date currently selected in the CalendarPanel is displayed in the Display-
DatePanel .Wedonot list the latter classes; they can be downloaded from the
topic's home page. A screen shot is shown in Figure 16.8.
File: its/ComboBox/CalendarPanel.java
1. package its.ComboBox;
2.
3. import java.awt.GridLayout;
4. import java.awt.event.ActionEvent;
Search WWH ::




Custom Search