Class TimeSelector

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

public class TimeSelector extends JComponent implements PropertyChangeListener
A control which allows the user to enter/change a time. The time can be entered into the textfield manually, or it can be selected by popping up a small analog clock. Note that this uses a JPopupMenu to display the popup analog clock, and there are known clipping bugs with JPopupMenu instances in modal dialogs. Note: Any changes done to this class might also need to be copied to DateSelector and DateTimeSelector
Author:
Nathan Brizzee
See Also:
  • Field Details

  • Constructor Details

    • TimeSelector

      public TimeSelector()
      Creates a new TimeSelector instance comprised of a JTextField and an selectable icon to the right of the JTextField. Selecting the icon will display an analog clock in a popup menu.
    • TimeSelector

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

    • hidePopup

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

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

      public Color getBackground()
      Gets the TextField's background color
      Overrides:
      getBackground in class Component
      Returns:
      the current background color
    • setBackground

      public void setBackground(Color bg)
      Sets the TextField's background color
      Overrides:
      setBackground in class JComponent
      Parameters:
      bg - Color to set background to
    • getForeground

      public Color getForeground()
      Gets the TextField's Foreground color
      Overrides:
      getForeground in class Component
      Returns:
      the current foreground color
    • setForeground

      public void setForeground(Color fc)
      Sets the TextField's Foreground color
      Overrides:
      setForeground in class JComponent
      Parameters:
      fc - color to set the foreground to
    • addChangeListener

      public void addChangeListener(ChangeListener l)
      Adds a change listener to the textfield
      Parameters:
      l - changeListener to add
    • removeChangeListener

      public void removeChangeListener(ChangeListener l)
      Removes a change listener from the textbox
      Parameters:
      l - changeListener to remove
    • paintComponent

      public void paintComponent(Graphics g)
      Repaints the component
      Overrides:
      paintComponent in class JComponent
      Parameters:
      g - - graphics to paint on
    • 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 time pattern
    • getPattern

      public String getPattern()
      Retrieves the date pattern used to render the time 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
    • getTime

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

      public void setTime(Date d)
      Establishes the current time represented by this component
      Parameters:
      d - the new date
    • setHour

      public void setHour(int value)
      Sets the hour to be displayed by the clock
      Parameters:
      value - the number representing the current hour
    • getHour

      public int getHour()
      Gets the hour displayed by the clock
      Returns:
      the current hour displayed by the clock
    • setMinutes

      public void setMinutes(int value)
      Sets the minutes to be displayed by the clock
      Parameters:
      value - the number representing the current minutes
    • getMinutes

      public int getMinutes()
      Gets the minutes displayed by the clock
      Returns:
      the current minutes displayed by the clock
    • setSeconds

      public void setSeconds(int value)
      Sets the seconds to be displayed by the clock
      Parameters:
      value - the number representing the current seconds
    • getSeconds

      public int getSeconds()
      Gets the seconds displayed by the clock
      Returns:
      the current seconds displayed by the clock
    • setBackgroundColor

      public void setBackgroundColor(Color c)
      Change the background color of the clock. Default color is light gray
      Parameters:
      c - the new background color
    • getBackgroundColor

      public Color getBackgroundColor()
      Retrieves the clock background color
      Returns:
      the background color.
    • setHourHandColor

      public void setHourHandColor(Color c)
      Change the hour hand color of the clock. Default color is blue.
      Parameters:
      c - the new hour hand color
    • getHourHandColor

      public Color getHourHandColor()
      Retrieves the hour hand color
      Returns:
      the hour hand color.
    • setMinuteHandColor

      public void setMinuteHandColor(Color c)
      Change the minute hand color of the clock. Default color is blue.
      Parameters:
      c - the new minute hand color
    • getMinuteHandColor

      public Color getMinuteHandColor()
      Retrieves the minute hand color
      Returns:
      the minute hand color.
    • setSecondHandColor

      public void setSecondHandColor(Color c)
      Change the second hand color of the clock. Default color is black.
      Parameters:
      c - the new second hand color
    • getSecondHandColor

      public Color getSecondHandColor()
      Retrieves the second hand color
      Returns:
      the second hand color.
    • setHoursDotColor

      public void setHoursDotColor(Color c)
      Change the hours dot color of the clock. Default color is red.
      Parameters:
      c - the new hours dot color
    • getHoursDotColor

      public Color getHoursDotColor()
      Retrieves the hours dot color
      Returns:
      the hours dot color.
    • setMinutesDotColor

      public void setMinutesDotColor(Color c)
      Change the Minutes dot color of the clock. Default color is white.
      Parameters:
      c - the new Minutes dot color
    • getMinutesDotColor

      public Color getMinutesDotColor()
      Retrieves the Minutes dot color
      Returns:
      the Minutes dot color.
    • setHourOffset

      public void setHourOffset(int value)
      Changes the number of hours to offset the clock by. Default is 0
      Parameters:
      value - the number of hours to offset the clock by. Valid values are +24 to -24.
    • getHourOffset

      public int getHourOffset()
      Retrieves the Hour offset value
      Returns:
      the Hour offset value
    • setMinutesOffset

      public void setMinutesOffset(int value)
      Changes the number of minutes to offset the clock by. Default is 0
      Parameters:
      value - the number of minutes to offset the clock by. Valid values are +60 to -60.
    • getMinutesOffset

      public int getMinutesOffset()
      Retrieves the Minute offset value
      Returns:
      the Minute offset value
    • setSecondsOffset

      public void setSecondsOffset(int value)
      Changes the number of Seconds to offset the clock by. Default is 0
      Parameters:
      value - the number of Seconds to offset the clock by. Valid values are +60 to -60.
    • getSecondsOffset

      public int getSecondsOffset()
      Retrieves the Seconds offset value
      Returns:
      the Seconds offset value
    • 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)
      Watch for user selection date changesmTimePopup from the analogClock component
      Specified by:
      propertyChange in interface PropertyChangeListener
      Parameters:
      e - property change event