JPasswordFieldpublic class JPasswordField extends JTextField JPasswordField is a lightweight component that allows
the editing of a single line of text where the view indicates
something was typed, but does not show the original characters.
You can find further information and examples in
How to Use Text Fields,
a section in The Java Tutorial.
JPasswordField is intended
to be source-compatible with java.awt.TextField
used with echoChar set. It is provided separately
to make it easier to safely change the UI for the
JTextField without affecting password entries.
NOTE:
By default, JPasswordField disables input methods; otherwise, input
characters could be visible while they were composed using input methods.
If an application needs the input methods support, please use the
inherited method, enableInputMethods(true) .
Warning: Swing is not thread safe. For more
information see Swing's Threading
Policy.
Warning:
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is
appropriate for short term storage or RMI between applications running
the same version of Swing. As of 1.4, support for long term storage
of all JavaBeansTM
has been added to the java.beans package.
Please see {@link java.beans.XMLEncoder}. |
Fields Summary |
---|
private static final String | uiClassID | private char | echoChar | private boolean | echoCharSet |
Constructors Summary |
---|
public JPasswordField()Constructs a new JPasswordField ,
with a default document, null starting
text string, and 0 column width.
this(null,null,0);
| public JPasswordField(String text)Constructs a new JPasswordField initialized
with the specified text. The document model is set to the
default, and the number of columns to 0.
this(null, text, 0);
| public JPasswordField(int columns)Constructs a new empty JPasswordField with the specified
number of columns. A default model is created, and the initial string
is set to null .
this(null, null, columns);
| public JPasswordField(String text, int columns)Constructs a new JPasswordField initialized with
the specified text and columns. The document model is set to
the default.
this(null, text, columns);
| public JPasswordField(Document doc, String txt, int columns)Constructs a new JPasswordField that uses the
given text storage model and the given number of columns.
This is the constructor through which the other constructors feed.
The echo character is set to '*', but may be changed by the current
Look and Feel. If the document model is
null , a default one will be created.
super(doc, txt, columns);
// We could either leave this on, which wouldn't be secure,
// or obscure the composted text, which essentially makes displaying
// it useless. Therefore, we turn off input methods.
enableInputMethods(false);
|
Methods Summary |
---|
public void | copy()Invokes provideErrorFeedback on the current
look and feel, which typically initiates an error beep.
The normal behavior of transferring the
currently selected range in the associated text model
to the system clipboard, and leaving the contents from
the model, is not acceptable for a password field.
if (getClientProperty("JPasswordField.cutCopyAllowed") != Boolean.TRUE) {
UIManager.getLookAndFeel().provideErrorFeedback(this);
} else {
super.copy();
}
| boolean | customSetUIProperty(java.lang.String propertyName, java.lang.Object value)This method is a hack to get around the fact that we cannot
directly override setUIProperty because part of the inheritance heirarchy
goes outside of the javax.swing package, and therefore calling a package
private method isn't allowed. This method should return true if the property
was handled, and false otherwise.
if (propertyName == "echoChar") {
if (!echoCharSet) {
setEchoChar((Character)value);
echoCharSet = false;
}
return true;
}
return false;
| public void | cut()Invokes provideErrorFeedback on the current
look and feel, which typically initiates an error beep.
The normal behavior of transferring the
currently selected range in the associated text model
to the system clipboard, and removing the contents from
the model, is not acceptable for a password field.
if (getClientProperty("JPasswordField.cutCopyAllowed") != Boolean.TRUE) {
UIManager.getLookAndFeel().provideErrorFeedback(this);
} else {
super.cut();
}
| public boolean | echoCharIsSet()Returns true if this JPasswordField has a character
set for echoing. A character is considered to be set if the echo
character is not 0.
return echoChar != 0;
| public javax.accessibility.AccessibleContext | getAccessibleContext()Returns the AccessibleContext associated with this
JPasswordField . For password fields, the
AccessibleContext takes the form of an
AccessibleJPasswordField .
A new AccessibleJPasswordField instance is created
if necessary.
if (accessibleContext == null) {
accessibleContext = new AccessibleJPasswordField();
}
return accessibleContext;
| public char | getEchoChar()Returns the character to be used for echoing. The default is '*'.
The default may be different depending on the currently running Look
and Feel. For example, Metal/Ocean's default is a bullet character.
return echoChar;
| public char[] | getPassword()Returns the text contained in this TextComponent .
If the underlying document is null , will give a
NullPointerException . For stronger
security, it is recommended that the returned character array be
cleared after use by setting each character to zero.
Document doc = getDocument();
Segment txt = new Segment();
try {
doc.getText(0, doc.getLength(), txt); // use the non-String API
} catch (BadLocationException e) {
return null;
}
char[] retValue = new char[txt.count];
System.arraycopy(txt.array, txt.offset, retValue, 0, txt.count);
return retValue;
| public java.lang.String | getText()Returns the text contained in this TextComponent .
If the underlying document is null , will give a
NullPointerException .
For security reasons, this method is deprecated. Use the
* getPassword method instead.
return super.getText();
| public java.lang.String | getText(int offs, int len)Fetches a portion of the text represented by the
component. Returns an empty string if length is 0.
For security reasons, this method is deprecated. Use the
getPassword method instead.
return super.getText(offs, len);
| public java.lang.String | getUIClassID()Returns the name of the L&F class that renders this component.
return uiClassID;
| protected java.lang.String | paramString()Returns a string representation of this JPasswordField .
This method is intended to be used only for debugging purposes, and the
content and format of the returned string may vary between
implementations. The returned string may be empty but may not
be null .
return super.paramString() +
",echoChar=" + echoChar;
| public void | setEchoChar(char c)Sets the echo character for this JPasswordField .
Note that this is largely a suggestion, since the
view that gets installed can use whatever graphic techniques
it desires to represent the field. Setting a value of 0 indicates
that you wish to see the text as it is typed, similar to
the behavior of a standard JTextField .
echoChar = c;
echoCharSet = true;
repaint();
revalidate();
| public void | updateUI(){@inheritDoc}
if(!echoCharSet) {
echoChar = '*";
}
super.updateUI();
| private void | writeObject(java.io.ObjectOutputStream s)See readObject() and writeObject() in JComponent for more
information about serialization in Swing.
s.defaultWriteObject();
if (getUIClassID().equals(uiClassID)) {
byte count = JComponent.getWriteObjCounter(this);
JComponent.setWriteObjCounter(this, --count);
if (count == 0 && ui != null) {
ui.installUI(this);
}
}
|
|