Class HintTextField

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, Scrollable, SwingConstants
Direct Known Subclasses:
KeyEntryTextField, MouseEntryTextField

public class HintTextField extends JTextField
Simple text field that shows a text hint when the field is empty.

Hint text will be shown in light grey. Normal text will be plain black.

See Also:
  • Constructor Details

    • HintTextField

      public HintTextField(String hint)
      Constructor
      Parameters:
      hint - the hint text
    • HintTextField

      public HintTextField(String hint, boolean required)
      Constructor
      Parameters:
      hint - the hint text
      required - true if the field should be marked as required
    • HintTextField

      public HintTextField(String hint, boolean required, InputVerifier verifier)
      Constructor
      Parameters:
      hint - the hint text
      required - true, if the field should be marked as required
      verifier - input verifier, or null if none needed
  • Method Details

    • setHint

      public void setHint(String hint)
      Sets the hint for this text field
      Parameters:
      hint - the hint text
    • addListeners

      public void addListeners()
      Key listener allows us to check field validity on every key typed
    • setText

      public void setText(String text)
      Overridden to check the field validity when text changes
      Overrides:
      setText in class JTextComponent
      Parameters:
      text - the text to fill
    • paintComponent

      public void paintComponent(Graphics g)
      Overridden to paint the hint text over the field when it's empty
      Overrides:
      paintComponent in class JComponent
    • setRequired

      public void setRequired(boolean required)
      Sets whether the field is required or not. If so, it will be rendered differently to indicate that to the user.
      Parameters:
      required - true if required, false otherwise
    • setDefaultBackgroundColor

      public void setDefaultBackgroundColor(Color color)
      Allows users to override the background color used by this field when the contents are valid. The invalid color is currently set by this class.
      Parameters:
      color - the color
    • isFieldValid

      public boolean isFieldValid()
      Returns true if the field contains valid input.
      Returns:
      true if valid, false otherwise