Results 1 to 6 of 6

Thread: How to Use Password Fields in Java?

  1. #1
    Join Date
    Aug 2006
    Posts
    173

    How to Use Password Fields in Java?

    I have completed the basic programs of the Java. I had started doing Java from last month. But still I don't know how to use the Password Fields in Java..?? I have tried to do lot of different things, but i am not getting the desired results. So I thought that posting to you guys might help me. Please tell me the solution for my problem. Any coding related to the topic would be grateful. Hoping that I will get help sooner.!!
    "Let us remember that our interest is in concord, not conflict, and that our real eminence rests in the victories of peace, not those of war." -McKinley

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: How to Use Password Fields in Java?

    Before moving to the codes, I think that you should know the basic things about the password fields. Once you are clear with the concepts, then you can go towards the coding side. Otherwise, without the concept you will not understand the coding. When the steps to use, why to use..?? will make you more confuse will writing the code. So, here is the information. The JPasswordField class, a subclass of JTextField, provides specialized text fields for password entry. A password field does not show the characters that the user types. Instead, the field displays a character different from the one typed, such as an asterisk '*'.

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to Use Password Fields in Java?

    You should also know that a password field stores its value as an array of characters, rather than as a string. In Java, login screens and login dialog boxes use password masking. Password masking is a technique to either hide the password as it is being typed or displaying a character (such as an asterisk '*') instead of the characters that users type. You can observe such things when you enter the password into your computer. The only thing you can see are the asterisk '*. In this way your password is protected.

  4. #4
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to Use Password Fields in Java?

    If you wish to provide a graphical login dialog box for your application, you can use the AWT's TextField class, which is a text component that allows the editing of a single line of text. To mask the password field, use the setEchoChar method. You can use the following example for hiding the characters from the other users :
    Code:
    TextField password = new TextField(6);
    password.setEchoChar('*');
    In the above example, the number 6 specifies the width of the text field based on the average character width for the font in use.

  5. #5
    Join Date
    Nov 2008
    Posts
    996

    Re: How to Use Password Fields in Java?

    I have provided you with a coding that can help you for using the password fields in Java :
    Code:
    public class PasswordField {
    
       public static String readPassword (String prompt) {
          EraserThread t = new EraserThread(prompt);
          Thread mask = new Thread(t);
          mask.start();
    
          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
          String password = "";
    
          try {
             password = in.readLine();
          } catch (IOException ioe) {
            ioe.printStackTrace();
          }
          t.stopMasking();
          return password;
       }
    }

  6. #6
    Join Date
    Mar 2008
    Posts
    672

    Re: How to Use Password Fields in Java?

    You can also give the password after implementing of the action listener's actionPerformed method, just like I have done in the following example :
    Code:
    public void actionPerformed(ActionEvent ae) {
        String cmd = ae.getActionCommand();
    
        if (OK.equals(cmd)) { 
            char[] input = passwordField.getPassword();
            if (isPasswordCorrect(input)) {
                JOptionPane.showMessageDialog(controllingFrame,
                    "Thank You!!");
            } else {
                JOptionPane.showMessageDialog(controllingFrame,
                    "Access Denied. Try again.",
                    "Error Message",
                    JOptionPane.ERROR_MESSAGE);
            }
    
            Arrays.fill(input, '0');
    
            passwordField.selectAll();
            resetFocus();
        } else ...
    }

Similar Threads

  1. How to validate user's password with PasswordEncryptor in java?
    By kamina23 in forum Software Development
    Replies: 6
    Last Post: 11-08-2010, 05:07 PM
  2. How to encrypt password before storing in java
    By Xylina in forum Software Development
    Replies: 7
    Last Post: 16-03-2010, 09:02 AM
  3. Replies: 4
    Last Post: 09-02-2010, 07:04 AM
  4. Issue while setting user password in ADAM using JAVA
    By VinodMaladkar in forum Active Directory
    Replies: 4
    Last Post: 08-07-2009, 05:25 PM
  5. Getting Username and Password with New java.io.Console Class
    By Cody in forum Software Development
    Replies: 1
    Last Post: 30-12-2008, 06:29 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,243,813.81176 seconds with 17 queries