org.freixas.jcalendar
Class JCalendar

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JPanel
                  extended by org.freixas.jcalendar.JCalendar
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible

public class JCalendar
extends JPanel

This class displays a panel through which a user can select a date and/or time. A time-only selection can be used to select a duration, as long as the duration is no longer than 23 hours, 59 minutes and 59 seconds.

The date is selected using a calendar display. The time is selected using a date spinner.

In reality, both date and time are part of the same Date returned by getDate() or getCalendar(). If you are selecting only the date, you should ignore the time portion of the Date or Calendar. If you are selecting only the time, ignore the date portion.

You can set the pattern used to display the time in the date spinner. The pattern is the same as used by SimpleDateFormat. The default format displays hours, minutes and seconds in a locale-specific way (some locales use AM/PM, some use a 24-hour clock). If you want to get a time duration, you will want to use a pattern such as "HH:mm:ss" to eliminate the possibility of an AM/PM field appearing. You can also use setTimePattern() to reduce the precision of the time obtained (e.g. "HH:mm").

You may specify a font for each of the elements that make up the calendar. If you do not specify a font (or specify a null font), a reasonable default will be generated based on the current Look & Feel.

When the calendar has focus, the following key bindings are supported:

This is in addition to using Tab and Enter to move through and select the buttons.

The time field is divided into hour, minute, second and AM/PM portions. You can select any portion and use the spinner arrows on the right side of the field to increment or decrement that portion. However, the entire time is being incremented or decremented, so that incrementing 1:59:59 by one second will generate 2:00:00.

Due to a design limitation in JFormatedTextField, incrementing 24:59:59 will not increment the day. A value in a JFormatedTextField (which is what the time field is), only calculates a date from the fields displayed. Since usually we display a HH:mm:ss pattern, the JFormattedTextField will set the date to a default value, not influenced by the date in the calendar

It is possible to pass in a time pattern that displays more than just the time -- this is not advisable since the date portion displayed in the JFormatedTextField will be ignored by the JCalendar component.

When the time field has focus, the up/down arrow keys increment or decrement the currently selected time portion, just like the spinner keys. The left and right arrow keys can be used to move to the next or previous portion.

Author:
Antonio Freixas
See Also:
Calendar, Date, DateFormat, SimpleDateFormat, Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JPanel
JPanel.AccessibleJPanel
 
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
static int DISPLAY_DATE
          Used to indicate that this component should display the date.
static int DISPLAY_TIME
          Used to indicate that this component should display the time.
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
JCalendar()
          Create an instance of JCalendar using the default calendar and locale.
JCalendar(Calendar calendar, Locale locale, int selectedComponents, boolean isTodayDisplayed)
          Create an instance of JCalendar using the given calendar and locale.
JCalendar(Calendar calendar, Locale locale, int selectedComponents, boolean isTodayDisplayed, String timePattern)
          Create an instance of JCalendar using the given calendar and locale.
JCalendar(int selectedComponents, boolean isTodayDisplayed)
          Create an instance of JCalendar using the default calendar and locale.
 
Method Summary
 void addDateListener(DateListener listener)
          Add a date listener.
 void addNotify()
          
protected  void fireDateChange()
          Fire a date change.
 Calendar getCalendar()
          Get a copy of the calendar used by this JCalendar.
 Date getDate()
          Get the date currently displayed by the calendar panel.
 Font getDayFont()
          Get the day font.
 Font getDayOfWeekFont()
          Get the day-of-week font (Mon, Tue, etc.).
 Locale getLocale()
          Return the locale used by this JCalendar.
 int getSelectedComponents()
          Return the components being displayed: (getSelectedComponents() & DISPLAY_DATE) > 0 means that the date calendar is being displayed.
 Font getTimeFont()
          Get the time spinner font.
 String getTimePattern()
          Get the pattern used to display the time in the time selection spinner.
 Font getTitleFont()
          Get the title font.
 Font getTodayFont()
          Get the font used to display today's date as text.
 boolean isNullAllowed()
          Get whether a null date is allowed.
 boolean isTodayDisplayed()
          Returns true if today's date is displayed at the bottom of the calendar.
protected  String paramString()
          
 void removeDateListener(DateListener listener)
          Remove a date listener.
 void setDate(Date date)
          Set the calendar panel to display the given date.
 void setDayFont(Font font)
          If the font is set to null, then the day font will default to 9/11th's of the L&F's Button default font.
 void setDayOfWeekFont(Font font)
          If the font is set to null, then the day-of-week font (Mon, Tue, etc.) will default to 9/11th's of the L&F's Label default font.
 void setDisplayDate(Date date)
          Reset the displayed date without changing the selected date.
 void setEnabled(boolean b)
          
 void setNullAllowed(boolean isNullAllowed)
          Set whether a null date is allowed.
 void setTimeFont(Font font)
          If the font is set to null, then the time spinner font will default to the L&F's Spinner default font.
 void setTitleFont(Font font)
          If the font is set to null, then the title font (for the Month Year title) will default to the L&F's Label default font.
 void setTodayFont(Font font)
          If the font is set to null, then the font used to display today's date as text will default to the L&F's Label default font.
protected  void setupDayFonts()
          Set the day labels' font.
protected  void setupDayOfWeekFonts()
          Set the day-of-week labels' font.
protected  void setupTimeFont()
          Set the time spinner's font.
protected  void setupTitleFont()
          Set the title's font.
protected  void setupTodayFont()
          Set the font used to display today's date as text.
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DISPLAY_DATE

public static final int DISPLAY_DATE
Used to indicate that this component should display the date.

See Also:
Constant Field Values

DISPLAY_TIME

public static final int DISPLAY_TIME
Used to indicate that this component should display the time.

See Also:
Constant Field Values
Constructor Detail

JCalendar

public JCalendar()
Create an instance of JCalendar using the default calendar and locale. Display the date but not the time. Don't display today's date at the bottom of the panel.


JCalendar

public JCalendar(int selectedComponents,
                 boolean isTodayDisplayed)
Create an instance of JCalendar using the default calendar and locale. Display a calendar and/or a time spinner as requested (to display both use DISPLAY_DATE | DISPLAY_TIME). Display today's date if requested. Use the defult pattern to display the time in the time spinner field (if there is one).

Parameters:
selectedComponents - Use DISPLAY_DATE, DISPLAY_TIME or (DISPLAY_DATE | DISPLAY_TIME).
isTodayDisplayed - True if today's date should be displayed at the bottom of the panel.

JCalendar

public JCalendar(Calendar calendar,
                 Locale locale,
                 int selectedComponents,
                 boolean isTodayDisplayed)
Create an instance of JCalendar using the given calendar and locale. Display a calendar and/or a time spinner as requested (to display both use DISPLAY_DATE | DISPLAY_TIME). Display today's date if requested. Use the default pattern to display the time in the time spinner field (if there is one).

Parameters:
calendar - The calendar to use.
locale - The locale to use.
selectedComponents - Use DISPLAY_DATE, DISPLAY_TIME or (DISPLAY_DATE | DISPLAY_TIME).
isTodayDisplayed - True if today's date should be displayed at the bottom of the panel.

JCalendar

public JCalendar(Calendar calendar,
                 Locale locale,
                 int selectedComponents,
                 boolean isTodayDisplayed,
                 String timePattern)
Create an instance of JCalendar using the given calendar and locale. Display a calendar and/or a time spinner as requested (to display both use DISPLAY_DATE | DISPLAY_TIME). Display today's date if requested. Set the pattern used to display the time in the time spinner field (if there is one). If null, use the default MEDIUM format for the given locale. Patterns are from DateFormat and SimpleDateFormat.

Parameters:
calendar - The calendar to use.
locale - The locale to use.
selectedComponents - Use DISPLAY_DATE, DISPLAY_TIME or (DISPLAY_DATE | DISPLAY_TIME).
isTodayDisplayed - True if today's date should be displayed at the bottom of the panel.
timePattern - The pattern used to display the time in the time spinner field.
See Also:
DateFormat, SimpleDateFormat
Method Detail

addDateListener

public void addDateListener(DateListener listener)
Add a date listener. This listener will receive events each time the selected date changes.

Parameters:
listener - The date listener to add.

removeDateListener

public void removeDateListener(DateListener listener)
Remove a date listener.

Parameters:
listener - The date listener to remove.

isNullAllowed

public boolean isNullAllowed()
Get whether a null date is allowed.

Returns:
Whether a null date is allowed.

setNullAllowed

public void setNullAllowed(boolean isNullAllowed)
Set whether a null date is allowed. A null date means that no date is selected. The user can select a null date by pressing DELETE anywhere within the calendar.

If nulls are not allowed, a setDate(null) will be ignored without error. The DELETE key will do nothing.

If you switch from allowing nulls to not allowing nulls and the current date is null, it will remain null until a date is selected.

The component default is to allow nulls.

Parameters:
isNullAllowed - Whether a null date is allowed.

getDate

public Date getDate()
Get the date currently displayed by the calendar panel. If no date is selected, null is returned.

Returns:
The date currently displayed.
See Also:
getCalendar()

setDate

public void setDate(Date date)
Set the calendar panel to display the given date. This will fire a DateEvent. The date may be null. If isNullAllowed() is true, then all dates will be unselected. If isNullAllowed() is false, a null date is ignored.

Parameters:
date - The date to set.

setDisplayDate

public void setDisplayDate(Date date)
Reset the displayed date without changing the selected date. No event occurs. A null date will reset to today's date. The display date simply selects the calendar page (month/year) to display.

Parameters:
date - The date to display.

getTimePattern

public String getTimePattern()
Get the pattern used to display the time in the time selection spinner. This is null if the time is not displayed.

Returns:
The pattern used to display the time in the time selection spinner.

getCalendar

public Calendar getCalendar()
Get a copy of the calendar used by this JCalendar. This calendar will be set to the currently selected date, so it is an alternative to the getDate() method. If no date is selected (getDate() returns null), the calendar's selected date is 9/9/9999 and should not be used.

Returns:
A copy of the calendar used by JCalendar.
See Also:
getDate()

getLocale

public Locale getLocale()
Return the locale used by this JCalendar.

Overrides:
getLocale in class Component
Returns:
The locale used by this JCalendar.

getSelectedComponents

public int getSelectedComponents()
Return the components being displayed: (getSelectedComponents() & DISPLAY_DATE) > 0 means that the date calendar is being displayed. (getSelectedComponents() & DISPLAY_TIME) > 0 menas that the time spinner field is being displayed.

Returns:
The selected components.

isTodayDisplayed

public boolean isTodayDisplayed()
Returns true if today's date is displayed at the bottom of the calendar.

Returns:
True if today's date is displayed at the bottom of the calendar.

getTitleFont

public Font getTitleFont()
Get the title font.

Returns:
The title font.

setTitleFont

public void setTitleFont(Font font)
If the font is set to null, then the title font (for the Month Year title) will default to the L&F's Label default font.

Otherwise, the title font is set as given.

Parameters:
font - The font to set.

getDayOfWeekFont

public Font getDayOfWeekFont()
Get the day-of-week font (Mon, Tue, etc.).

Returns:
The day-of-week font.

setDayOfWeekFont

public void setDayOfWeekFont(Font font)
If the font is set to null, then the day-of-week font (Mon, Tue, etc.) will default to 9/11th's of the L&F's Label default font.

Otherwise, the day-of-week font is set as given.

Parameters:
font - The font to set.

getDayFont

public Font getDayFont()
Get the day font.

Returns:
The day font.

setDayFont

public void setDayFont(Font font)
If the font is set to null, then the day font will default to 9/11th's of the L&F's Button default font.

Otherwise, the day font is set as given.

Parameters:
font - The font to set.

getTimeFont

public Font getTimeFont()
Get the time spinner font.

Returns:
The time spinner font.

setTimeFont

public void setTimeFont(Font font)
If the font is set to null, then the time spinner font will default to the L&F's Spinner default font.

Otherwise, the time spinner font is set as given.

Parameters:
font - The font to set.

getTodayFont

public Font getTodayFont()
Get the font used to display today's date as text.

Returns:
The font used to display today's date.

setTodayFont

public void setTodayFont(Font font)
If the font is set to null, then the font used to display today's date as text will default to the L&F's Label default font.

Otherwise, the font used to display today's date is set as given.

Parameters:
font - The font to set.

setEnabled

public void setEnabled(boolean b)

Overrides:
setEnabled in class JComponent

addNotify

public void addNotify()

Overrides:
addNotify in class JComponent

paramString

protected String paramString()

Overrides:
paramString in class JPanel

fireDateChange

protected void fireDateChange()
Fire a date change. This notifies all listeners that the date has changed.


setupTitleFont

protected void setupTitleFont()
Set the title's font.


setupDayOfWeekFonts

protected void setupDayOfWeekFonts()
Set the day-of-week labels' font.


setupDayFonts

protected void setupDayFonts()
Set the day labels' font.


setupTimeFont

protected void setupTimeFont()
Set the time spinner's font.


setupTodayFont

protected void setupTodayFont()
Set the font used to display today's date as text.


Copyright © 2004-2005 Antonio Freixas
Licensed under the Artistic License