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
*************************************************************************

Create file chooser using JFileChooser

Source code below will show you, how to create a file chooser in java using JFileChooser class.

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


import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JButton;

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

import java.awt.FlowLayout;

public class CreateFileChooserUsingJFileChooser implements ActionListener
{
//Create file chooser using JFileChooser
JFileChooser fileChooser=new JFileChooser();

//Create a button that use to open file chooser
JButton button=new JButton("Click here to open file chooser");

//Create a JFrame that use to place created button
//JFrame title is ( Create file chooser using JFileChooser )
JFrame frame=new JFrame("Create file chooser using JFileChooser");

public CreateFileChooserUsingJFileChooser()
{
//add ActionListener to created button
button.addActionListener(this);

//set JFrame layout to FlowLayout
frame.setLayout(new FlowLayout());

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

//set default close operation for JFrame
//So when you click window close button, this program will exit
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//set JFrame size to :
//WIDTH : 500 pixels
//HEIGHT : 500 pixels
frame.setSize(500,500);

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

//Action that will take by button
public void actionPerformed(ActionEvent event)
{
//When you click the button, a file chooser will appear
if(event.getSource()==button)
{
//Show file chooser with title ( Choose a file )
fileChooser.showDialog(frame,"Choose a file");
}
}

public static void main(String[]args)
{
CreateFileChooserUsingJFileChooser cfcujfc=new CreateFileChooserUsingJFileChooser();
}
}


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

Create file chooser in java

In java, you can create file chooser using JFileChooser class.

Create password field using JPasswordField

Source code below will show you, how to create password field in java using JPasswordField class.

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


import javax.swing.JFrame;
import javax.swing.JPasswordField;

import java.awt.FlowLayout;

public class CreatePasswordFieldUsingJPasswordField
{
public static void main(String[]args)
{
//Create password field with number of columns equal 10
//Number of columns is use to calculate preferred size
//You can change it for what value that you want but make sure it positive integer
JPasswordField passwordField=new JPasswordField(10);

//Create JFrame that will be use to place created JPasswordField
JFrame frame=new JFrame("Create password field");

//Set JFrame layout to FlowLayout
frame.setLayout(new FlowLayout());

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

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

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

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


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

RELAXING NATURE VIDEO