Advertisement
Guest User

JPasswordField Example

a guest
Apr 28th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4.  
  5. public class rawr implements ActionListener{
  6.  
  7. private char[] password={'l','o','l','1','2','3'};
  8. private JPasswordField passwordField=null;
  9.  
  10.  
  11. public static void main(String[] args){
  12. JPasswordField pass = new JPasswordField("", 0);
  13. rawr x = new rawr(pass);
  14. pass.setEchoChar('*');
  15. JButton but = new JButton("Check Pass");
  16. JFrame f = new JFrame("Password Example");
  17. f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
  18. f.setLayout(new GridLayout(1, 2));
  19. but.addActionListener(x);
  20. f.add(pass);
  21. f.add(but);
  22. f.pack();
  23. f.setVisible(true);
  24.  
  25. }
  26.  
  27. public rawr(JPasswordField pf){
  28. passwordField=pf;
  29. }
  30.  
  31. private boolean checkPass(){
  32. char[] cc = passwordField.getPassword();
  33.  
  34. if(cc.length==password.length){
  35. for(int i=0;i<cc.length;i++){
  36. try{
  37. if(cc[i]==password[i]){
  38. cc[i]=' ';
  39. }else{
  40. return false;
  41. }
  42. }catch(IndexOutOfBoundsException ex){
  43. if(cc[i]!=' '){
  44. return false;
  45. }
  46. }
  47. }
  48. return true;
  49. }
  50. return false;
  51.  
  52. }
  53.  
  54. @Override
  55. public void actionPerformed(ActionEvent e) {
  56.  
  57. if(checkPass()){
  58.  
  59. }
  60.  
  61. if(checkPass()){
  62. JOptionPane.showMessageDialog(null, "Password is Correct!");
  63. }else{
  64. JOptionPane.showMessageDialog(null, "Password is Incorrect, YOU FOOL!");
  65. }
  66.  
  67. }
  68.  
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement