• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

char to String

 
Tualha Khan
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
Hey!!
I wanted to ask a simple question. I am implementing a keylistener on a
textfield. As soon as a user types a character, it's clone is printed on
another textfield. For this I am using getKeyChar() method on the KeyEvent
object. It returns a char type on every key stroke. Now, how do I change this
char data type to a string type and show in the latter textfield. So the
actual thing to do is :
I type on TextField1 : abcdef
The System shows on TextField2 : abcdef
Finally, Swing's JPasswordField also has a method getPassword() to return a
char based (value or array). How do I convert it to a string and store in the
database or in TextField or in a physical file.
So Please help!!!
Bye,
Tualha Khan
 
sachin patel
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well you can convert the char arrray returned by getPassword like this

String s = new String(password.getPassword());
where password is JPasswordField...
this works because String class has constructor that takes char array as argument...
hope that helps

------------------
Sachin,
****************************************************
Learn from others mistakes. Life is too short to make all yourself.
****************************************************
 
Randall Twede
Ranch Hand
Posts: 4489
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for your first question you can do this:
Character c = new Character(e.getKeyChar());
String s = c.toString();
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic