Java Reference
In-Depth Information
public JPasswordField()
JPasswordField passwordField = new JPasswordField();
public JPasswordField(String text)
JPasswordField passwordField = new JPasswordField("Initial Password");
public JPasswordField(int columnWidth)
JPasswordField passwordField = new JPasswordField(14);
public JPasswordField(String text, int columnWidth)
JPasswordField passwordField = new JPasswordField("Initial Password", 14);
public JPasswordField(Document model, String text, int columnWidth)
JPasswordField passwordField = new JPasswordField(aModel, "Initial Password", 14);
With the no-argument constructor, you get an empty input field zero columns wide, a
default initial
Document
model, and an echo character of
*
. Although you can specify the initial
text in the constructor, you're usually prompting a user for a password to verify the user's iden-
tity, not to see if the user can submit a form. Therefore, a
JPasswordField
tends to be empty at
startup. As with the
JTextField
, you can also specify the initial width, assuming that the layout
manager of the container in which the
JPasswordField
is placed will honor this request.
You can also specify the
Document
data model for the password field in a constructor. When
specifying the
Document
data model, you should specify a
null
initial-text argument; otherwise,
the current contents of the document will be replaced by the initial text for the password field.
In addition, you should
not
try to use a custom
Document
with a
JPasswordField
. Because the
component doesn't display any visual feedback besides how many characters have been
entered, it can be confusing to a user if you tried to restrict input to only numeric data.
JPasswordField Properties
Table 15-6 shows the four properties of
JPasswordField
.
Table 15-6.
JPasswordField Properties
Property Name
Data Type
Access
accessibleContext
AccessibleContext
Read-only
echoChar
char
Read-write
password
char[ ]
Read-only
UIClassID
String
Read-only
Setting the
echoChar
property allows you to use a mask character other than the default
asterisk (*) character. If the
echoChar
property is set to the character
\ u0000
(0), the
public
boolean echoCharIsSet()
method returns
false
. In all other cases, the method returns
true
.