APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing

public Class JPasswordField

extends JTextField
Class Inheritance
Known Direct Subclasses
sun.awt.X11.XTextFieldPeer.XAWTTextField
Annotations
@JavaBean
description:Allows the editing of a line of text but doesn\'t show the characters.
@SwingContainer:false
@SuppressWarnings:serial
Imports
java.beans.BeanProperty, .JavaBean, java.io.IOException, .ObjectOutputStream, .Serial, java.util.Arrays, javax.accessibility.AccessibleContext, .AccessibleRole, .AccessibleText, .AccessibleTextSequence, javax.swing.text.AbstractDocument, .BadLocationException, .Document, .DocumentFilter, .Segment

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

The JPasswordField will not show the original characters that were typed, instead displaying alternative text or graphics. However this doesn't prevent the password from appearing in the system memory. For handling confidential information such as the password text, refer to the relevant section at Secure Coding Guidelines.

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 JavaBeans has been added to the java.beans package. Please see java.beans.XMLEncoder.

Author
Timothy Prinzing
Since
1.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
JPasswordField.AccessibleJPasswordField

This class implements accessibility support for the JPasswordField class.

Field Summary

Modifier and TypeField and Description
private char
private boolean
private static final String
Inherited from javax.swing.JTextField:
notifyAction

Constructor Summary

AccessConstructor and Description
public
JPasswordField()

Constructs a new JPasswordField, with a default document, null starting text string, and 0 column width.

public
JPasswordField(String
the text to be displayed, null if none
text
)

Constructs a new JPasswordField initialized with the specified text.

public
JPasswordField(int
the number of columns >= 0
columns
)

Constructs a new empty JPasswordField with the specified number of columns.

public
JPasswordField(String
the text to be displayed, null if none
text
,
int
the number of columns >= 0
columns
)

Constructs a new JPasswordField initialized with the specified text and columns.

public
JPasswordField(Document
the text storage to use
doc
,
String
the text to be displayed, null if none
txt
,
int
the number of columns to use to calculate the preferred width >= 0; if columns is set to zero, the preferred width will be whatever naturally results from the component implementation
columns
)

Constructs a new JPasswordField that uses the given text storage model and the given number of columns.

Method Summary

Modifier and TypeMethod and Description
public void
copy()

Overrides javax.swing.text.JTextComponent.copy.

Invokes provideErrorFeedback on the current look and feel, which typically initiates an error beep.

pack-priv boolean
customSetUIProperty(String propertyName, Object value)

This method is a hack to get around the fact that we cannot directly override setUIProperty because part of the inheritance hierarchy goes outside of the javax.swing package, and therefore calling a package private method isn't allowed.

public void
cut()

Overrides javax.swing.text.JTextComponent.cut.

Invokes provideErrorFeedback on the current look and feel, which typically initiates an error beep.

public boolean

Returns:

true if a character is set for echoing
echoCharIsSet
()

Returns true if this JPasswordField has a character set for echoing.

public AccessibleContext

Returns:

an AccessibleJPasswordField that serves as the AccessibleContext of this JPasswordField
getAccessibleContext
()

Overrides javax.swing.JTextField.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Returns the AccessibleContext associated with this JPasswordField.

public char

Returns:

the echo character, 0 if unset
getEchoChar
()

Returns the character to be used for echoing.

public char[]

Returns:

the text
getPassword
()

Returns the text contained in this TextComponent.

public String

Returns:

the text
getText
()

Overrides javax.swing.text.JTextComponent.getText.

Deprecated As of Java 2 platform v1.2, replaced by getPassword.

Returns the text contained in this TextComponent.

public String

Returns:

the text
getText
(int
the offset >= 0
offs
,
int
the length >= 0
len
)

Overrides javax.swing.text.JTextComponent.getText.

Deprecated As of Java 2 platform v1.2, replaced by getPassword.

Fetches a portion of the text represented by the component.

public String

Returns:

the string "PasswordFieldUI"
getUIClassID
()

Overrides javax.swing.JTextField.getUIClassID.

Returns the name of the L&F class that renders this component.

protected String

Returns:

a string representation of this JPasswordField
paramString
()

Overrides javax.swing.JTextField.paramString.

Returns a string representation of this JPasswordField.

public void
setEchoChar(char
the echo character to display
c
)

Sets the echo character for this JPasswordField.

public void
setText(String t)

Overrides javax.swing.text.JTextComponent.setText.

Sets the text of this TextComponent to the specified text.

public void
updateUI()

Overrides javax.swing.text.JTextComponent.updateUI.

Reloads the pluggable UI.

private void
writeObject(ObjectOutputStream s)

Hides javax.swing.JTextField.writeObject.

See readObject() and writeObject() in JComponent for more information about serialization in Swing.

Inherited from javax.swing.JTextField:
actionPropertyChangedaddActionListenerconfigurePropertiesFromActioncreateActionPropertyChangeListenercreateDefaultModelfireActionPerformedgetActiongetActionListenersgetActionsgetColumnsgetColumnWidthgetHorizontalAlignmentgetHorizontalVisibilitygetPreferredSizegetScrollOffsethasActionListenerisValidateRootpostActionEventremoveActionListenerscrollRectToVisiblesetActionsetActionCommandsetColumnssetDocumentsetFontsetHorizontalAlignmentsetScrollOffset

Field Detail

echoCharback to summary
private char echoChar
echoCharSetback to summary
private boolean echoCharSet
uiClassIDback to summary
private static final String uiClassID

Hides javax.swing.JTextField.uiClassID.

See Also
getUIClassID, readObject

Constructor Detail

JPasswordFieldback to summary
public JPasswordField()

Constructs a new JPasswordField, with a default document, null starting text string, and 0 column width.

JPasswordFieldback to summary
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.

Parameters
text:String

the text to be displayed, null if none

JPasswordFieldback to summary
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.

Parameters
columns:int

the number of columns >= 0

JPasswordFieldback to summary
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.

Parameters
text:String

the text to be displayed, null if none

columns:int

the number of columns >= 0

JPasswordFieldback to summary
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.

Parameters
doc:Document

the text storage to use

txt:String

the text to be displayed, null if none

columns:int

the number of columns to use to calculate the preferred width >= 0; if columns is set to zero, the preferred width will be whatever naturally results from the component implementation

Method Detail

copyback to summary
public void copy()

Overrides javax.swing.text.JTextComponent.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.

customSetUIPropertyback to summary
pack-priv boolean customSetUIProperty(String propertyName, Object value)

This method is a hack to get around the fact that we cannot directly override setUIProperty because part of the inheritance hierarchy 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.

cutback to summary
public void cut()

Overrides javax.swing.text.JTextComponent.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.

echoCharIsSetback to summary
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.

Returns:boolean

true if a character is set for echoing

See Also
setEchoChar, getEchoChar
getAccessibleContextback to summary
public AccessibleContext getAccessibleContext()

Overrides javax.swing.JTextField.getAccessibleContext.

Implements javax.accessibility.Accessible.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.

Returns:AccessibleContext

an AccessibleJPasswordField that serves as the AccessibleContext of this JPasswordField

Annotations
@BeanProperty
bound:false
getEchoCharback to summary
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.

Returns:char

the echo character, 0 if unset

See Also
setEchoChar, echoCharIsSet
getPasswordback to summary
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.

Returns:char[]

the text

Annotations
@BeanProperty
bound:false
getTextback to summary
public String getText()

Overrides javax.swing.text.JTextComponent.getText.

Deprecated

As of Java 2 platform v1.2, replaced by getPassword.

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.

Returns:String

the text

Annotations
@Deprecated
getTextback to summary
public String getText(int offs, int len) throws BadLocationException

Overrides javax.swing.text.JTextComponent.getText.

Deprecated

As of Java 2 platform v1.2, replaced by getPassword.

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.

Parameters
offs:int

the offset >= 0

len:int

the length >= 0

Returns:String

the text

Annotations
@Deprecated
Exceptions
BadLocationException:
if the offset or length are invalid
getUIClassIDback to summary
public String getUIClassID()

Overrides javax.swing.JTextField.getUIClassID.

Returns the name of the L&F class that renders this component.

Returns:String

the string "PasswordFieldUI"

Annotations
@BeanProperty
bound:false
See Also
JComponent#getUIClassID, UIDefaults#getUI
paramStringback to summary
protected String paramString()

Overrides javax.swing.JTextField.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.

Returns:String

a string representation of this JPasswordField

setEchoCharback to summary
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.

Parameters
c:char

the echo character to display

Annotations
@BeanProperty
bound:false
visualUpdate:true
description:character to display in place of the real characters
See Also
echoCharIsSet, getEchoChar
setTextback to summary
public void setText(String t)

Overrides javax.swing.text.JTextComponent.setText.

Doc from javax.swing.text.JTextComponent.setText.

Sets the text of this TextComponent to the specified text. If the text is null or empty, has the effect of simply deleting the old text. When text has been inserted, the resulting caret location is determined by the implementation of the caret class.

Note that text is not a bound property, so no PropertyChangeEvent is fired when it changes. To listen for changes to the text, use DocumentListener.

Annotations
@Override
@BeanProperty
bound:false
description:the text of this component
updateUIback to summary
public void updateUI()

Overrides javax.swing.text.JTextComponent.updateUI.

Doc from javax.swing.text.JTextComponent.updateUI.

Reloads the pluggable UI. The key used to fetch the new interface is getUIClassID(). The type of the UI is TextUI. invalidate is called after setting the UI.

Since
1.6
writeObjectback to summary
private void writeObject(ObjectOutputStream s) throws IOException

Hides javax.swing.JTextField.writeObject.

See readObject() and writeObject() in JComponent for more information about serialization in Swing.

Annotations
@Serial
Exceptions
IOException:

Doc from java.awt.Container.writeObject.

if an I/O error occurs

javax.swing back to summary

protected Class JPasswordField.AccessibleJPasswordField

extends AccessibleJTextField
Class Inheritance

This class implements accessibility support for the JPasswordField class. It provides an implementation of the Java Accessibility API appropriate to password field user-interface elements.

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 JavaBeans has been added to the java.beans package. Please see java.beans.XMLEncoder.

Constructor Summary

AccessConstructor and Description
protected
AccessibleJPasswordField()

Constructs an AccessibleJPasswordField.

Method Summary

Modifier and TypeMethod and Description
public AccessibleRole

Returns:

an instance of AccessibleRole describing the role of the object (AccessibleRole.PASSWORD_TEXT)
getAccessibleRole
()

Overrides javax.swing.text.JTextComponent.AccessibleJTextComponent.getAccessibleRole.

Gets the role of this object.

public AccessibleText

Returns:

AccessibleText for the JPasswordField
getAccessibleText
()

Overrides javax.swing.text.JTextComponent.AccessibleJTextComponent.getAccessibleText.

Gets the AccessibleText for the JPasswordField.

public String

Returns:

a String if part and index are valid. Otherwise, null is returned
getAfterIndex
(int
the CHARACTER, WORD or SENTENCE to retrieve
part
,
int
an index within the text
index
)

Overrides javax.swing.text.JTextComponent.AccessibleJTextComponent.getAfterIndex.

Implements javax.accessibility.AccessibleText.getAfterIndex.

Returns the String after a given index.

public String

Returns:

a String if part and index are valid. Otherwise, null is returned
getAtIndex
(int
the CHARACTER, WORD or SENTENCE to retrieve
part
,
int
an index within the text
index
)

Overrides javax.swing.text.JTextComponent.AccessibleJTextComponent.getAtIndex.

Implements javax.accessibility.AccessibleText.getAtIndex.

Returns the String at a given index.

public String

Returns:

a String if part and index are valid. Otherwise, null is returned
getBeforeIndex
(int
the CHARACTER, WORD or SENTENCE to retrieve
part
,
int
an index within the text
index
)

Overrides javax.swing.text.JTextComponent.AccessibleJTextComponent.getBeforeIndex.

Implements javax.accessibility.AccessibleText.getBeforeIndex.

Returns the String before a given index.

private String
public String

Returns:

the text string if the indices are valid. Otherwise, null is returned
getTextRange
(int
the start index in the text
startIndex
,
int
the end index in the text
endIndex
)

Overrides javax.swing.text.JTextComponent.AccessibleJTextComponent.getTextRange.

Implements javax.accessibility.AccessibleEditableText.getTextRange, javax.accessibility.AccessibleExtendedText.getTextRange.

Returns the text between two indices.

public AccessibleTextSequence

Returns:

an AccessibleTextSequence specifying the text if part and index are valid. Otherwise, null is returned
getTextSequenceAfter
(int
the CHARACTER, WORD, SENTENCE, LINE or ATTRIBUTE_RUN to retrieve
part
,
int
an index within the text
index
)

Overrides javax.swing.text.JTextComponent.AccessibleJTextComponent.getTextSequenceAfter.

Implements javax.accessibility.AccessibleExtendedText.getTextSequenceAfter.

Returns the AccessibleTextSequence after a given index.

public AccessibleTextSequence

Returns:

an AccessibleTextSequence specifying the text if part and index are valid. Otherwise, null is returned
getTextSequenceAt
(int
the CHARACTER, WORD, SENTENCE, LINE or ATTRIBUTE_RUN to retrieve
part
,
int
an index within the text
index
)

Overrides javax.swing.text.JTextComponent.AccessibleJTextComponent.getTextSequenceAt.

Implements javax.accessibility.AccessibleExtendedText.getTextSequenceAt.

Returns the AccessibleTextSequence at a given index.

public AccessibleTextSequence

Returns:

an AccessibleTextSequence specifying the text if part and index are valid. Otherwise, null is returned
getTextSequenceBefore
(int
the CHARACTER, WORD, SENTENCE, LINE or ATTRIBUTE_RUN to retrieve
part
,
int
an index within the text
index
)

Overrides javax.swing.text.JTextComponent.AccessibleJTextComponent.getTextSequenceBefore.

Implements javax.accessibility.AccessibleExtendedText.getTextSequenceBefore.

Returns the AccessibleTextSequence before a given index.

Inherited from javax.swing.JTextField.AccessibleJTextField:
getAccessibleStateSet

Constructor Detail

AccessibleJPasswordFieldback to summary
protected AccessibleJPasswordField()

Constructs an AccessibleJPasswordField.

Method Detail

getAccessibleRoleback to summary
public AccessibleRole getAccessibleRole()

Overrides javax.swing.text.JTextComponent.AccessibleJTextComponent.getAccessibleRole.

Gets the role of this object.

Returns:AccessibleRole

an instance of AccessibleRole describing the role of the object (AccessibleRole.PASSWORD_TEXT)

See Also
AccessibleRole
getAccessibleTextback to summary
public AccessibleText getAccessibleText()

Overrides javax.swing.text.JTextComponent.AccessibleJTextComponent.getAccessibleText.

Gets the AccessibleText for the JPasswordField. The returned object also implements the AccessibleExtendedText interface.

Returns:AccessibleText

AccessibleText for the JPasswordField

Since
1.6
See Also
javax.accessibility.AccessibleContext, javax.accessibility.AccessibleContext#getAccessibleText, javax.accessibility.AccessibleText, javax.accessibility.AccessibleExtendedText
getAfterIndexback to summary
public String getAfterIndex(int part, int index)

Overrides javax.swing.text.JTextComponent.AccessibleJTextComponent.getAfterIndex.

Implements javax.accessibility.AccessibleText.getAfterIndex.

Returns the String after a given index.

Parameters
part:int

the CHARACTER, WORD or SENTENCE to retrieve

index:int

an index within the text

Returns:String

a String if part and index are valid. Otherwise, null is returned

Since
1.6
See Also
javax.accessibility.AccessibleText#CHARACTER, javax.accessibility.AccessibleText#WORD, javax.accessibility.AccessibleText#SENTENCE
getAtIndexback to summary
public String getAtIndex(int part, int index)

Overrides javax.swing.text.JTextComponent.AccessibleJTextComponent.getAtIndex.

Implements javax.accessibility.AccessibleText.getAtIndex.

Returns the String at a given index.

Parameters
part:int

the CHARACTER, WORD or SENTENCE to retrieve

index:int

an index within the text

Returns:String

a String if part and index are valid. Otherwise, null is returned

Since
1.6
See Also
javax.accessibility.AccessibleText#CHARACTER, javax.accessibility.AccessibleText#WORD, javax.accessibility.AccessibleText#SENTENCE
getBeforeIndexback to summary
public String getBeforeIndex(int part, int index)

Overrides javax.swing.text.JTextComponent.AccessibleJTextComponent.getBeforeIndex.

Implements javax.accessibility.AccessibleText.getBeforeIndex.

Returns the String before a given index.

Parameters
part:int

the CHARACTER, WORD or SENTENCE to retrieve

index:int

an index within the text

Returns:String

a String if part and index are valid. Otherwise, null is returned

Since
1.6
See Also
javax.accessibility.AccessibleText#CHARACTER, javax.accessibility.AccessibleText#WORD, javax.accessibility.AccessibleText#SENTENCE
getEchoStringback to summary
private String getEchoString(String str)
getTextRangeback to summary
public String getTextRange(int startIndex, int endIndex)

Overrides javax.swing.text.JTextComponent.AccessibleJTextComponent.getTextRange.

Implements javax.accessibility.AccessibleEditableText.getTextRange, javax.accessibility.AccessibleExtendedText.getTextRange.

Returns the text between two indices.

Parameters
startIndex:int

the start index in the text

endIndex:int

the end index in the text

Returns:String

the text string if the indices are valid. Otherwise, null is returned

Since
1.6
getTextSequenceAfterback to summary
public AccessibleTextSequence getTextSequenceAfter(int part, int index)

Overrides javax.swing.text.JTextComponent.AccessibleJTextComponent.getTextSequenceAfter.

Implements javax.accessibility.AccessibleExtendedText.getTextSequenceAfter.

Returns the AccessibleTextSequence after a given index.

Parameters
part:int

the CHARACTER, WORD, SENTENCE, LINE or ATTRIBUTE_RUN to retrieve

index:int

an index within the text

Returns:AccessibleTextSequence

an AccessibleTextSequence specifying the text if part and index are valid. Otherwise, null is returned

Since
1.6
See Also
javax.accessibility.AccessibleText#CHARACTER, javax.accessibility.AccessibleText#WORD, javax.accessibility.AccessibleText#SENTENCE, javax.accessibility.AccessibleExtendedText#LINE, javax.accessibility.AccessibleExtendedText#ATTRIBUTE_RUN
getTextSequenceAtback to summary
public AccessibleTextSequence getTextSequenceAt(int part, int index)

Overrides javax.swing.text.JTextComponent.AccessibleJTextComponent.getTextSequenceAt.

Implements javax.accessibility.AccessibleExtendedText.getTextSequenceAt.

Returns the AccessibleTextSequence at a given index.

Parameters
part:int

the CHARACTER, WORD, SENTENCE, LINE or ATTRIBUTE_RUN to retrieve

index:int

an index within the text

Returns:AccessibleTextSequence

an AccessibleTextSequence specifying the text if part and index are valid. Otherwise, null is returned

Since
1.6
See Also
javax.accessibility.AccessibleText#CHARACTER, javax.accessibility.AccessibleText#WORD, javax.accessibility.AccessibleText#SENTENCE, javax.accessibility.AccessibleExtendedText#LINE, javax.accessibility.AccessibleExtendedText#ATTRIBUTE_RUN
getTextSequenceBeforeback to summary
public AccessibleTextSequence getTextSequenceBefore(int part, int index)

Overrides javax.swing.text.JTextComponent.AccessibleJTextComponent.getTextSequenceBefore.

Implements javax.accessibility.AccessibleExtendedText.getTextSequenceBefore.

Returns the AccessibleTextSequence before a given index.

Parameters
part:int

the CHARACTER, WORD, SENTENCE, LINE or ATTRIBUTE_RUN to retrieve

index:int

an index within the text

Returns:AccessibleTextSequence

an AccessibleTextSequence specifying the text if part and index are valid. Otherwise, null is returned

Since
1.6
See Also
javax.accessibility.AccessibleText#CHARACTER, javax.accessibility.AccessibleText#WORD, javax.accessibility.AccessibleText#SENTENCE, javax.accessibility.AccessibleExtendedText#LINE, javax.accessibility.AccessibleExtendedText#ATTRIBUTE_RUN