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.

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

Property

Data type

get

is

set

Default value

accessibleContext o

AccessibleContext

·

  

AccessibleJPasswordField

echoChar

char

·

 

·

'*'

password

char[]

·

   

text o

string

·

 

·

 

UIClassID o

String

·

  

“PasswordFieldUI”

ooverridden

See also properties from the JTextCompoment ...

Get Java Swing, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.