Showing posts with label length. Show all posts
Showing posts with label length. 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
**********************************************************************

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