Class LabeledTextField

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

public class LabeledTextField extends JTextField
A custom JTextField component that includes a 'default text' that appears when the field is empty and unfocused. The default text is styled with a desaturated/lighter color to differentiate it from user input.

This component automatically handles focus changes and user input to toggle between the default text and the actual input. It also provides methods to set and retrieve the default text.

Example usage:

 LabeledTextField textField = new LabeledTextField("Enter your name");
 
See Also:
  • Constructor Details

    • LabeledTextField

      public LabeledTextField(String defaultText)
      Constructs a LabeledTextField with the specified default text.
      Parameters:
      defaultText - the default text to display when the field is empty and unfocused
    • LabeledTextField

      public LabeledTextField(String defaultText, float opacity)
      Constructs a LabeledTextField with the specified default text.
      Parameters:
      defaultText - the default text to display when the field is empty and unfocused
      opacity - the opacity of the default text color (0.0f to 1.0f)
  • Method Details

    • setDefaultText

      public void setDefaultText(String defaultText)
      Sets the default text to be displayed in the text field when it is empty and unfocused.
      Parameters:
      defaultText - the default text to display
    • getDefaultText

      public String getDefaultText()
      Retrieves the default text displayed in the text field when it is empty and unfocused.
      Returns:
      the default text
    • setOpacity

      public void setOpacity(float opacity)
      Sets the current opacity of the default text color.
      Parameters:
      opacity - the opacity value to set (0.0f to 1.0f)
    • getOpacity

      public float getOpacity()
      Retrieves the current opacity of the default text color.
      Returns:
      the opacity value (0.0f to 1.0f)
    • setForeground

      public void setForeground(Color foreground)
      Overrides:
      setForeground in class JComponent
    • setText

      public void setText(String text)
      Overrides:
      setText in class JTextComponent