Search code examples
javajpasswordfield

How to set value to a JPassword field?


I need to populate JPassword field. However it looks like there is no method setPassword (char [])

The only method for this is to setText . However I will need to feed a String

Since getText is deprecated , so I presume setText will also be deprecated.

Here is an extract from an answer on stackoverflow

When calling getText you get a String (immutable object) that may not be changed (except reflection) and so the password stays in the memory until garbage collected

So does the same thing happen when I setText . I feed a String and it stays in memory. Why isn't setText deprecated? Why isn't there a setPassword (char []) method ?


Solution

  • Why isn't there a setPassword (char []) method ?

    Whether you insert a String or a char array in the end it's the same.

    ... so I presume setText will also be deprecated.

    No it won't. Usually the user will type in a password. And if you store your passwords somewhere they need to be decrypted and inserted in this field before you can send them.