Displaying the password in a JPasswordField rather

2019-02-25 04:27发布

How do I display the text of a JPasswordField rather than set 0 as an echo char?

Java Docs says:

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.

The following results in an Incompatible types error

outField.setEchoChar(0);

wheras

outField.setEchoChar('0');

simply sets 0 as the echo char (displays 0s for every digit).

The reason for using JPasswordField over JTextField is that I want to change the echo char and hide the password with another method.

Thanks!

5条回答
Rolldiameter
2楼-- · 2019-02-25 04:46

When you do this:

outField.setEchoChar(0);

you're trying to pass an int into the method, and this isn't allowed since it expects a char type. Instead try

outField.setEchoChar((char)0);
查看更多
混吃等死
3楼-- · 2019-02-25 04:57

try outField.setEchoChar('\0');

查看更多
小情绪 Triste *
4楼-- · 2019-02-25 05:03

Use the char with value 0, not the number 0, or the char '0':

    outField.setEchoChar((char)0);
查看更多
聊天终结者
5楼-- · 2019-02-25 05:09

Do this:

outField.setEchoChar((char)0);

If you do this:

outField.setEchoChar(0);

then 0 is an integer, not a char, and the method requires a char.

If you do this:

outField.setEchoChar('0');

then '0' is equivalent to (char)48.

查看更多
男人必须洒脱
6楼-- · 2019-02-25 05:09

Try it out like this......

outField.setEchoChar((char)0);
查看更多
登录 后发表回答