How to use password field as text field in Java

This post will show you how to use password field as text field in Java like this:

Using password field as text field

You must have thoughts “Why do I do this?”, “I can use text field”,…. The answer is that it is convenient. You can see it according above figure. Instead of using extra text fields, labels,… to show errors, why don’t we use itself, password field, to show errors? That helps to save space and keep UI simple.

OK, you see the purpose of this? Let’s continue.

When you type a password, you can’t see it because password field replaces your password by an another character, called by “echo char”. You can get or set the echo char by using getEchoChar() and setEchoChar() method.

By default, echo char is a black dot and you can change it to any other character you want. For example:

setEchoChar('*') //change the default echo char to '*' character

Now, you understand how it works but how to hide the echo char and show what you type. It’s very simple. You just need set the echo char to the blank character.


The problem is solved.

However, there is still an other problem. That is how to get the original password field back that means you don’t see what you type. I guess you can get the solution 😀 that is just saving the default echo char and reusing later.

JPasswordField pw = new JPasswordField(10);
char defaultEchoChar = pw.getEchoChar(); //back up the default echo char
pw.setEchoChar(''); //delete echo char - show what you type
pw.setEchoChar(defaultEchoChar); //restore the default echo char


About ninjapro

It is better to feel by yourself about me

One response to “How to use password field as text field in Java”

  1. Martin Digman says :

    I dugg some of you post as I cogitated they were very useful extremely helpful

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: