This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic