JClass Elements

PreviousNextIndex

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 of JComboBox. Instead of selecting an item from a drop-down list, the user selects a date/time value using a popup calendar editor. JCPopupCalendar uses a JFormattedTextField 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 with JComboBox, 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:

JCPopupCalendar

Creates the JCPopupCalendar component.

JCPopupCalendarEditor

Interface that the popup editor must implement.

JCPopupCalendarBeanInfo

Contains the JCPopupCalendar bean information.

DateTimeChooser

Creates the component that the popup calendar editor displays to the user.

This contains the JCDateChooser and TimeSpin components that are manipulated by the user to select a new date and/or time.

DateTimePopup

Creates the actual calendar popup editor.

This class is responsible for implementing the JCPopupCalendarEditor interface, containing the DateTimeChooser, and communicating with the actual popup object.

JCDateChooser

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.

TimeSpin

Allows the time to be edited with a spinner.

JCPopupListener

Responds to events that happen in the popup.

JCPopupEvent

Gets passed to JCPopupListeners when the value is committed from the popup calendar to JCPopupCalendar.


5.3 Properties

Method

Type

Description

calendarType

int Calendar type. This must be one of the following:
  • JCPopupCalendar.DATE_TIME (default): indicates that both the date and time can be edited.
  • JCPopupCalendar.DATE: indicates that only the date can be edited.
  • hidePopupOnDayTableClick

    boolean If the value is TRUE, the popup calendar editor will pop down when a day is selected. If the value is FALSE (default), the day can be changed without the editor popping down, and the calendar will pop down when it is double-clicked.

    maximumDate

    java.util.Date Maximum date value. If a date is provided, the popup calendar cannot be set later than that value. Default is null, meaning that there is no maximum.

    minimumDate

    java.util.Date Minimum date value. If a date is provided, the popup calendar cannot be set earlier than that value. Default is null, meaning that there is no minimum.

    popupEditor

    JCPopupCalendarEditor Current calendar popup editor. Default is DateTimePopup. The DateTimeChooser is retrieved through this property, allowing for other properties to be set (for example, chooserType). Note: It is not recommended that you build your own popupEditor, but that you customize the one that is provided.

    showApplyButton

    boolean If the value is TRUE, an Apply button is available on the calendar which, when selected, will commit the current value and will pop down the editor. This may be useful if the hidePopupOnDayTableClick property is FALSE, but it is still desirable to provide another way to dismiss the popup, other than double clicking on the day table. If the value is FALSE (default), the Apply button is not available.

    showPopupOnUpDownArrow

    boolean If the value is TRUE (default), the calendar popup editor will pop up when the up or down arrow is selected in the text editor. If the value is FALSE, the calendar popup editor will not pop up when the up or down arrow is selected in the text editor.

    value

    java.util.Date Current value of the component. Defaults to the current date and time.


    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.

    Constructor

    Description

    JCPopupCalendar()

    Constructs a JCPopupCalendar.DATE_TIME calendar type, with the current date, the current time, and the default locale selected.

    public JCPopupCalendar
    (int calendarType)

    Constructs a JCPopupCalendar of the given calendar type with the current date, the current time, and the default locale selected.

    public JCPopupCalendar
    (Date d)

    Constructs a JCPopupCalendar.DATE_TIME calendar type, with the default locale and provided date and time.

    public JCPopupCalendar (int calendarType, Date d)

    Construct a JCPopupCalendar of the given type with the default locale and provided date and time.

    public JCPopupCalendar (Date d, Locale l)

    Constructs a JCPopupCalendar.DATE_TIME calendar type, with a specified locale and provided date and time.

    public JCPopupCalendar (int calendarType, Date d, Locale l)

    Constructs a JCPopupCalendar of the calendar type, with a specified locale and provided date and time.


    5.5 Listeners and Events

    The JCPopupListener listens for JCPopup events, which are generated when the calendar popup editor's value is committed to JCPopupCalendar and the popup is popped down. JCPopupEvent has the following methods:

    Method

    Description

    getSource()

    The source of the event which is the DateTimePopup object.

    getNewValue()

    The new value to be committed.


    5.6 Examples

    Please refer to examples.elements.CalendarPopup.java to see a working popup calendar, or refer to examples.elements.CalendarDialog.java to see a how to use the DateTimeChooser 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();
    }

    }


    PreviousNextIndex