Constructors of the JPasswordField class:
-JPasswordField()
creates a new password field.
-JPasswordField(int columns)
create a new pasword field with specified number of columns.
-JPasswordField(String text)
creates a new password field with the default password text.
-JPasswordField(String text, int columns)
create a new password field with the default password text and number of columns.
-JPasswordField(Document doc, String text, int columns)
create a new password field with the specified document model, the default password text and number of columns.
Useful methods of the JPasswordField class:
-getPassword():char[]
gets the text from the password field.
-setEchoChar(char ch):void
sets echo character of the password field. This character is shown in the password field instead of the original characters.
Example:
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.plaf.basic.BasicBorders;
class JPasswordFieldShow extends JFrame implements DocumentListener{
JTextField txtname;
JPasswordField txtpassword;
JLabel lbl;
JPasswordFieldShow(String title){
setTitle(title);
setSize(500,150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contpane=getContentPane();
contpane.setLayout(new FlowLayout());
JPanel p1=new JPanel();
p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));
JPanel p2=new JPanel();
p2.setLayout(new BoxLayout(p2, BoxLayout.Y_AXIS));
JLabel lblname=new JLabel("Name:");
txtname=new JTextField(20);
JLabel lblpassword=new JLabel("Password:");
txtpassword=new JPasswordField(20);
txtpassword.setEchoChar('*');
txtpassword.getDocument().addDocumentListener(this);
JButton btok=new JButton("OK");
btok.setBorder(new BasicBorders.ButtonBorder(Color.GREEN,Color.BLACK,Color.GREEN,Color.YELLOW));
lbl=new JLabel(" ");
lbl.setForeground(Color.RED);
lbl.setLabelFor(txtpassword);
p1.add(lblname);
p2.add(txtname);
p1.add(lblpassword);
p2.add(txtpassword);
p1.add(btok);
p2.add(lbl);
JPanel mp=new JPanel();
mp.setLayout(new BoxLayout(mp,BoxLayout.X_AXIS));
mp.add(p1);
mp.add(p2);
contpane.add(mp);
setVisible(true);
}
public void checkPassword(char[] passwordText){
boolean str=strongPass(passwordText);
if(str)
lbl.setText("Strong password");
else
lbl.setText("Weak weak");
}
public void removeUpdate(DocumentEvent e){
checkPassword(txtpassword.getPassword());
}
public void insertUpdate(DocumentEvent e){
checkPassword(txtpassword.getPassword());
}
public void changedUpdate(DocumentEvent e){
}
public boolean strongPass(char[] passwordText){
boolean strong=false;
if(passwordText.length>=8){
if(countDigit(passwordText)>0 && countLowercase(passwordText)>0 && countUppercase(passwordText)>0 && countSymbol(passwordText)>0)
strong=true;
}
return strong;
}
public int countDigit(char[] passwordText){
int digitNum=0;
for(char c:passwordText){
if(c>='0' && c<='9')
digitNum++;
}
return digitNum;
}
public int countLowercase(char[] passwordText){
int lowercaseNum=0;
for(char c:passwordText){
if(c>='a' && c<='z')
lowercaseNum++;
}
return lowercaseNum;
}
public int countUppercase(char[] passwordText){
int uppercaseNum=0;
for(char c:passwordText){
if(c>='A' && c<='Z')
uppercaseNum++;
}
return uppercaseNum;
}
public int countSymbol(char[] passwordText){
int symNum=0;
for(char c:passwordText){
if((c>='!' && c<='/') || (c>=':' && c<='@') || (c>='[' && c<='`') || (c>='{' && c<='~' ))
symNum++;
}
return symNum;
}
}
public class JFrameSwing {
public static void main(String[] args){
new JPasswordFieldShow("JPasswordField");
}
}
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.plaf.basic.BasicBorders;
class JPasswordFieldShow extends JFrame implements DocumentListener{
JTextField txtname;
JPasswordField txtpassword;
JLabel lbl;
JPasswordFieldShow(String title){
setTitle(title);
setSize(500,150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contpane=getContentPane();
contpane.setLayout(new FlowLayout());
JPanel p1=new JPanel();
p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));
JPanel p2=new JPanel();
p2.setLayout(new BoxLayout(p2, BoxLayout.Y_AXIS));
JLabel lblname=new JLabel("Name:");
txtname=new JTextField(20);
JLabel lblpassword=new JLabel("Password:");
txtpassword=new JPasswordField(20);
txtpassword.setEchoChar('*');
txtpassword.getDocument().addDocumentListener(this);
JButton btok=new JButton("OK");
btok.setBorder(new BasicBorders.ButtonBorder(Color.GREEN,Color.BLACK,Color.GREEN,Color.YELLOW));
lbl=new JLabel(" ");
lbl.setForeground(Color.RED);
lbl.setLabelFor(txtpassword);
p1.add(lblname);
p2.add(txtname);
p1.add(lblpassword);
p2.add(txtpassword);
p1.add(btok);
p2.add(lbl);
JPanel mp=new JPanel();
mp.setLayout(new BoxLayout(mp,BoxLayout.X_AXIS));
mp.add(p1);
mp.add(p2);
contpane.add(mp);
setVisible(true);
}
public void checkPassword(char[] passwordText){
boolean str=strongPass(passwordText);
if(str)
lbl.setText("Strong password");
else
lbl.setText("Weak weak");
}
public void removeUpdate(DocumentEvent e){
checkPassword(txtpassword.getPassword());
}
public void insertUpdate(DocumentEvent e){
checkPassword(txtpassword.getPassword());
}
public void changedUpdate(DocumentEvent e){
}
public boolean strongPass(char[] passwordText){
boolean strong=false;
if(passwordText.length>=8){
if(countDigit(passwordText)>0 && countLowercase(passwordText)>0 && countUppercase(passwordText)>0 && countSymbol(passwordText)>0)
strong=true;
}
return strong;
}
public int countDigit(char[] passwordText){
int digitNum=0;
for(char c:passwordText){
if(c>='0' && c<='9')
digitNum++;
}
return digitNum;
}
public int countLowercase(char[] passwordText){
int lowercaseNum=0;
for(char c:passwordText){
if(c>='a' && c<='z')
lowercaseNum++;
}
return lowercaseNum;
}
public int countUppercase(char[] passwordText){
int uppercaseNum=0;
for(char c:passwordText){
if(c>='A' && c<='Z')
uppercaseNum++;
}
return uppercaseNum;
}
public int countSymbol(char[] passwordText){
int symNum=0;
for(char c:passwordText){
if((c>='!' && c<='/') || (c>=':' && c<='@') || (c>='[' && c<='`') || (c>='{' && c<='~' ))
symNum++;
}
return symNum;
}
}
public class JFrameSwing {
public static void main(String[] args){
new JPasswordFieldShow("JPasswordField");
}
}
nice article in your blog.thank you for sharing useful info.
ReplyDeletevisit
web programming tutorial
welookups
Great blog right here! Also your site quite a bit up fast! What host are you the usage of? Can I am getting your affiliate hyperlink for your host? I wish my site loaded up as quickly as yours lol.digital marketing service
ReplyDelete