Class PasswordField

java.lang.Object
ca.corbett.forms.fields.FormField
ca.corbett.forms.fields.PasswordField
All Implemented Interfaces:
EventListener, DocumentListener

public class PasswordField extends FormField implements DocumentListener
A FormField for password input. This field is very similar to ShortTextField, with the exception that characters typed into the field are masked (with asterisk by default, though this is customizable). There is an optional "show password" button that can be used to toggle visibility of the typed characters.
Since:
swing-extras 2.5
Author:
scorbo2
  • Field Details

    • DEFAULT_ECHO_CHAR

      public static final char DEFAULT_ECHO_CHAR
      See Also:
    • textField

      protected final JPasswordField textField
    • showPasswordButton

      protected final JButton showPasswordButton
    • copyButton

      protected final JButton copyButton
    • echoChar

      protected char echoChar
  • Constructor Details

    • PasswordField

      public PasswordField(String label, int cols)
      Creates a new PasswordField with the specified column width. By default, the echo char will be an asterisk, but you can control this with setEchoChar. Buttons are presented to allow toggling visibility of the password, and also to copy the password to the clipboard. You can hide these buttons via the setAllowShowPassword() and setAllowClipboard() methods. Setting them to false will hide the button in question.
  • Method Details

    • setEnabled

      public FormField setEnabled(boolean isEnabled)
      Overridden so we can disable all our child components when setEnabled is invoked. We could leave the show password button and the copy to clipboard button enabled when disabling the field, as they can't be used to change the field contents, but it might be a bit weird to have enabled components on a disabled field, so here we will enable or disable all at once.
      Overrides:
      setEnabled in class FormField
      Parameters:
      isEnabled - whether to enable or disable the components.
    • isAllowBlank

      public boolean isAllowBlank()
      Reports whether a NonBlankFieldValidator has been added to this TextField.
    • setAllowBlank

      public PasswordField setAllowBlank(boolean allow)
      By default, TextField will allow blank values (empty text) to pass validation. You can disallow that with this method - passing false will add a NonBlankFieldValidator to this TextField. Passing true will remove the NonBlankFieldValidator if one is present.
    • setAllowShowPassword

      public PasswordField setAllowShowPassword(boolean allow)
      Controls the visibility of the "show password" toggle button. If allowed, the user can hit that toggle button to reveal the hidden text in the field, and to hide it again.
    • isAllowShowPassword

      public boolean isAllowShowPassword()
      Reports whether the "show password" toggle button is visible.
    • setAllowClipboard

      public PasswordField setAllowClipboard(boolean allow)
      Controls the visibility of the "copy to clipboard" button.
    • isAllowClipboard

      public boolean isAllowClipboard()
      Reports whether the "copy to clipboard" button is visible.
    • setEchoChar

      public PasswordField setEchoChar(char c)
      By default, characters typed into the password field will be shown as an asterisk. But, you can set some other character to be used for that purpose.

      Warning: setEchoChar((char)0) will disable hiding of the password. It's better to use the revealPassword() and hidePassword() methods for this purpose instead, as it allows you to toggle password visibility easily.

    • getEchoChar

      public char getEchoChar()
      Returns the character that is used to mask user input in this field. By default, this is an asterisk, but it can be controlled via setEchoChar.

      Note: this method will return the configured echo char, even if the user has hit the "reveal password" button. If you wish to check if the password is currently revealed, use the isPasswordRevealed() method.

    • revealPassword

      public void revealPassword()
      Programmatically reveals the password, without changing the currently configured echo character. You can invoke hidePassword() to re-hide the password after invoking this method. Does nothing if the password is already revealed.
    • hidePassword

      public void hidePassword()
      Programmatically hides the password, if it were previously revealed via the revealPassword() method, or via the user clicking on the show/hide password toggle button. Does nothing if the password was already hidden.

      Note: If you have previously invoked setEchoChar((char)0) to manually reveal the password, this method will reset the configured echo char back to the default (asterisk).

    • isPasswordRevealed

      public boolean isPasswordRevealed()
      Indicates whether the password is currently revealed (either by an explicit call to revealPassword(), or by the user clicking the show/hide password toggle button).
    • setPassword

      public PasswordField setPassword(String password)
      Sets the text for the password field.
    • getPassword

      public String getPassword()
      Returns the current text entered in the field.
    • toggleShowPassword

      protected void toggleShowPassword()
      Invoked internally to toggle the current revealed/hidden state of the password text.
    • copyPassword

      protected void copyPassword()
      Invoked internally to copy the current password text to the clipboard.
    • addNonBlankValidatorIfNotPresent

      protected void addNonBlankValidatorIfNotPresent()
    • removeNonBlankValidatorIfPresent

      protected void removeNonBlankValidatorIfPresent()
    • insertUpdate

      public void insertUpdate(DocumentEvent e)
      Specified by:
      insertUpdate in interface DocumentListener
    • removeUpdate

      public void removeUpdate(DocumentEvent e)
      Specified by:
      removeUpdate in interface DocumentListener
    • changedUpdate

      public void changedUpdate(DocumentEvent e)
      Specified by:
      changedUpdate in interface DocumentListener