Package ca.corbett.forms.fields
Class PasswordField
java.lang.Object
ca.corbett.forms.fields.FormField
ca.corbett.forms.fields.PasswordField
- All Implemented Interfaces:
EventListener,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 Summary
FieldsModifier and TypeFieldDescriptionprotected final JButtonstatic final charprotected charprotected final JButtonprotected final JPasswordFieldFields inherited from class ca.corbett.forms.fields.FormField
DEFAULT_FONT, defaultFont, extraAttributes, fieldComponent, fieldLabel, fieldValidators, helpLabel, ICON_SIZE, identifier, isEnabled, isVisible, margins, validationLabel, valueChangedListeners -
Constructor Summary
ConstructorsConstructorDescriptionPasswordField(String label, int cols) Creates a new PasswordField with the specified column width. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidvoidprotected voidInvoked internally to copy the current password text to the clipboard.charReturns the character that is used to mask user input in this field.Returns the current text entered in the field.voidProgrammatically hides the password, if it were previously revealed via the revealPassword() method, or via the user clicking on the show/hide password toggle button.voidbooleanReports whether a NonBlankFieldValidator has been added to this TextField.booleanReports whether the "copy to clipboard" button is visible.booleanReports whether the "show password" toggle button is visible.booleanIndicates whether the password is currently revealed (either by an explicit call to revealPassword(), or by the user clicking the show/hide password toggle button).protected voidvoidvoidProgrammatically reveals the password, without changing the currently configured echo character.setAllowBlank(boolean allow) By default, TextField will allow blank values (empty text) to pass validation.setAllowClipboard(boolean allow) Controls the visibility of the "copy to clipboard" button.setAllowShowPassword(boolean allow) Controls the visibility of the "show password" toggle button.setEchoChar(char c) By default, characters typed into the password field will be shown as an asterisk.setEnabled(boolean isEnabled) Overridden so we can disable all our child components when setEnabled is invoked.setPassword(String password) Sets the text for the password field.protected voidInvoked internally to toggle the current revealed/hidden state of the password text.Methods inherited from class ca.corbett.forms.fields.FormField
addAllExtraAttributes, addFieldValidator, addValueChangedListener, clearExtraAttribute, clearExtraAttributes, clearValidationResults, equals, fireValueChangedEvent, getDefaultFont, getExtraAttribute, getFieldComponent, getFieldLabel, getHelpLabel, getHelpText, getIdentifier, getMargins, getValidationLabel, hasFieldLabel, hashCode, hasHelpLabel, hasValidationLabel, isEnabled, isMultiLine, isValid, isVisible, preRender, removeAllFieldValidators, removeFieldValidator, removeValueChangedListener, setAllExtraAttributes, setDefaultFont, setExtraAttribute, setFieldLabelFont, setHelpText, setIdentifier, setMargins, setVisible, shouldExpand, validate
-
Field Details
-
DEFAULT_ECHO_CHAR
public static final char DEFAULT_ECHO_CHAR- See Also:
-
textField
-
showPasswordButton
-
copyButton
-
echoChar
protected char echoChar
-
-
Constructor Details
-
PasswordField
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
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:
setEnabledin classFormField- Parameters:
isEnabled- whether to enable or disable the components.
-
isAllowBlank
public boolean isAllowBlank()Reports whether a NonBlankFieldValidator has been added to this TextField. -
setAllowBlank
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
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
Controls the visibility of the "copy to clipboard" button. -
isAllowClipboard
public boolean isAllowClipboard()Reports whether the "copy to clipboard" button is visible. -
setEchoChar
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
Sets the text for the password field. -
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
- Specified by:
insertUpdatein interfaceDocumentListener
-
removeUpdate
- Specified by:
removeUpdatein interfaceDocumentListener
-
changedUpdate
- Specified by:
changedUpdatein interfaceDocumentListener
-