Class DateSelector

All Implemented Interfaces:
FocusListener, ImageObserver, MenuContainer, PropertyChangeListener, Serializable, EventListener

public class DateSelector extends JComponent implements PropertyChangeListener, FocusListener
A control which allows the user to enter/change a date. The date can be entered into the textfield manually, or it can be selected by popping up a small calendar window which displays dates one month at a time. The calendar popup will always display the current date in a specified color (default red) and the user selected date (if different from the current date) in a different color (default yellow).

Note that this uses a JPopupMenu to display the popup calendar, and there are known clipping bugs with JPopupMenu instances in modal dialogs.
Author:
Kapono Carter
See Also:
  • Field Details

    • dateTF

      protected JTextField dateTF
    • popupInvoker

      protected JButton popupInvoker
    • calPopup

      protected JPopupMenu calPopup
    • minical

      protected MiniCal minical
    • normalizeYear

      protected boolean normalizeYear
  • Constructor Details

    • DateSelector

      public DateSelector()
      Creates a new DateSelector instance comprised of a JTextField and an selectable icon to the right of the JTextField. Selecting the icon will display a calendar "month" in a popup menu.
    • DateSelector

      public DateSelector(boolean showDate)
      Creates a new DateSelector instance comprised of a JTextField and an selectable icon to the right of the JTextField. Selecting the icon will display a calendar "month" in a popup menu.
      Parameters:
      showDate - determines if today's date is displayed in the textfield initially. If showDate is false, the textfield is initially blank and the tooltip (containing the date pattern) is turned on.
  • Method Details

    • hidePopup

      public void hidePopup()
      Provides a way to force the pop-up to hide
    • computePopupBounds

      public static Point computePopupBounds(int px, int py, int pw, int ph, Component comp)
      Computes the location on the screen for a Popup taking into account the edges of the screen.
      Parameters:
      px - : x coordinate - relative to the Component (usually 0)
      py - : y coordinate - The height of the control (usually : comp.getBounds().height)
      pw - : width - width of the popup (usually Popup.getSize().width)
      ph - : height - height of the popup (usually Popup.getSize().height)
      comp - : The component the pop-up belongs to. Like a JTextBox.
      Returns:
      Point : The location to set the popup at. With this point, call : Popup.show(comp, Point.x, Point.y);
    • requestFocus

      public void requestFocus()
      Override requestFocus to pass it on to the actual text field
      Overrides:
      requestFocus in class JComponent
    • setLayout

      public void setLayout(LayoutManager lm)
      Overrides:
      setLayout in class Container
    • getBackground

      public Color getBackground()
      Overrides:
      getBackground in class Component
    • setBackground

      public void setBackground(Color bg)
      Overrides:
      setBackground in class JComponent
    • addChangeListener

      public void addChangeListener(ChangeListener l)
    • removeChangeListener

      public void removeChangeListener(ChangeListener l)
    • paintComponent

      public void paintComponent(Graphics g)
      Overrides:
      paintComponent in class JComponent
    • setShowPatternTip

      public void setShowPatternTip(boolean b)
      Turns on/off the tooltip displaying the current date pattern. This is potentially useful if the user doesn't know what format to manually enter a date string.
      Parameters:
      b - the flag dictating if the tooltip should be on or off
    • setEditable

      public void setEditable(boolean editable)
      Set editability of component
      Parameters:
      editable - New editable flag
    • isEditable

      public boolean isEditable()
      Returns true if component is editable
      Returns:
      true if component is editable
    • getShowPatternTip

      public boolean getShowPatternTip()
      Indicates if the tooltip is displayed or not.
      Returns:
      the state of the tooltip containing the current date pattern
    • getPattern

      public String getPattern()
      Retrieves the date pattern used to render the date into the textfield
      Returns:
      the date format pattern
    • setPattern

      public void setPattern(String p)
    • getText

      public String getText()
      Retrieves the contents of the date textfield.
      Returns:
      the contents of the date textfield
    • getDate

      public Date getDate() throws IllegalArgumentException
      Retrieves the current date represented by this component. If the date manually entered by the user is not a valid date, an IllegalArgumentException is thrown.
      Returns:
      the current date
      Throws:
      IllegalArgumentException
    • setDate

      public void setDate(Date d)
      Establishes the current date represented by this component
      Parameters:
      d - the new date
    • setTodayColor

      public void setTodayColor(Color c)
      Change the color representing "today". Today is red by default
      Parameters:
      c - the new today color
    • getTodayColor

      public Color getTodayColor()
      Retrieves the color representing "today"
      Returns:
      the color representing "today"
    • setSelectedDateColor

      public void setSelectedDateColor(Color c)
      Change the color representing the user selected date. This is yellow by default.
      Parameters:
      c - the new current date color
    • getSelectedDateColor

      public Color getSelectedDateColor()
      Retrieves the color representing the user selected date
      Returns:
      the color representing the user selected date
    • setEnabled

      public void setEnabled(boolean b)
      Enables/disables the subcomponents that comprise the main component
      Overrides:
      setEnabled in class JComponent
      Parameters:
      b - If true, the component is enabled; otherwise it is disabled.
    • propertyChange

      public void propertyChange(PropertyChangeEvent e)
      Specified by:
      propertyChange in interface PropertyChangeListener
    • focusGained

      public void focusGained(FocusEvent e)
      Invoked when a component gains the keyboard focus.
      Specified by:
      focusGained in interface FocusListener
      Parameters:
      event - The focus event
    • focusLost

      public void focusLost(FocusEvent e)
      Invoked when a component loses the keyboard focus.
      Specified by:
      focusLost in interface FocusListener
      Parameters:
      event - The focus event