javaswingjpasswordfield

How do you reveal the text of a JPasswordField


I'm making a Authentication app in java and i want to add a JRadioButton, where if it is selected it will allow you to see the text in the JPasswordField, i was trying to figure it out and i was just wondering is there any way to do it?


Solution

  • From the Java-Doc of the setEchoChar() method:

    Setting a value of 0 indicates that you wish to see the text as it is typed, similar to the behavior of a standard JTextField.

    So just use:

    second.setEchoChar((char)0);
    

    to set and to reset:

    second.setEchoChar('\u2022');
    

    The default EchoChar is determined by the Look & Feel. To be always in harmony with the theme it's the best to save the default EchoChar before changing it the first time:

    char originalEchoChar = second.getEchoChar();