ImageObserver
, MenuContainer
, Serializable
, Accessible
, Scrollable
, SwingConstants
@JavaBean(description="Allows the editing of a line of text but doesn\'t show the characters.") public 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: 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 XMLEncoder
.
Modifier and Type | Class | Description |
---|---|---|
protected class |
JPasswordField.AccessibleJPasswordField |
This class implements accessibility support for the JPasswordField class. |
JTextField.AccessibleJTextField
JTextComponent.AccessibleJTextComponent, JTextComponent.DropLocation, JTextComponent.KeyBinding
JComponent.AccessibleJComponent
Container.AccessibleAWTContainer
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
notifyAction
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
Constructor | Description |
---|---|
JPasswordField() |
Constructs a new JPasswordField , with a default document, null starting text string, and 0 column width. |
JPasswordField |
Constructs a new empty JPasswordField with the specified number of columns. |
JPasswordField |
Constructs a new JPasswordField initialized with the specified text. |
JPasswordField |
Constructs a new JPasswordField initialized with the specified text and columns. |
JPasswordField |
Constructs a new JPasswordField that uses the given text storage model and the given number of columns. |
Modifier and Type | Method | Description |
---|---|---|
void |
copy() |
Invokes provideErrorFeedback on the current look and feel, which typically initiates an error beep. |
void |
cut() |
Invokes provideErrorFeedback on the current look and feel, which typically initiates an error beep. |
boolean |
echoCharIsSet() |
Returns true if this JPasswordField has a character set for echoing. |
AccessibleContext |
getAccessibleContext() |
Returns the AccessibleContext associated with this JPasswordField . |
char |
getEchoChar() |
Returns the character to be used for echoing. |
char[] |
getPassword() |
Returns the text contained in this TextComponent . |
String |
getText() |
Deprecated. |
String |
getText |
Deprecated. As of Java 2 platform v1.2, replaced by getPassword . |
String |
getUIClassID() |
Returns the name of the L&F class that renders this component. |
protected String |
paramString() |
Returns a string representation of this JPasswordField . |
void |
setEchoChar |
Sets the echo character for this JPasswordField . |
void |
setText |
Sets the text of this TextComponent to the specified text. |
void |
updateUI() |
Reloads the pluggable UI. |
actionPropertyChanged, addActionListener, configurePropertiesFromAction, createActionPropertyChangeListener, createDefaultModel, fireActionPerformed, getAction, getActionListeners, getActions, getColumns, getColumnWidth, getHorizontalAlignment, getHorizontalVisibility, getPreferredSize, getScrollOffset, isValidateRoot, postActionEvent, removeActionListener, scrollRectToVisible, setAction, setActionCommand, setColumns, setDocument, setFont, setHorizontalAlignment, setScrollOffset
addCaretListener, addKeymap, fireCaretUpdate, getCaret, getCaretColor, getCaretListeners, getCaretPosition, getDisabledTextColor, getDocument, getDragEnabled, getDropLocation, getDropMode, getFocusAccelerator, getHighlighter, getInputMethodRequests, getKeymap, getKeymap, getMargin, getNavigationFilter, getPreferredScrollableViewportSize, getPrintable, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getScrollableUnitIncrement, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getToolTipText, getUI, isEditable, loadKeymap, modelToView, modelToView2D, moveCaretPosition, paste, print, print, print, read, removeCaretListener, removeKeymap, replaceSelection, restoreComposedText, saveComposedText, select, selectAll, setCaret, setCaretColor, setCaretPosition, setDisabledTextColor, setDragEnabled, setDropMode, setEditable, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setUI, viewToModel, viewToModel2D, write
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
public JPasswordField()
JPasswordField
, with a default document, null
starting text string, and 0 column width.public JPasswordField(String text)
JPasswordField
initialized with the specified text. The document model is set to the default, and the number of columns to 0.text
- the text to be displayed, null
if nonepublic JPasswordField(int columns)
JPasswordField
with the specified number of columns. A default model is created, and the initial string is set to null
.columns
- the number of columns >= 0public JPasswordField(String text, int columns)
JPasswordField
initialized with the specified text and columns. The document model is set to the default.text
- the text to be displayed, null
if nonecolumns
- the number of columns >= 0public JPasswordField(Document doc, String txt, int columns)
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.doc
- the text storage to usetxt
- the text to be displayed, null
if nonecolumns
- 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@BeanProperty(bound=false) public String getUIClassID()
getUIClassID
in class JTextField
public void updateUI()
getUIClassID()
. The type of the UI is TextUI
. invalidate
is called after setting the UI.updateUI
in class JTextComponent
public char getEchoChar()
@BeanProperty(bound=false, visualUpdate=true, description="character to display in place of the real characters") public void setEchoChar(char c)
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
.c
- the echo character to displaypublic boolean echoCharIsSet()
JPasswordField
has a character set for echoing. A character is considered to be set if the echo character is not 0.public void cut()
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.cut
in class JTextComponent
public void copy()
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.copy
in class JTextComponent
@Deprecated public String getText()
getPassword
.TextComponent
. If the underlying document is null
, will give a NullPointerException
. For security reasons, this method is deprecated. Use the * getPassword
method instead.
getText
in class JTextComponent
@Deprecated public String getText(int offs, int len) throws BadLocationException
getPassword
. For security reasons, this method is deprecated. Use the getPassword
method instead.
getText
in class JTextComponent
offs
- the offset >= 0len
- the length >= 0BadLocationException
- if the offset or length are invalid@BeanProperty(bound=false, description="the text of this component") public void setText(String t)
JTextComponent
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
.
setText
in class JTextComponent
t
- the new text to be set@BeanProperty(bound=false) public char[] getPassword()
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.protected String paramString()
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
.paramString
in class JTextField
JPasswordField
@BeanProperty(bound=false) public AccessibleContext getAccessibleContext()
AccessibleContext
associated with this JPasswordField
. For password fields, the AccessibleContext
takes the form of an AccessibleJPasswordField
. A new AccessibleJPasswordField
instance is created if necessary.getAccessibleContext
in interface Accessible
getAccessibleContext
in class JTextField
AccessibleJPasswordField
that serves as the AccessibleContext
of this JPasswordField
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.desktop/javax/swing/JPasswordField.html
getPassword
.