![]() ![]() ![]() |
5
JCPopupCalendar Component
Features of JCPopupCalendar
Classes
Constructors and Methods
Listeners and Events
Examples
5.1 Features of JCPopupCalendar
JCPopupCalendar
is a component that allows you to edit the date and time using a drop-down calendar. In its editable form, the popup calendar displays a text field with a button next to it. Pushing on the button pops down a calendar from which a date and time can be selected. By default, the calendar has spinboxes for the year, month, and time along with a table which displays the days of the month. The day table updates each time the year and month are changed with the mouse clicks. The time spinbox allows editing of the hour, minute, second, and meridiem.
JCPopupCalendar
is an extension ofJComboBox
. Instead of selecting an item from a drop-down list, the user selects a date/time value using a popup calendar editor.JCPopupCalendar
uses aJFormattedTextField
that is configured to edit dates as its text editor. The text editor's value is kept in sync with the popup calendar editor's value, so changing one will automatically update the other. As withJComboBox
,JCPopupCalendar
is non-editable by default. In this case, the text field is replaced with a button which when selected activates the popup calendar editor. The popup calendar editor can still change the value in the non-editable case.Note: This component can only be used with JDK 1.4 and above. Those using JDKs prior to JDK 1.4 can use
JCPopupField
which is a part of JClass Field.
Figure 28 : A sample popup calendar.
The default value for a
JCPopupCalendar
component is the current date and time.
5.2 Classes
The pertinent classes to
JCPopupCalendar
are:
Creates the component that the popup calendar editor displays to the user.
This contains the
JCDateChooser
andTimeSpin
components that are manipulated by the user to select a new date and/or time.Creates the actual calendar popup editor.
This class is responsible for implementing the
JCPopupCalendarEditor
interface, containing theDateTimeChooser
, and communicating with the actual popup object.Allows the date to be edited. This component can be configured to use various formats. For more information, see Features of JCDateChooser, in Chapter 4.
Gets passed to
JCPopupListeners
when the value is committed from the popup calendar toJCPopupCalendar
.
5.3 Properties
5.4 Constructors and Methods
JCPopupCalendar Constructors
JCPopupCalendar
's constructor constructs a popup calendar, where the default date and time can be configured, as well as the locale and calendar type.
5.5 Listeners and Events
The
JCPopupListener
listens forJCPopup
events, which are generated when the calendar popup editor's value is committed toJCPopupCalendar
and the popup is popped down.JCPopupEvent
has the following methods:
5.6 Examples
Please refer to
examples.elements.CalendarPopup.java
to see a working popup calendar, or refer toexamples.elements.CalendarDialog.java
to see a how to use theDateTimeChooser
component in a dialog editor.The following code produces a screen with three possible popup calendars: one in English, one in French, and one in Spanish.
import com.klg.jclass.swing.JCPopupCalendar;
import com.klg.jclass.util.swing.JCExitFrame;
import com.klg.jclass.util.swing.JCAlignLayout;
import com.klg.jclass.util.JCEnvironment;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.Locale;
import java.util.Date;
public class CalendarPopup extends JPanel {
protected JCPopupCalendar popup1, popup2, popup3;
public CalendarPopup()
{
// Set the layout
setLayout(new BorderLayout());
// Place all the popup fields in a panel
JPanel p = new JPanel();
add(p, BorderLayout.CENTER);
JCAlignLayout mgr = new JCAlignLayout(2, 3, 3);
p.setLayout(mgr);
p.setBorder(new TitledBorder("JClass Elements JCCalendarPopup"));
//
// Example of a Date/Time JCPopupCalendar in English
//
Locale locale = new Locale("en", "US");
popup1 = new JCPopupCalendar(JCPopupCalendar.DATE_TIME, new Date(),
locale);
popup1.setEditable(true);
Component c = popup1.getEditor().getEditorComponent();
if (c instanceof JTextField) {
((JTextField)c).setColumns(15);
}
p.add(new JLabel("Date Time Editor (English): "));
p.add(popup1);
mgr.setResizeWidth(popup1, true);
//
// Example of a Date JCPopupCalendar in French
//
locale = new Locale("fr", "FR");
popup2 = new JCPopupCalendar(JCPopupCalendar.DATE, new Date(), locale);
popup2.setEditable(true);
p.add(new JLabel("Date Editor (French): "));
p.add(popup2);
mgr.setResizeWidth(popup2, true);
//
// Example of a non-editable Date/Time JCPopupCalendar in Spanish.
//
locale = new Locale("es", "ES");
popup3 = new JCPopupCalendar(JCPopupCalendar.DATE_TIME, new Date(),
locale);
popup3.setEditable(false);
p.add(new JLabel("Date Time Editor (Spanish): "));
p.add(popup3);
mgr.setResizeWidth(popup3, true);
}
public static void main(String[] args)
{
if (JCEnvironment.getJavaVersion() < 140) {
System.err.println("\nThis example is incompatible " +
"with JDKs prior to 1.4.0
System.exit(1);
}
JCExitFrame frame = new JCExitFrame("JCPopupCalendar Examples");
CalendarPopup t = new CalendarPopup();
frame.getContentPane().add(t);
frame.pack();
frame.show();
}
}
![]() ![]() ![]() |