Get password from JPasswordField

Source code below will show you, how to get password that we put in JPasswordField.

*************************************************************************
COMPLETE SOURCE CODE FOR : GetPassword.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 GetPassword implements ActionListener
{
//Create a password field using JPasswordField
JPasswordField passwordField=new JPasswordField();

//Create a button with text ( Get the password )
JButton button=new JButton("Get the password");

//Create a JFrame with title ( Get password from JPasswordField )
JFrame frame=new JFrame("Get password from JPasswordField");

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

//Set JFrame layout to GridLayout.
//ROW : 2
//COLUMN : 1
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 : 500 pixels
//HEIGHT : 100 pixels
frame.setSize(500,100);

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

//Operation for button
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==button)
{
//Show message box that contains your password
JOptionPane.showMessageDialog(frame,"MY PASSWORD IS : "+passwordField.getText());
}
}

public static void main(String[]args)
{
GetPassword gp=new GetPassword();
}
}


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

RELAXING NATURE VIDEO