JSR-209 (Final Release)

javax.swing
Class JPasswordField

java.lang.Object
  extended byjava.awt.Component
      extended byjava.awt.Container
          extended byjavax.swing.JComponent
              extended byjavax.swing.text.JTextComponent
                  extended byjavax.swing.JTextField
                      extended byjavax.swing.JPasswordField
All Implemented Interfaces:
java.awt.image.ImageObserver, Scrollable, java.io.Serializable, SwingConstants

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.

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 java.beans.XMLEncoder.


Nested Class Summary
 
Nested classes inherited from class javax.swing.text.JTextComponent
JTextComponent.KeyBinding
 
Field Summary
 
Fields inherited from class javax.swing.JTextField
notifyAction
 
Fields inherited from class javax.swing.text.JTextComponent
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY
 
Fields inherited from class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface javax.swing.SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
JPasswordField()
          Constructs a new JPasswordField, with a default document, null starting text string, and 0 column width.
JPasswordField(Document doc, java.lang.String txt, int columns)
          Constructs a new JPasswordField that uses the given text storage model and the given number of columns.
JPasswordField(int columns)
          Constructs a new empty JPasswordField with the specified number of columns.
JPasswordField(java.lang.String text)
          Constructs a new JPasswordField initialized with the specified text.
JPasswordField(java.lang.String text, int columns)
          Constructs a new JPasswordField initialized with the specified text and columns.
 
Method Summary
 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.
 char getEchoChar()
          Returns the character to be used for echoing.
 char[] getPassword()
          Returns the text contained in this TextComponent.
protected  java.lang.String paramString()
          Returns a string representation of this JPasswordField.
 void setEchoChar(char c)
          Sets the echo character for this JPasswordField.
 
Methods inherited from class javax.swing.JTextField
addActionListener, configurePropertiesFromAction, createActionPropertyChangeListener, createDefaultModel, fireActionPerformed, getAction, getActionListeners, getActions, getColumns, getColumnWidth, getHorizontalAlignment, getHorizontalVisibility, getPreferredSize, getScrollOffset, isValidateRoot, paint, paintBorder, paintComponent, paintComponents, postActionEvent, printBorder, printComponent, printComponents, removeActionListener, scrollRectToVisible, setAction, setActionCommand, setColumns, setDocument, setFont, setHorizontalAlignment, setScrollOffset
 
Methods inherited from class javax.swing.text.JTextComponent
addCaretListener, addInputMethodListener, addKeymap, fireCaretUpdate, getCaret, getCaretColor, getCaretListeners, getCaretPosition, getDisabledTextColor, getDocument, getFocusAccelerator, getHighlighter, getInputMethodRequests, getKeymap, getKeymap, getMargin, getNavigationFilter, getPreferredScrollableViewportSize, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getScrollableUnitIncrement, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, getText, isEditable, loadKeymap, modelToView, moveCaretPosition, paste, processInputMethodEvent, read, removeCaretListener, removeKeymap, removeNotify, replaceSelection, select, selectAll, setCaret, setCaretColor, setCaretPosition, setDisabledTextColor, setEditable, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setText, viewToModel, write
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getSize, getToolTipText, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, isDoubleBuffered, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, paintChildren, paintImmediately, paintImmediately, print, printAll, printChildren, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDefaultLocale, setDoubleBuffered, setEnabled, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, areFocusTraversalKeysSet, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, list, list, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
addComponentListener, addFocusListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentListeners, getCursor, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getIgnoreRepaint, getInputContext, getInputMethodListeners, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getSize, getToolkit, getTreeLock, hasFocus, imageUpdate, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isShowing, isValid, isVisible, list, list, list, paintAll, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processMouseEvent, processMouseWheelEvent, removeComponentListener, removeFocusListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, setBounds, setBounds, setCursor, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JPasswordField

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


JPasswordField

public JPasswordField(java.lang.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 - the text to be displayed, null if none

JPasswordField

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 - the number of columns >= 0

JPasswordField

public JPasswordField(java.lang.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 - the text to be displayed, null if none
columns - the number of columns >= 0

JPasswordField

public JPasswordField(Document doc,
                      java.lang.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 '*'. If the document model is null, a default one will be created.

Parameters:
doc - the text storage to use
txt - the text to be displayed, null if none
columns - 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

getEchoChar

public char getEchoChar()
Returns the character to be used for echoing. The default is '*'.

Returns:
the echo character, 0 if unset
See Also:
setEchoChar(char), echoCharIsSet()

setEchoChar

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 - the echo character to display
See Also:
echoCharIsSet(), getEchoChar()

echoCharIsSet

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:
true if a character is set for echoing
See Also:
setEchoChar(char), getEchoChar()

cut

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.

Overrides:
cut in class JTextComponent

copy

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.

Overrides:
copy in class JTextComponent

getPassword

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:
the text

paramString

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.

Overrides:
paramString in class JTextField
Returns:
a string representation of this JPasswordField

JSR-209 (Final Release)

Java and Java 2D are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
Copyright 1993 - 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, California 95054, U.S.A. All Rights Reserved.
Use of this specification is subject to this license.