public class JCalendar
extends javax.swing.JPanel
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:
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.
Calendar
,
Date
,
DateFormat
,
SimpleDateFormat
,
Serialized Formjavax.swing.JPanel.AccessibleJPanel
javax.swing.JComponent.AccessibleJComponent
Modifier and Type | Field and Description |
---|---|
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.
|
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
Constructor and Description |
---|
JCalendar()
Create an instance of JCalendar using the default calendar and
locale.
|
JCalendar(java.util.Calendar calendar,
java.util.Locale locale,
int selectedComponents,
boolean isTodayDisplayed)
Create an instance of JCalendar using the given calendar and
locale.
|
JCalendar(java.util.Calendar calendar,
java.util.Locale locale,
int selectedComponents,
boolean isTodayDisplayed,
java.lang.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.
|
Modifier and Type | Method and Description |
---|---|
void |
addDateListener(DateListener listener)
Add a date listener.
|
void |
addNotify() |
protected void |
fireDateChange()
Fire a date change.
|
org.joda.time.DateTime |
getDate()
Get the date currently displayed by the calendar panel.
|
java.awt.Font |
getDayFont()
Get the day font.
|
java.awt.Font |
getDayOfWeekFont()
Get the day-of-week font (Mon, Tue, etc.).
|
java.util.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. |
java.awt.Font |
getTimeFont()
Get the time spinner font.
|
java.lang.String |
getTimePattern()
Get the pattern used to display the time in the time selection
spinner.
|
java.awt.Font |
getTitleFont()
Get the title font.
|
java.awt.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 java.lang.String |
paramString() |
void |
removeDateListener(DateListener listener)
Remove a date listener.
|
void |
setDate(org.joda.time.DateTime date)
Set the calendar panel to display the given date.
|
void |
setDayFont(java.awt.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(java.awt.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(org.joda.time.DateTime 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(java.awt.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(java.awt.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(java.awt.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.
|
java.lang.String |
toString() |
getAccessibleContext, getUI, getUIClassID, setUI, updateUI
addAncestorListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, 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, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, 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
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, transferFocusDownCycle, validate, validateTree
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, 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, transferFocus, transferFocusBackward, transferFocusUpCycle
public static final int DISPLAY_DATE
public static final int DISPLAY_TIME
public JCalendar()
public JCalendar(int selectedComponents, boolean isTodayDisplayed)
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.public JCalendar(java.util.Calendar calendar, java.util.Locale locale, int selectedComponents, boolean isTodayDisplayed)
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.public JCalendar(java.util.Calendar calendar, java.util.Locale locale, int selectedComponents, boolean isTodayDisplayed, java.lang.String timePattern)
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.DateFormat
,
SimpleDateFormat
public void addDateListener(DateListener listener)
listener
- The date listener to add.public void removeDateListener(DateListener listener)
listener
- The date listener to remove.public boolean isNullAllowed()
public void setNullAllowed(boolean isNullAllowed)
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.
isNullAllowed
- Whether a null date is allowed.public org.joda.time.DateTime getDate()
public void setDate(org.joda.time.DateTime date)
date
- The date to set.public void setDisplayDate(org.joda.time.DateTime date)
date
- The date to display.public java.lang.String getTimePattern()
public java.util.Locale getLocale()
getLocale
in class java.awt.Component
public int getSelectedComponents()
(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.public java.lang.String toString()
toString
in class java.awt.Component
public boolean isTodayDisplayed()
public java.awt.Font getTitleFont()
public void setTitleFont(java.awt.Font font)
Otherwise, the title font is set as given.
font
- The font to set.public java.awt.Font getDayOfWeekFont()
public void setDayOfWeekFont(java.awt.Font font)
Otherwise, the day-of-week font is set as given.
font
- The font to set.public java.awt.Font getDayFont()
public void setDayFont(java.awt.Font font)
Otherwise, the day font is set as given.
font
- The font to set.public java.awt.Font getTimeFont()
public void setTimeFont(java.awt.Font font)
Otherwise, the time spinner font is set as given.
font
- The font to set.public java.awt.Font getTodayFont()
public void setTodayFont(java.awt.Font font)
Otherwise, the font used to display today's date is set as given.
font
- The font to set.public void setEnabled(boolean b)
setEnabled
in class javax.swing.JComponent
public void addNotify()
addNotify
in class javax.swing.JComponent
protected java.lang.String paramString()
paramString
in class javax.swing.JPanel
protected void fireDateChange()
protected void setupTitleFont()
protected void setupDayOfWeekFonts()
protected void setupDayFonts()
protected void setupTimeFont()
protected void setupTodayFont()