![]() ![]() ![]() |
4
Date Chooser
Features of JCDateChooser
Classes and Interfaces
Properties
Methods
Examples
4.1 Features of JCDateChooser
JCDateChooser
is a component that displays a calendar in one of four variant forms. Each one displays the days of the month in the familiar form of a calendar, but varies the ways that the month and year are displayed.
- Spin Popdown
The year is shown in a spin box; the month is shown in a popdown.
- Dual Spin
Spin boxes are used to display both the year and the month.
- Quick Select
The year is shown in a spin box; a table is used to display all twelve months. One of the months may be highlighted to indicate that it has been selected.
- Read Only
The year and month are shown in non-editable fields. The table showing the days of the month is read-only as well. Selected "special" dates still appear highlighted.
- Like the standard Swing components,
JCDateChooser
provides for the optional use of a Tool Tip.The accompanying figure shows the full component in its Quick Select form, so the year is shown in a spin box, while tables are used to show months and days of the month. Note that special days, in this case Saturdays and Sundays, are distinguishable from the others. You can observe the other calendar styles by running the example called examples.elements.DateChooser.java.
Figure 27 : A JCDateChooser.
4.2 Classes and Interfaces
Classes in the com.klg.jclass.util.calendar Package
4.2.1 The CalendarComponent Interface
The calendar component uses a single model for the day, month, and year.
The methods declared in
public interface CalendarComponent
are:
public void setCalendarModel(JCValueModel model)
Sets the model which provides the current date being used by the calendar component.
public void setSpecialDates(JCCalendar special_dates
Sets the special dates being used by the calendar component.
public void setLocale(Locale locale)
;
Sets the locale being used by the calendar component.public void addActionListener(ActionListener l)
Adds an action listener to detect specific actions on this component.
public void removeActionListener(ActionListener l)
Removes action listener to detect specific actions on this component.
4.2.2 The SpecialDate Interface
This interface has only one method:
boolean isSpecialDate(int year, int month, int date, int week)You'll note that the numeric value for the week (1 - 52) is a redundant parameter in
isSpecialDate
. This is done for efficiency's sake. If you implement theSpecialDate
interface, you will have to supply a numeric value for the week even though it is possible to compute it from the first three parameters inisSpecialDate
. Note that you useJCCalendar
'sdayofweek()
method to calculate this value.
4.3 Properties
Properties of JCDateChooser
Properties of JCCalendar
For a full listing of the properties, see Appendix A.
4.4 Methods
JCDateChooser
There are four visual aspects to the date chooser: Quick Select, Dual Spin, Spin Popdown, and Read Only. Use
setChooserType(int type)
to select the type you want to display.type
is one ofJCDateChooser.DUAL_SPIN
,JCDateChooser.QUICK_SELECT
,JCDateChooser.READ_ONLY
, orJCDateChooser.SPIN_POPDOWN.
The
CalendarComponent
interface provides the mechanism for extracting parts of aJCDateChooser
date. The methods aregetDayComponent()
,getMonthComponent()
, andgetYearComponent()
.As noted in the section on properties, you set minimum and maximum dates by providing
setMinimumDate()
andsetMaximumDate()
with ajava.util.Calendar
object.Set the currently selected date programmatically with
setValue()
, or determine what its value is withgetValue()
. The parameter is once again ajava.util.Calendar
object.JCCalendar
While not subclassed from
java.util.Calendar
,JCCalendar
is used in conjunction with it to provide for a classification of some dates as "special." Special days are managed through these methods:addSpecialDate()
andremoveSpecialDate()
, which take aSpecialDate
object as a parameter. andisSpecialDate()
. There is no restriction on how many dates may be deemed special.The class contains a number of utility methods. One, called
isLeapYear()
, can be used to determine if any given year is a leap year. WithdayOfWeek()
, you can determine the day of the week given a year, month, and day. You can clone aCalendar
object usingcopyCalendar()
.Certain applications involving calendars require that certain days are treated specially. For example, some businesses that are open on the weekend close on Mondays. In such a case, it is useful to be able to lump all Mondays together and classify them as days when the store is closed. Perhaps the store's founder always holds a sale on his birthday, the 29th of February. In this and similar cases, it's useful to be able to denote anniversary days that occur on the same date every year. There are other days, such as Labor day, which is defined as the first Monday in September.
DayOfWeek sunday = new DayOfWeek(0);JCCalendar
contains inner classesDayOfWeek
,MonthDayOfMonth
,MonthWeekDayOfWeek
to help you deal with these situations. These classes allow you to store various calendar objects of the types just mentioned. The first of these allows you to store a day, like Sunday, by declaring an instance variableFrom the example, you see that the seven days of the week are mapped using a zero-based index.
To store a date like July 4, use:
MonthDayOfMonth july4 = new MonthDayOfMonth(7, 4)To store a date like Labor Day, use:
MonthWeekDayOfWeek laborDay = new MonthWeekDayOfWeek(9, 1, 1)
4.5 Examples
The illustrative code snippets shown here demonstrate how you can create special days and how you can set bounds on the permissible dates. Refer to the Date Chooser example, automatically installed into com/klg/jclass/examples/elements/ when you install JClass Elements, for the complete example.
//The location of JCDateChooser
import com.klg.jclass
.util.calendar.*;
//Create an instance of JCDateChooser within your class.
dateChooser = new JCDateChooser();
...
//Create a "special day"
JCCalendar special_dates = new JCCalendar();
// Make Sundays special days
special_dates.addSpecialDate(new JCCalendar.DayOfWeek(0));
...
dateChooser.setSpecialDates(special_dates);
...
//Set bounds for the calendar
Calendar max = Calendar.getInstance();
max.set(max.YEAR, 2050);
dateChooser.setMaximumDate(max);
![]() ![]() ![]() |