File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes I enter a password into a JPasswordField, but nothing happens?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "I enter a password into a JPasswordField, but nothing happens??" Watch "I enter a password into a JPasswordField, but nothing happens??" New topic
Author

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

Fernando Sanz
Ranch Hand

Joined: Jun 27, 2003
Posts: 101
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

Joined: Jun 27, 2003
Posts: 101
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

Joined: Oct 18, 2000
Posts: 4121

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".


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Jon Dornback
Ranch Hand

Joined: Apr 24, 2002
Posts: 137
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.


use the [CODE] tags - it makes it much easier for people to help you.
Fernando Sanz
Ranch Hand

Joined: Jun 27, 2003
Posts: 101
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.
 
 
subject: I enter a password into a JPasswordField, but nothing happens??
 
Similar Threads
Swing Program with Buttons
How to draw straight line instead of curved line??
what layouts to use
divide by 0 exception
java.net.SocketException: Error