How to Create a Password Application In Java

Password login
Image by TBIT – Pixabay

Tutorial On How To Create A Password Application In Java

In this tutorial, I will show you how to create a password application in Java. This application is using JPasswordField class.

The password field, of course, let users to enter a password. And just like every form of password around the world, password field displays echo characters instead of the password itself. The default echo character is a bullet, but you can change that echo character to any character on your keyboard.

Example Of The Password Field program

Here is an example of what you can create using JPasswordField class :

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;

public class PasswordField extends JPanel
implements ActionListener {
private static String LOGIN = “Login”;
private static String HELP = “help”;

private JFrame controllingFrame; //needed for dialogs
private JPasswordField passwordField;

public PasswordField(JFrame f) {
//Use the default FlowLayout.
controllingFrame = f;

//Create everything.

passwordField = new JPasswordField(10);
passwordField.setActionCommand(LOGIN);
passwordField.addActionListener(this);

JLabel label = new JLabel(“Enter the password: “);
label.setLabelFor(passwordField);

JComponent buttonPane = createButtonPanel();

//Lay out everything.
JPanel textPane = new JPanel(new FlowLayout(FlowLayout.TRAILING));
textPane.add(label);
textPane.add(passwordField);

add(textPane);
add(buttonPane);
}

protected JComponent createButtonPanel() {
JPanel p = new JPanel(new GridLayout(0,1));
JButton loginButton = new JButton(“Login”);
JButton helpButton = new JButton(“Help”);

loginButton.setActionCommand(LOGIN);
helpButton.setActionCommand(HELP);
loginButton.addActionListener(this);
helpButton.addActionListener(this);

p.add(loginButton);
p.add(helpButton);

return p;
}

public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();

if (LOGIN.equals(cmd)) { //Process the password.
char[] input = passwordField.getPassword();
if (isPasswordCorrect(input)) {
JOptionPane.showMessageDialog(controllingFrame,
“Success! You typed the right password.”);
} else {
JOptionPane.showMessageDialog(controllingFrame,
“Invalid password. Try again.”,
“Error Message”,
JOptionPane.ERROR_MESSAGE);
}

//Zero out the possible password, for security.
Arrays.fill(input, ‘0’);

passwordField.selectAll();
resetFocus();
} else { //The user has asked for help.
JOptionPane.showMessageDialog(controllingFrame,
“You can get the password by searching this example’s\n”
+ “source code for the string \”correctPassword\”.\n”
+ “Or look at the section How to Use Password Fields in\n”
+ “the components section of The Java Tutorial.”);
}
}

private static boolean isPasswordCorrect(char[] input) {
boolean isCorrect = true;
char[] correctPassword = {‘s’,’e’,’c’,’r’,’e’,’t’}; //Here is where you set your password

if (input.length != correctPassword.length) {
isCorrect = false;
} else {
isCorrect = Arrays.equals (input, correctPassword);
}

//Zero out the password.
Arrays.fill(correctPassword,’0′);

return isCorrect;
}

protected void resetFocus() {
passwordField.requestFocusInWindow();
}

private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame(“PasswordField”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
final PasswordField newContentPane = new PasswordField(frame);
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);

frame.addWindowListener(new WindowAdapter() {
public void windowActivated(WindowEvent e) {
newContentPane.resetFocus();
}
});

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//creating and showing this application’s GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off use of bold fonts
UIManager.put(“swing.boldMetal”, Boolean.FALSE);
createAndShowGUI();
}
});
}
}

Here is the output of the program

how to create a password application in java

how to create a password application in java

EXTRA

Beside just knowing the basic of how to create a password application in java, You can change the echo character inside this program by adding this bit of code:

….
passwordField = new JPasswordField(10);
passwordField.setActionCommand(LOGIN);
passwordField.addActionListener(this);
passwordField.setEchoChar(‘*’);
….

The programming language that I specialize in is Java, because I think Java programming language is more universal and of course because I like it regardless of any reason.

Published by

Rafif Santika

The programming language that I specialize in is Java, because I think Java programming language is more universal and of course because I like it regardless of any reason.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.