Header javaperspective.com
JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.12. Password Fields

5.12. Password Fields
Last updated: 5 January 2013.

This tutorial will show you how to use password fields in Java.

A password field is an instance of the class JPasswordField. Since JPasswordField inherits JTextField, password fields have all the features provided by text fields. Particularly, an action event is fired when the user hits enter. A password field has the appearance of a normal text field except that the characters the user types in are not shown. Instead, each character is replaced by a dot (the default) or any other user defined echo character. You can call the method setEchoChar(char c) to specify an echo character.

The value of a password field is not stored in a string but in an array of characters for security reasons. It is recommended to reset the array of characters containing a password when you have finished using it. For example, you can fill the array with zeros.

The following picture shows a basic example of a frame containing a password field. When the user enters the correct password, the frame is simply hidden.



Password field example



Here is the code:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public final class PasswordFields extends JFrame implements ActionListener {

   
private JPasswordField passwordField;

   
public PasswordFields() {
         
init();
          addComponents
();

          setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
          setVisible
(true);
   
}


   
public void actionPerformed(ActionEvent e){
         
char[] expectedPassword = { 'j', 'a', 'v', 'a', 'p', 'e', 'r', 's', 'p' , 'e' , 'c' , 't' , 'i' , 'v' , 'e' };
         
char[] typedPassword = passwordField.getPassword();

         
if(Arrays.equals(expectedPassword, typedPassword)){
               
// Hide the frame
               
setVisible(false);
         
}

         
// Fill both arrays with zeros
         
Arrays.fill(expectedPassword, '0');
          Arrays.fill
(typedPassword, '0');
   
}


   
private void addComponents() {
         
// Set up the content pane
         
JPanel contentPane = new JPanel(new BorderLayout());
          contentPane.setBorder
(BorderFactory.createEmptyBorder(10, 10, 10, 10));

         
// Set up the label
         
JLabel label = new JLabel("Password", SwingConstants.TRAILING);
          label.setBorder
(BorderFactory.createEmptyBorder(0, 0, 0, 10));
          contentPane.add
(label, BorderLayout.LINE_START);

         
// Set up the password field
         
passwordField = new JPasswordField(15);
          passwordField.addActionListener
(this);
          contentPane.add
(passwordField);

         
// Add the content pane to the JFrame
         
add(contentPane);
   
}


   
private void init() {
         
setTitle("PasswordFields");
          setSize
(250, 75);
          setLocationRelativeTo
(null);
   
}


   
public static void main(String[] args){
         
SwingUtilities.invokeLater(new Runnable() {
               
public void run() {
                     
new PasswordFields();
               
}
          })
;
   
}
}


You are here :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.12. Password Fields
Next tutorial :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.13. Tool tips

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7