0

my password program can hide keyboard input . its invisible when i type password.

But my qus is how can show password input as asterisk (*)

can any one help me please? :p

my code is here:

public void login()
    {
        Scanner input = new Scanner(System.in);//create Scanner object

        System.out.println("\n");


        Console console = System.console();

        char[] ad_password = "admin".toCharArray();
        char[] sm_password = "salesman".toCharArray();

        char[] passwordEntered = console.readPassword("Enter Password To Access The Project ( As Admin / Sales Man ): ");

        if (Arrays.equals(ad_password, passwordEntered))
        {
            System.out.println("\n Congratulation!!! Access granted \n");
            System.out.println("\n Welcome Admin. :-) \n\n");

            Shoping_mall obj_ad_dis=new Shoping_mall();

            obj_ad_dis.ad_dis();
        }

        else if(Arrays.equals(sm_password, passwordEntered))
        {
            System.out.println("\n Congratulation!!! Access granted \n");
            System.out.println("\n Welcome Sales Man. :-) \n\n");

            Shoping_mall obj1_sm_dis=new Shoping_mall();
            obj1_sm_dis.sm_dis();
        }

        else
        {
            System.out.println("\n Error: Your Password Doesn't Meet. Access Denied !!! :-( \n\n");
            System.out.println("\n Enter Correct Password.\n\n");

            Shoping_mall obj_login=new Shoping_mall();//creat object for calling Admin_works() method
            obj_login.login();
        }
    }
Smit
  • 4,685
  • 1
  • 24
  • 28
Sojib
  • 1
  • 1
  • 2
  • 2

1 Answers1

0

You can use JPasswordField to do that; Below is an example:

    private JPasswordField password;
    private String typedPassword;
    private final String defaultPassword = "yourDesiredPassword";

    public void createPasswordField(){

    password = new JPasswordField(30);
    password.setBounds(280, 240, 90, 20);
    password.setEchoChar('*');
    password.setBackground(Color.white);
    password.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            password = (JPasswordField) e.getSource();
            char [] tempPass = password.getPassword();
            typedPassword = new String(tempPass);

            if (!typedPassword.equals(defaultPassword)){

                JOptionPane.showMessageDialog(null,
            "Your password is not correct",
            "Stack Over Flow example",
            JOptionPane.ERROR_MESSAGE);
            }
        }
    });
}
Survivor
  • 674
  • 1
  • 7
  • 13
  • can you please give example with my code? or change my for code it? – Sojib Mar 22 '13 at 17:23
  • Sure, just give me a couple of minutes. – Survivor Mar 22 '13 at 17:29
  • @Sojib, Why don't you read the JPasswordField API where you will find a link on `How to Use Text Fields`, which will contain working examples on this as well as other Swing components. Don't expect people to spoon feed you answers. Do some reading and learning on your own. – camickr Mar 22 '13 at 18:03
  • @Sojib, I'm sorry but what you want is not suitable for Java; I mean you can do that in C much easier and in Java you can do this kind of program much easier by creating a GUI ( Graphical User Interface ); make a JFrame and JPanel and on to that JPanel add a JPasswordField and you can use what I posted above for the JPasswordField. If you still wanna do this in your way, take a look here: http://stackoverflow.com/questions/10819469/hide-input-on-command-line – Survivor Mar 22 '13 at 18:42