Class HintTextField

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

public class HintTextField extends JTextField implements FocusListener
A class extending a JTextField that can display a 'ghost' text prompt in the field when it is empty. Hint display by default is whenever the field has no text, but hint display can be limited to only when the field has focus.
See Also:
Serialized Form
  • Constructor Details

    • HintTextField

      public HintTextField(int length, String hint)
      Create a new HintTextField with a length and specified hint
      Parameters:
      length - Field Length
      hint - Hint text
    • HintTextField

      public HintTextField(String hint)
      Create a new HintTextField with a specified hint
      Parameters:
      hint - Hint text
  • Method Details

    • paint

      public void paint(Graphics g)
      Overrides:
      paint in class JComponent
    • focusCheck

      protected boolean focusCheck()
    • focusGained

      public void focusGained(FocusEvent e)
      Specified by:
      focusGained in interface FocusListener
    • focusLost

      public void focusLost(FocusEvent e)
      Specified by:
      focusLost in interface FocusListener
    • isFocusOnly

      public boolean isFocusOnly()
      Does this field only display a hint when it has the focus?
      Returns:
      Display hint only with focus
    • setFocusOnly

      public void setFocusOnly(boolean focusOnly)
      Set whether or not the hint should be displayed if the field does not have focus
      Parameters:
      focusOnly - If true, hint will only be displayed if field has focus
    • getHint

      public String getHint()
      Return the current hint text
      Returns:
      Hint text
    • setHint

      public void setHint(String hint)
      Set the Hint text
      Parameters:
      hint - Hint text