org.jdesktop.swingx
public class JXDatePicker extends javax.swing.JComponent
The date selection is controlled by the JXMonthView's DateSelectionModel. This allows the use of all its functionality in the JXDatePicker as well. F.i. restrict the selection to a date in the current or next week:
Appointment appointment = new Appointment(director,
"Be sure to have polished shoes!");
JXDatePicker picker = new JXDatePicker();
Calendar calendar = picker.getMonthView().getCalendar();
// starting today if we are in a hurry
calendar.setTime(new Date());
picker.getMonthView().setLowerBound(calendar.getTime());
// end of next week
CalendarUtils.endOfWeek(calendar);
calendar.add(Calendar.WEEK_OF_YEAR);
picker.getMonthView().setUpperBound(calendar.getTime());
Similar to a JXMonthView, the JXDatePicker fires an ActionEvent when the user
actively commits or cancels a selection. Interested client code can add a
ActionListener to be notified by the user action.
JXDatePicker picker = new JXDatePicker(new Date());
ActionListener l = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (JXDatePicker.COMMIT_KEY.equals(e.getActionCommand)) {
saveDate(picker.getDate());
}
}
};
picker.addActionListener(l);
Note that ActionListener will not be notified if the user
edits the date text without hitting the Enter key afterwards. To detect both kinds of
date change, interested client code can add a PropertyChangeListener.
JXDatePicker picker = new JXDatePicker(new Date());
PropertyChangeListener listener = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
if ("date".equals(e.getPropertyName()) {
saveDate(picker.getDate());
}
}
};
picker.addPropertyChangeListener(listener);
The DateFormats used in the JXDatePicker's are initialized to the default formats of the DatePickerFormatter, as defined by the picker's resourceBundle DatePicker.properties. Application code can overwrite the picker's default
picker.setDateFormats(myCustomFormat, myAlternativeCustomFormat);
PENDING JW: explain what the alternatives are for (after understanding it
myself ;-)
The selected Date is a bound property of the JXDatePicker. This allows easy binding to a property of a custom bean when using a binding framework.
Keybindings (as installed by the UI-Delegate)
JXMonthView
,
DateSelectionModel
,
DatePickerFormatter
,
Serialized Formjavax.swing.JComponent.AccessibleJComponent
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
CANCEL_KEY
action command used for cancel actionEvent.
|
static java.lang.String |
COMMIT_KEY
action command used for commit actionEvent.
|
static java.lang.String |
EDITOR |
static java.lang.String |
HOME_COMMIT_KEY
action key for commit home action
|
static java.lang.String |
HOME_NAVIGATE_KEY
action key for navigate home action
|
protected boolean |
lightWeightPopupEnabled |
static java.lang.String |
LINK_PANEL |
static java.lang.String |
MONTH_VIEW |
static java.lang.String |
uiClassID
UI Class ID
|
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
Constructor and Description |
---|
JXDatePicker()
Intantiates a date picker with no selection and the default
DatePickerFormatter . |
JXDatePicker(java.util.Date selected)
Intantiates a date picker using the specified time as the initial
selection and the default
DatePickerFormatter . |
JXDatePicker(java.util.Date selection,
java.util.Locale locale)
Intantiates a date picker using the specified time as the initial
selection and the default
DatePickerFormatter . |
JXDatePicker(java.util.Locale locale)
Intantiates a date picker with no selection and the default
DatePickerFormatter . |
Modifier and Type | Method and Description |
---|---|
void |
addActionListener(java.awt.event.ActionListener l)
Adds an ActionListener.
|
void |
cancelEdit()
Cancels the editor's changes and notifies ActionListeners.
|
void |
commitEdit()
Commits the editor's changes and notifies ActionListeners.
|
protected void |
fireActionPerformed(java.lang.String actionCommand)
Fires an ActionEvent with the given actionCommand
to all listeners.
|
int |
getBaseline(int width,
int height)
Get the baseline for the specified component, or a value less
than 0 if the baseline can not be determined.
|
java.util.Date |
getDate()
Returns the currently selected date.
|
javax.swing.JFormattedTextField |
getEditor()
Returns the formatted text field used to edit the date selection.
|
java.awt.Font |
getFont()
Returns the font that is associated with the editor of this date picker.
|
java.text.DateFormat[] |
getFormats()
Returns an array of the formats used by the installed formatter
if it is a subclass of
JXDatePickerFormatter |
java.util.Date |
getLinkDay()
Returns the date shown in the LinkPanel.
|
protected java.text.MessageFormat |
getLinkFormat() |
javax.swing.JPanel |
getLinkPanel()
Return the panel that is used at the bottom of the popup.
|
<T extends java.util.EventListener> |
getListeners(java.lang.Class<T> listenerType) |
JXMonthView |
getMonthView()
Return the
JXMonthView used in the popup to
select dates from. |
java.util.TimeZone |
getTimeZone()
Gets the time zone.
|
DatePickerUI |
getUI()
Returns the look and feel (L&F) object that renders this component.
|
java.lang.String |
getUIClassID() |
boolean |
isEditable()
Returns the editable property.
|
boolean |
isEditValid()
Returns true if the current value being edited is valid.
|
boolean |
isLightWeightPopupEnabled()
Gets the value of the
lightWeightPopupEnabled
property. |
void |
removeActionListener(java.awt.event.ActionListener l)
Removes an ActionListener.
|
void |
setComponentOrientation(java.awt.ComponentOrientation orientation) |
void |
setDate(java.util.Date date)
Sets the date property.
|
void |
setEditable(boolean value)
Sets the editable property.
|
void |
setEditor(javax.swing.JFormattedTextField editor)
Sets the editor.
|
void |
setFont(java.awt.Font font)
Set the font for the editor associated with this date picker.
|
void |
setFormats(java.text.DateFormat... formats)
Replaces the currently installed formatter and factory used by the
editor.
|
void |
setFormats(java.lang.String... formats)
Replaces the currently installed formatter and factory used by the
editor.
|
void |
setLightWeightPopupEnabled(boolean aFlag)
Sets the
lightWeightPopupEnabled property, which
provides a hint as to whether or not a lightweight
Component should be used to contain the
JXDatePicker , versus a heavyweight
Component such as a Panel
or a Window . |
void |
setLinkDay(java.util.Date linkDay)
Sets the date shown in the TodayPanel.
|
void |
setLinkDay(java.util.Date linkDay,
java.lang.String linkFormatString)
Set the date the link will use and the string defining a MessageFormat
to format the link.
|
protected void |
setLinkFormat(java.text.MessageFormat _linkFormat) |
void |
setLinkPanel(javax.swing.JPanel linkPanel)
Set the panel that will be used at the bottom of the popup.
|
void |
setMonthView(JXMonthView monthView)
Set the component to use the specified JXMonthView.
|
void |
setTimeZone(java.util.TimeZone tz)
Sets the time zone with the given time zone value.
|
void |
setUI(DatePickerUI ui)
Sets the L&F object that renders this component.
|
protected void |
updateTimeZone(java.util.TimeZone oldValue,
java.util.TimeZone newValue)
Callback from monthView timezone changes.
|
void |
updateUI()
Resets the UI property with the value from the current look and feel.
|
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, 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, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, 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, setEnabled, setFocusTraversalKeys, 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, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, 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, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
public static final java.lang.String uiClassID
public static final java.lang.String EDITOR
public static final java.lang.String MONTH_VIEW
public static final java.lang.String LINK_PANEL
public static final java.lang.String COMMIT_KEY
public static final java.lang.String CANCEL_KEY
public static final java.lang.String HOME_NAVIGATE_KEY
public static final java.lang.String HOME_COMMIT_KEY
protected boolean lightWeightPopupEnabled
public JXDatePicker()
DatePickerFormatter
.
The date picker is configured with the default time zone and localesetTimeZone(java.util.TimeZone)
,
getTimeZone()
public JXDatePicker(java.util.Date selected)
DatePickerFormatter
.
The date picker is configured with the default time zone and localeselected
- the initially selected datesetTimeZone(java.util.TimeZone)
,
getTimeZone()
public JXDatePicker(java.util.Locale locale)
DatePickerFormatter
.
The date picker is configured with the default time zone and specified
localelocale
- initial LocalesetTimeZone(java.util.TimeZone)
,
getTimeZone()
public JXDatePicker(java.util.Date selection, java.util.Locale locale)
DatePickerFormatter
.
The date picker is configured with the default time zone and specified localeselection
- initially selected Datelocale
- initial LocalesetTimeZone(java.util.TimeZone)
,
getTimeZone()
public void setDate(java.util.Date date)
Does nothing if the ui vetos the new date - as might happen if the code tries to set a date which is unselectable in the monthView's context. The actual value of the new Date is controlled by the JXMonthView's DateSelectionModel. The default implementation normalizes the date to the start of the day in the model's calendar's coordinates, that is all time fields are zeroed. To keep the time fields, configure the monthView with a SingleDaySelectionModel.
At all "stable" (= not editing in date input field nor in the monthView) times the date is the same in the JXMonthView, this JXDatePicker and the editor. If a new Date is set, this invariant is enforced by the DatePickerUI.
This is a bound property.
date
- the new date to set.getDate()
,
DateSelectionModel
,
SingleDaySelectionModel
public java.util.Date getDate()
protected void updateTimeZone(java.util.TimeZone oldValue, java.util.TimeZone newValue)
NOTE: as timeZone is a bound property of this class we need to guarantee the propertyChangeNotification. As this class doesn't own this property it must listen to the owner (monthView) and re-fire the change.
oldValue
- the old timezone.newValue
- the new timezone.public DatePickerUI getUI()
public void setUI(DatePickerUI ui)
ui
- UI to use for this JXDatePicker
public void updateUI()
updateUI
in class javax.swing.JComponent
UIManager.getUI(javax.swing.JComponent)
public java.lang.String getUIClassID()
getUIClassID
in class javax.swing.JComponent
public void setFormats(java.lang.String... formats)
java.text.SimpleDateFormat
class. Note: The given formats are internally synched to the picker's current TimeZone.
formats
- zero or more not null string formats to use. Note that a
null array is allowed and resets the formatter to use the
localized default formats.java.lang.NullPointerException
- any array element is null.SimpleDateFormat
public void setFormats(java.text.DateFormat... formats)
Note: The given formats are internally synched to the picker's current TimeZone.
formats
- zero or more not null formats to use. Note that a
null array is allowed and resets the formatter to use the
localized default formats.java.lang.NullPointerException
- any of its elements is null.public java.text.DateFormat[] getFormats()
JXDatePickerFormatter.
javax.swing.JFormattedTextField.AbstractFormatter
and javax.swing.text.DefaultFormatter
do not have
support for accessing the formats used.
public JXMonthView getMonthView()
JXMonthView
used in the popup to
select dates from.public void setMonthView(JXMonthView monthView)
monthView
- month view comopnent.java.lang.NullPointerException
- if view component is nullsetTimeZone(java.util.TimeZone)
,
getTimeZone()
public java.util.TimeZone getTimeZone()
TimeZone
used by the JXMonthView
.public void setTimeZone(java.util.TimeZone tz)
PENDING JW: currently this property is the only property of the monthView which is exposed in this api. Not sure why it is here at all. It's asymetric (to the other properties) and as such should be either removed or the others which might be relevant to a datePicker exposed as well (probably hiding the monthView itself as an implementation detail of the ui delegate).
tz
- The TimeZone
.public java.util.Date getLinkDay()
PENDING JW: the property should be named linkDate - but that's held by the deprecated long returning method. Maybe revisit if we actually remove the other.
public void setLinkDay(java.util.Date linkDay, java.lang.String linkFormatString)
linkDay
- the Date to set on the LinkPanellinkFormatString
- String used to format the linkMessageFormat
public void setLinkDay(java.util.Date linkDay)
linkDay
- the date used in the TodayPanelprotected void setLinkFormat(java.text.MessageFormat _linkFormat)
_linkFormat
- the _linkFormat to setprotected java.text.MessageFormat getLinkFormat()
public javax.swing.JPanel getLinkPanel()
public void setLinkPanel(javax.swing.JPanel linkPanel)
linkPanel
- The new panel to install in the popuppublic javax.swing.JFormattedTextField getEditor()
Clients should NOT use this method. It is provided to temporarily support the PLAF code.
public void setEditor(javax.swing.JFormattedTextField editor)
The default is created and set by the UI delegate.
Clients should NOT use this method. It is provided to temporarily support the PLAF code.
editor
- the formatted input.java.lang.NullPointerException
- if editor is null.getEditor()
public void setComponentOrientation(java.awt.ComponentOrientation orientation)
setComponentOrientation
in class java.awt.Component
public boolean isEditValid()
public void commitEdit() throws java.text.ParseException
java.text.ParseException
- Throws parse exception if the date
can not be parsed.public void cancelEdit()
public void setEditable(boolean value)
value
- isEditable()
public boolean isEditable()
true
if the picker is editable; false
otherwisepublic java.awt.Font getFont()
getFont
in interface java.awt.MenuContainer
getFont
in class java.awt.Component
public void setFont(java.awt.Font font)
setFont
in class javax.swing.JComponent
public void setLightWeightPopupEnabled(boolean aFlag)
lightWeightPopupEnabled
property, which
provides a hint as to whether or not a lightweight
Component
should be used to contain the
JXDatePicker
, versus a heavyweight
Component
such as a Panel
or a Window
. The decision of lightweight
versus heavyweight is ultimately up to the
JXDatePicker
. Lightweight windows are more
efficient than heavyweight windows, but lightweight
and heavyweight components do not mix well in a GUI.
If your application mixes lightweight and heavyweight
components, you should disable lightweight popups.
The default value for the lightWeightPopupEnabled
property is true
, unless otherwise specified
by the look and feel. Some look and feels always use
heavyweight popups, no matter what the value of this property.
See the article Mixing Heavy and Light Components
on
The Swing Connection
This method fires a property changed event.aFlag
- if true
, lightweight popups are desiredpublic boolean isLightWeightPopupEnabled()
lightWeightPopupEnabled
property.lightWeightPopupEnabled
propertysetLightWeightPopupEnabled(boolean)
public int getBaseline(int width, int height)
getBaseline
in class javax.swing.JComponent
width
- Width of the component to determine baseline for.height
- Height of the component to determine baseline for.public void addActionListener(java.awt.event.ActionListener l)
l
- The ActionListener that is to be notifiedpublic void removeActionListener(java.awt.event.ActionListener l)
l
- The action listener to remove.public <T extends java.util.EventListener> T[] getListeners(java.lang.Class<T> listenerType)
getListeners
in class javax.swing.JComponent
protected void fireActionPerformed(java.lang.String actionCommand)