public class Date_selector_panel extends javax.swing.JPanel implements Date_selector
"Today" is highlighted. Select a date by clicking on it. The background is transparent by default — it's grey here because the underlying window is grey.
This "raw" date selector can be "decorated" in several ways
to make it more useful. First, you can add a navigation bar to the bottom to advances the calendar by one month
(single arrow) or one year (double arrow) forwards (right-pointing arrow) or backwards (left-pointing arrow). "Today"
is highlighted. Navigation bars are specified using a Gang-of-Four "Decorator" object that wraps the raw
Date_selector_panel
Both the wrapper and the underlying panel implement the Date_selector
interface, so can be use used interchangeably. The following code creates the date selector at right.
Date_selector selector = new Date_selector_panel(); selector = new Navigable_date_selector(selector);
The same thing can be accomplished with a convenience constructor that creates the wrapped Date_selector_panel for you:
Date_selector selector = new Navigable_date_selector();
The other augmentation of interest is a title that shows the month name and year that's displayed. (there's an example at right). Use the same decoration strategy as before to add the title:
Date_selector selector = new Date_selector_panel(); selector = new Navigable_date_selector(selector); selector = new Titled_date_selector(selector);
You can leave out the navigation bar by omitting the second line of the foregoing code. Again, a convenience constructor is provided to create a titled date selector (without the navigation bar) as follows:
Date_selector selector = new Titled_date_selector();
The final variant is the lightweight popup dialog shown at right. It can be dragged around by the title bar (though dragging can be disabled) and closed by clicking on the "close" icon on the upper right. As before, use a decorator to manufacture a dialog:
Date_selector selector = new Date_selector_panel(); selector = new Navigable_date_selector(selector); // add navigation selector = new Date_selector_dialog(selector);
Note that you don't need a title because one is supplied for you in the dialog-box title bar. Also as before, a convenience constructor to create a navigable dialog box like the one at right:
Date_selector = new Date_selector_dialog(); <pre> All the earlier examples create a calendar for the current month. Several methods are provided, below, to change the date in your program. For the most part, they work like similar methods of the@see com.holub.ui.Date_selector @see com.holub.ui.Date_selector_dialog @see com.holub.ui.Navigable_date_selectorCalendar
class. <DL> <DT> <b>Known Problems</b> <DD> The week layout (S M T W Th F Sa Su) is the default layout for the underlyingCalendar
, which should change with Locale as appropriate. </DD> </DL>
com.holub.ui.Titled_date_selector
,
Serialized FormModifier and Type | Class and Description |
---|---|
class |
Date_selector_panel.DayButton |
javax.swing.JPanel.AccessibleJPanel
javax.swing.JComponent.AccessibleJComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
CHANGE_ACTION, COMMIT_ACTION, SELECT_ACTION
Constructor and Description |
---|
Date_selector_panel()
Create a Date_selector representing the current date.
|
Date_selector_panel(java.util.Date initialDate)
Create a Date_selector_panel for an arbitrary date.
|
Modifier and Type | Method and Description |
---|---|
void |
addActionListener(java.awt.event.ActionListener l)
Add a listener that's notified when the user scrolls the selector or picks a date.
|
void |
addNotify()
Create a navigation button with an image appropriate to the caption.
|
protected javax.swing.JPanel |
createWeekdayPanel() |
java.util.Date |
get_current_date()
Returns the currently displayed
Date . |
java.util.Date |
get_selected_date()
Returns the
Date selected by the user or null if the window was closed without selecting a date. |
int |
get(int field)
Works just like
Calendar.get(int) |
javax.swing.JComponent |
getComponent() |
java.lang.String[] |
getDayNames(java.util.Locale locale) |
java.lang.String[] |
getMonthNames(java.util.Locale locale)
Returns an array of locale-specific short month names
|
javax.swing.border.Border |
getSelectedBorder() |
java.awt.Color |
getStyleBackground(int style) |
java.awt.Color |
getStyleForeground(int style) |
void |
localeChanged(java.util.Locale newLocale)
Components that use Date_selector_panel need to call this when locale changes.
|
protected void |
rebuildCalendar() |
void |
removeActionListener(java.awt.event.ActionListener l)
Remove a listener.
|
void |
roll(int field,
boolean up)
Works just like
Calendar.roll(int, boolean) . |
void |
set_selected_date(java.util.Date d) |
void |
setEnabled(boolean b) |
void |
setFont(java.awt.Font font) |
void |
setForeground(java.awt.Color fg) |
void |
setSelectedBorder(javax.swing.border.Border b) |
void |
setStyleBackground(int style,
java.awt.Color color) |
void |
setStyleForeground(int style,
java.awt.Color foreground) |
getAccessibleContext, getUI, getUIClassID, paramString, 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, 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, getLocale, 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, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
public Date_selector_panel()
public Date_selector_panel(java.util.Date initialDate)
initialDate
- Calendar will display this date. The specified date is highlighted as "today".public java.awt.Color getStyleBackground(int style)
public void setStyleBackground(int style, java.awt.Color color)
public java.awt.Color getStyleForeground(int style)
public void setStyleForeground(int style, java.awt.Color foreground)
public void setSelectedBorder(javax.swing.border.Border b)
public javax.swing.border.Border getSelectedBorder()
public void localeChanged(java.util.Locale newLocale)
Why not just implement LocaleListener, you may ask? Because this class is paying absolutely no attention to locale changes when it's done that way.
protected void rebuildCalendar()
public void setFont(java.awt.Font font)
setFont
in class javax.swing.JComponent
public void setEnabled(boolean b)
setEnabled
in class javax.swing.JComponent
public void setForeground(java.awt.Color fg)
setForeground
in class javax.swing.JComponent
protected javax.swing.JPanel createWeekdayPanel()
public void addActionListener(java.awt.event.ActionListener l)
addActionListener
in interface Date_selector
Date_selector
public void removeActionListener(java.awt.event.ActionListener l)
removeActionListener
in interface Date_selector
Date_selector
public void addNotify()
caption
argument is used as
the button's "action command." This method is public only because it has to be. (It overrides a public method.)
Pretend it's not here.addNotify
in class javax.swing.JComponent
public void set_selected_date(java.util.Date d)
set_selected_date
in interface Date_selector
public java.util.Date get_selected_date()
Date
selected by the user or null if the window was closed without selecting a date. The
returned Date has hours, minutes, and seconds values of 0.get_selected_date
in interface Date_selector
public java.util.Date get_current_date()
Date
.get_current_date
in interface Date_selector
public void roll(int field, boolean up)
Calendar.roll(int, boolean)
.roll
in interface Date_selector
public int get(int field)
Calendar.get(int)
get
in interface Date_selector
public java.lang.String[] getMonthNames(java.util.Locale locale)
public java.lang.String[] getDayNames(java.util.Locale locale)
public javax.swing.JComponent getComponent()
getComponent
in interface Date_selector