19.5 The JPasswordField Class
A
JPasswordField
is a text field in which an echo character (* by
default) is displayed in place of the characters typed by the user.
This feature is generally used when entering passwords to avoid
showing the password on the screen. Except for the quirky display,
JPasswordField behaves like an ordinary
JTextField, though some steps have been taken to
enhance the password field's security.
One reason that JPasswordField is a separate class
from JTextField in Swing (which is not the case in
the analogous AWT classes) is so the L&F can treat them
differently by specifying different UI delegates. The UI delegate is
responsible for hiding the input characters in a
JPasswordField.
19.5.1 Properties
Table 19-5 shows the properties defined by
JPasswordField. JPasswordField
has its own unique UIClassID value. The value for
accessibleContext property is
AccessibleJPasswordField, an inner class that
extends the JTextField.AccessibleJTextField class.
The echoChar property specifies the
character to be displayed in the field each time a key is pressed.
This character is used to hide the actual input characters, though
the L&F may choose to ignore it and hide the input characters
some other way.
Table 19-5. JPasswordField properties
accessibleContexto
|
AccessibleContext
|
·
|
|
|
AccessibleJPasswordField
|
echoChar
|
char
|
·
|
|
·
|
'*'
|
password
|
char[]
|
·
|
|
|
|
texto
|
string
|
·
|
|
·
|
|
UIClassIDo
|
String
|
·
|
|
|
"PasswordFieldUI"
|
ooverridden
See also properties from the JTextCompoment class (Table 19-1).
|
In the interest of security, the getText( )
accessor methods have been deprecated in the
JPasswordField field. To get the entered password,
it is recommended that the getPassword( ) method
be used instead. getPassword( ) returns a mutable
char[] array, not an immutable
String, so that you can clobber the password with
'\0' characters when you are done with it.
19.5.2 Constructors
- public JPasswordField( )
-
Create a new password text field with zero columns.
- public JPasswordField(String text)
-
Create a new field containing the text (displayed
using the echo character).
- public JPasswordField(int columns)
-
Create a new password field with the requested number of
columns.
- public JPasswordField(String text, int columns)
-
Create a new password field with the specified number of
columns, containing the supplied
text (displayed using the echo character).
- public JPasswordField(Document doc, String text, int columns)
-
This constructor (called by all the others) creates a new password
field that uses the specified document model and number of
columns. If text is
null, the
Document's current text is
"displayed" with the echo
character. Otherwise, text replaces the
Document's content and is
"displayed." This constructor sets
the echo character as an asterisk (*).
19.5.3 Data Protection Methods
- public void
cut( )
- public void copy( )
-
These methods are overridden to disable cut and copy behavior in
password fields. They simply call the L&F's
provideErrorFeedback( ) method, which typically
emits a beep. If these methods were not overridden, it would be
possible for hidden passwords to be copied from password fields and
pasted into nonhidden fields.
- public string getText(int offs, int len) throws BadLocationException
-
Defined in this class only for the purpose of being marked as
deprecated. The getPassword( ) method should be
used instead.
19.5.4 Miscellaneous Methods
- public boolean echoCharIsSet( )
-
Indicate whether an echo character has been set. Note that a default
echo character (*) is defined, so this method
always returns true unless the echo character is
explicitly set to '\0'.
|