JPasswordField
Home ] Up ] JTextComponent ] JTextField ] [ JPasswordField ] JTextArea ] The Document Model ] JTextArea Scrolling ] JTextArea Editing ]

 

 

A JPasswordField is a special case of a JTextField. (it's a subclass of JTextField).  

It does not display the text stored in this field;  instead, it echoes each character of the text with an "echo" character instead of the real character.  

For example:

package swingExamples;

import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

class PasswordFieldPanel extends JPanel
{
  public PasswordFieldPanel()
  {
    setLayout( new FlowLayout(FlowLayout.LEFT) );
    JLabel userLabel = new JLabel("Username:");
    add(userLabel);
    JTextField user = new JTextField(25);
    add(user);
    JLabel passwordLabel = new JLabel("Password:");
    add(passwordLabel);
    JPasswordField password = new JPasswordField(25);
    add(password);
  }
}

class PasswordFieldFrame extends JFrame
{
  public PasswordFieldFrame()
  {
    setTitle("PasswordField");
    setSize(300, 200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container contentPane = getContentPane();
    contentPane.add( new PasswordFieldPanel() );
  }
}

public class PasswordField
{
  public static void main(String[] args)
  {
    PasswordFieldFrame frame = new PasswordFieldFrame();
    frame.setVisible(true);
  }
}

which produces:

 

This page was last modified on 02 October, 2007