How to show characters in JPassword

getPassword () gives me an array of characters, but I want a password similar to this ******to be displayed as MyPaSwOrd, how can I do this?

Guys, I don’t want to extract the password, I just want to write “Enter your password” inside JPasswordField (), and when the user clicks on it, it will disappear, and then he will type his own password, which is so * * * * * *

+3
source share
3 answers

two ways:

  • use the setEchoChar method (see here ) and set the value to 0 (as JavaDoc explains)
  • use generic JTextField

EDIT:

, " PW" , , - :

public class JPassword {

    public static void main(String[] args) {

        JFrame frame = new JFrame();

        final JPasswordField field = new JPasswordField();

        field.addFocusListener(new FocusListener() {

            @Override
            public void focusLost(FocusEvent arg0) {

                char c = 0;
                field.setEchoChar(c);
                field.setText("Enter PW");

            }

            @Override
            public void focusGained(FocusEvent arg0) {

                char c = 1;
                field.setEchoChar(c);
                            field.setText("");
            }
        });

        char c = 0;
        field.setText("Enter PW");
        field.setEchoChar(c);

        frame.setLayout(new FlowLayout());
        frame.getContentPane().add(new JButton("test"));
        frame.getContentPane().add(field);
        frame.pack();
        frame.setVisible(true);
    }
}

. , . , focuseGained. . .

+1

:

String password = new String(passwordField.getText());

, Strings // .

, char [].

EDIT:

JField " " text listener. , JTextField JPassword

+1

... , Field , .

Field .

and finally make a text box to copy the text entered in passwordField.

0
source

All Articles