JaVa
   

The JPasswordField

The JPasswordField works similarly to the JTextField but with two major differences. Instead of the text being displayed as the user types, each of the characters is replaced by the * character to prevent others from seeing what is being typed. The other main difference is that you must use the getPassword method, which retrieves a character array of what the user entered, rather than the getText method, which returns a string. Let's now look at an example that uses the JPasswordField.

Code Listing 5: Using the JPasswordField
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JPasswordFieldExample extends JFrame implements
 ActionListener
{
 public static void main(String[] argv)
 {
 JPasswordFieldExample mainApp = new JPasswordFieldExample();
 }
 public JPasswordFieldExample()
 {
 super("JPasswordField Example");
 setBounds(0, 0, 450, 125);
 getContentPane().setLayout(null);
 setDefaultCloseOperation(EXIT_ON_CLOSE);
 // Create the password field...
 passwordfield = new JPasswordField(15);
 passwordfield.setLocation(140, 10);
 passwordfield.setSize(passwordfield.getPreferredSize());
 // Create the labels...
 label = new JLabel("Enter your password:");
 label.setLocation(10, 10);
 label.setSize(label.getPreferredSize());
 passwordLabel = new JLabel("The password you entered was: ");
 passwordLabel.setLocation(10, 60);
 passwordLabel.setSize(passwordLabel.getPreferredSize());
 // Create the button...
 button = new JButton("Update");
 button.setLocation(350, 10);
 button.setSize(button.getPreferredSize());
 // Add the action listeners
 button.addActionListener(this);
 // Add the objects to the content pane...
 getContentPane().add(passwordfield);
 getContentPane().add(label);
 getContentPane().add(passwordLabel);
 getContentPane().add(button);
 setVisible(true);
 }
 public void actionPerformed(ActionEvent e)
 {
 if(e.getSource() == button)
 {
 String passwordString = String.copyValueOf
 (passwordfield.getPassword());
 passwordLabel.setText("The password you entered was: " + passwordString);
 passwordLabel.setSize(passwordLabel.getPreferredSize());
 }
 }
 JLabel label;
 JLabel passwordLabel;
 JButton button;
 JPasswordField passwordfield;
}



Java End example

If you now execute the password example and type something in the password box, you will see that it comes up as * characters. If, however, you click the Update button, the app retrieves the password from the JPasswordField and displays it as text in the label below it. Here is a screen shot of how this looks:

Java Click To expand
Screenshot: Using the JPasswordField

Let's now take a look at what we have added to the code to create the JPasswordField. To create the actual password field, use the following code.

passwordfield = new JPasswordField(15);
passwordfield.setLocation(140, 10);
passwordfield.setSize(passwordfield.getPreferredSize());


Notice that it is identical to the JTextField, except we are creating a JPasswordField object instead of a JTextField object. The only real difference with the code is where we try to retrieve the password that the user has entered. The JPasswordField's getPassword method returns the password that the user has entered as a char[] array for security reasons. So to display it, we need to first copy it to a string with the following line of code:

String passwordString = String.copyValueOf
 (passwordfield.getPassword());


Now that the string is referenced by the passwordString variable, we can display it using the passwordString variable this time instead of the getText method that we used for the JTextField.

JaVa
   
Comments