Showing posts with label password. Show all posts
Showing posts with label password. Show all posts

Set password length for JPasswordField

Complete source code below will show you, how you can set password length in JPasswordField. In source code below, we will enable user to enter only password that contains five characters.After that it will disable password field. User can delete entered password by press 'Backspace' key.

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


import javax.swing.*;

import javax.swing.event.*;

import java.awt.event.*;

import java.awt.FlowLayout;

public class SetPasswordLength implements CaretListener
{
//Create password field
JPasswordField passwordField=new JPasswordField(12);

JLabel label=new JLabel("Press Backspace to delete password");

JFrame frame;

//Create key listener that will listen when someone press Backspace key
KeyListener kl=new KeyAdapter()
{
public void keyPressed(KeyEvent event)
{
if(event.getKeyCode()==KeyEvent.VK_BACK_SPACE)
{
passwordField.setEditable(true);

label.show(false);

frame.repaint();

frame.validate();
}
}
};

public SetPasswordLength()
{
label.show(false);

passwordField.addCaretListener(this);

passwordField.addKeyListener(kl);

frame=new JFrame("Set password length");

frame.setLayout(new FlowLayout());

frame.add(passwordField);

frame.add(label);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(500,100);

frame.setVisible(true);
}

//Listen for changes in the caret position
public void caretUpdate(CaretEvent event)
{
int passwordLength=passwordField.getText().length();

//If password length equal to five characters, password field will uneditable
//You can change to any password length that you want by change 5 to any other value
if(passwordLength==5)
{
passwordField.setEditable(false);

label.show(true);

frame.repaint();

frame.validate();
}
}

public static void main(String[]args)
{
SetPasswordLength spl=new SetPasswordLength();
}
}


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

Set password symbol or echo character color for JPasswordField

Source code below will show you, how to set JPasswordField echo character color base on RGB. You can get RGB value for your color using "Color picker" at above.

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


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

import java.awt.Color;

public class SetJPasswordFieldEchoCharacterColor
{
public static void main(String[]args)
{
//Create password field with number of columns equal to 10
JPasswordField passwordField=new JPasswordField(10);

//Create JFrame with title ( Set JPasswordField echo character color )
JFrame frame=new JFrame("Set JPasswordField echo character color");

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above
//R=255
//G=0
//B=0
Color color=new Color(255,0,0);

//Set JPasswordField echo character color to color that you choose
passwordField.setForeground(color);

//Add JPasswordField into JFrame
frame.add(passwordField);

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

//Set JFrame size
frame.setSize(500,65);

//Make JFrame visible
frame.setVisible(true);
}
}


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

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

Set password symbol or echo character for JPasswordField

Source code below will show you, how to set symbol that will be use in password field that build using JPasswordField. It also called echo character.

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


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

import java.awt.FlowLayout;

public class SetPasswordSymbol
{
public static void main(String[]args)
{
//Create a password field with number of columns equal to 10
JPasswordField passwordField=new JPasswordField(10);

//Set password symbol
//In my case i use *
//You can change for what you want.But make sure it is only one character
//This is because, method setEchoChar receive only one character
passwordField.setEchoChar('*');


//Create a JFrame with title ( Set Password Symbol )
JFrame frame=new JFrame("Set Password Symbol");

//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 : 300 pixels
//HEIGHT : 65 pixels
frame.setSize(300,65);

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


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

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

Create password field in java

In java, you can create a password field using JPasswordField class.

RELAXING NATURE VIDEO