0

Can I make this password field hidden in Java Swing JTextField?

I want to set the password always to welcome as shown in below code but in hidden field. Is there an option by which I can achieve this in Java Swing?

jTextField_password.setBounds(new Rectangle(245, 25, 100, 25));
jTextField_password.setText("welcome");
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Dojo_user
  • 281
  • 4
  • 9
  • 28
  • 2
    Why not use JPasswordField ? – Hirak May 28 '14 at 09:31
  • `..setBounds(new Rectangle(245, 25, 100, 25));` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). In this case, the size of a text field can also be changed by changing the number of columns displayed, or changing the font. – Andrew Thompson May 28 '14 at 09:44

2 Answers2

3

You should use JPasswordField. it's a subclass of JTextField that make text invisible.

Mifmif
  • 3,132
  • 18
  • 23
3

You can use JPasswordField. It's perfect for your case. It's also much safer than a normal Textfield, because the String will be stored as a char array, which isn't in the normal String pool.

Sean Mickey
  • 7,618
  • 2
  • 32
  • 58
Mansouritta
  • 166
  • 1
  • 1
  • 10