Get password length from JPasswordField

Source code below will show you, how to get password length from a JPasswordField.

*************************************************************************
COMPLETE SOURCE CODE FOR : GetPasswordLength.java
*************************************************************************


import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.GridLayout;

public class GetPasswordLength implements ActionListener
{
//Create a password field using JPasswordField with number of columns equal to 10
JPasswordField passwordField=new JPasswordField(10);

//Create a window using JFrame with title ( Get password length )
JFrame frame=new JFrame("Get password length");

//Create a button with text ( GET PASSWORD LENGTH )
JButton button=new JButton("GET PASSWORD LENGTH");

public GetPasswordLength()
{
//Add action listener to button
button.addActionListener(this);

//Set JFrame layout using GridLayout
frame.setLayout(new GridLayout(2,1));

//Add password field into JFrame
frame.add(passwordField);

//Add button into JFrame
frame.add(button);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size to :
//WIDTH = 400 pixels
//HEIGHT = 120 pixels
frame.setSize(400,120);

//Make JFrame visible. So we can see it
frame.setVisible(true);
}

//Action for button
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==button)
{
//Show message box that contain password length
JOptionPane.showMessageDialog(frame,"PASSWORD LENGTH : "+(passwordField.getText().length()));
}
}

public static void main(String[]args)
{
GetPasswordLength gpl=new GetPasswordLength();
}
}


*************************************************************************
JUST COMPILE AND EXECUTE IT
*************************************************************************

RELAXING NATURE VIDEO