Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I enter a password into a JPasswordField, but nothing happens??

 
Fernando Sanz
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,
I have to include a password for the program I'm writing. So I decided to include a JPasswordField within the Main Class. Don't worry about the password itself (as in its security) or the user names, becuase it's part of a GUI, so it's all "faked".
I'm posting my code for this class, I hope somebody can point out what I'm doing wrong, or how I should do it. Thanks!
 
Fernando Sanz
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I've changed a line to this:

and now it works. Still, I don't understand why it didn't work before, anyone?
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It didn't work before because getPassword() returns an array of characters (char[])... you can create a String from an array of characters with new String( char[] ), but if you say char[].toString(), you will get back a String representation of the array reference... something that looks like "[C@1774b9b".
 
Jon Dornback
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
most of the java APIs have started using array of chars instead of String objects whenever security is concerned. If you look at the javax.crypto and java.security packages, many of those methods are the same. The reason (as I remember/interpret it) is that Strings are interned and more prone to security problems than an array of chars that will be garbage collected.
If you were actually using the password, the return type of char[] can be sent directly as the parameter to unlock a private key and other methods, no String needed.
 
Fernando Sanz
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nate for helping me understand it, I appreciate it, cause even though it was working, I was still thinking about it
Jon: ok, so no more Strings anymore I guess that if the program was going to use passwords for real, I'd look more into it, but I was just trying to get a password in the GUI. Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic