Package View.includes

Class TextPrompt

All Implemented Interfaces:
FocusListener, ImageObserver, MenuContainer, Serializable, EventListener, Accessible, DocumentListener, SwingConstants

public class TextPrompt extends JLabel implements FocusListener, DocumentListener
The `TextPrompt` class displays a prompt over a text component when its document is empty. The prompt's visibility and appearance can be customized. It will automatically hide when text is entered.
See Also:
  • Field Details

    • component

      private JTextComponent component
      The `component` field represents the associated `JTextComponent` where the prompt is displayed.
    • document

      private Document document
      The `document` field represents the `Document` of the associated `JTextComponent`.
    • show

      private TextPrompt.Show show
      The `show` field is of the `Show` enum type and controls when the prompt is displayed. Possible values are `ALWAYS`, `FOCUS_GAINED`, and `FOCUS_LOST`.
    • showPromptOnce

      private boolean showPromptOnce
      The `showPromptOnce` field determines whether the prompt should be shown only once. If set to `true`, the prompt is shown only once when the component gains/loses focus.
    • focusLost

      private int focusLost
      The `focusLost` field keeps track of the number of times focus has been lost on the component. It is used to manage the prompt's visibility based on focus events.
  • Constructor Details

    • TextPrompt

      public TextPrompt(String text, JTextComponent component)
      Constructs a `TextPrompt` with the specified text and text component, using the default show behavior "ALWAYS."
      Parameters:
      text - The text to be displayed as the prompt.
      component - The text component for which the prompt is displayed.
      See Also:
    • TextPrompt

      public TextPrompt(String text, JTextComponent component, TextPrompt.Show show)
      Constructs a `TextPrompt` with the specified text, text component, and show behavior.
      Parameters:
      text - The text to be displayed as the prompt.
      component - The text component for which the prompt is displayed.
      show - When to show the prompt (ALWAYS, FOCUS_GAINED, or FOCUS_LOST, as defined in the Show enum).
      See Also:
  • Method Details

    • changeAlpha

      public void changeAlpha(float alpha)
      Convenience method to change the alpha value of the current foreground Color to the specifice value.
      Parameters:
      alpha - value in the range of 0 - 1.0.
    • changeAlpha

      public void changeAlpha(int alpha)
      Convenience method to change the alpha value of the current foreground Color to the specifice value.
      Parameters:
      alpha - value in the range of 0 - 255.
    • changeStyle

      public void changeStyle(int style)
      Convenience method to change the style of the current Font. The style values are found in the Font class. Common values might be: Font.BOLD, Font.ITALIC and Font.BOLD + Font.ITALIC.
      Parameters:
      style - value representing the the new style of the Font.
    • getShow

      public TextPrompt.Show getShow()
      Get the Show property
      Returns:
      the Show property.
    • setShow

      public void setShow(TextPrompt.Show show)
      Set the prompt Show property to control when the promt is shown. Valid values are: Show.AWLAYS (default) - always show the prompt Show.Focus_GAINED - show the prompt when the component gains focus (and hide the prompt when focus is lost) Show.Focus_LOST - show the prompt when the component loses focus (and hide the prompt when focus is gained)
      Parameters:
      show - a valid Show enum
    • getShowPromptOnce

      public boolean getShowPromptOnce()
      Get the showPromptOnce property
      Returns:
      the showPromptOnce property.
    • setShowPromptOnce

      public void setShowPromptOnce(boolean showPromptOnce)
      Show the prompt once. Once the component has gained/lost focus once, the prompt will not be shown again.
      Parameters:
      showPromptOnce - when true the prompt will only be shown once, otherwise it will be shown repeatedly.
    • checkForPrompt

      private void checkForPrompt()
      Check whether the prompt should be visible or not. The visibility will change on updates to the Document and on focus changes.
    • focusGained

      public void focusGained(FocusEvent e)
      Invoked when the associated text component gains focus. This method checks whether the prompt should be displayed.
      Specified by:
      focusGained in interface FocusListener
      Parameters:
      e - The `FocusEvent` associated with the focus gain.
    • focusLost

      public void focusLost(FocusEvent e)
      Invoked when the associated text component loses focus. This method increments the focusLost count and checks whether the prompt should be displayed.
      Specified by:
      focusLost in interface FocusListener
      Parameters:
      e - The `FocusEvent` associated with the focus loss.
    • insertUpdate

      public void insertUpdate(DocumentEvent e)
      Invoked when text is inserted into the associated text component's document. This method checks whether the prompt should be displayed.
      Specified by:
      insertUpdate in interface DocumentListener
      Parameters:
      e - The `DocumentEvent` associated with text insertion.
    • removeUpdate

      public void removeUpdate(DocumentEvent e)
      Invoked when text is removed from the associated text component's document. This method checks whether the prompt should be displayed.
      Specified by:
      removeUpdate in interface DocumentListener
      Parameters:
      e - The `DocumentEvent` associated with text removal.
    • changedUpdate

      public void changedUpdate(DocumentEvent e)
      Invoked when there is a change in the associated text component's document. This method does not affect prompt visibility and is left empty.
      Specified by:
      changedUpdate in interface DocumentListener
      Parameters:
      e - The `DocumentEvent` associated with the document change.