• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Want JTextField outputting a char[] instead of String. (Similar to JPasswordField)

 
Chris Kimball
Ranch Hand
Posts: 35
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to be able to enter multi-line text into an application, yet not have the text returned as a String. Instead I'd like it to be returned as a clearable char[]. JPasswordField allows a char[] output, but, alas, is for single line only.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
read the methods of the String class, what you seek is there.
 
Paul Clapham
Sheriff
Posts: 21117
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch, Chris!
 
Chris Kimball
Ranch Hand
Posts: 35
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem isn't in converting a String to a char[]. It's having an unclearable, immutable String representation of the text in memory. That's why the JPasswordField class has a char[] output and the String outputs are deprecated.

To demonstrate String x = "Secret" isn't cleared by a subsequent x = "" statement. The String content "Secret" is no longer accessible, but it's somewhere on the computer. With a char[], the data can be overwritten and the original contents are lost and gone forever, as intended.

Thanks,

Chris
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JPasswordField extends JTextField, so maybe you could write your own PasswordArea by extending JTextArea along the same lines.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just use a JPasswordField. From the Javadoc comment of its setEchoChar method:
Setting a value of 0 indicates that you wish to see the text as it is typed, similar to the behavior of a standard JTextField.
 
Chris Kimball
Ranch Hand
Posts: 35
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The suggestion of extending JTextArea seemed pretty complicated.

JTextComponent is a predecessor of JTextField and offers a getDocument() method. If I retrieve the Document, then the Document getText(int, int, Segment) is available. In Segment a char[] of text is a field. Presumably I could copy the text as a char[] from that field and put trash back into the field's char[] afterwards.

The javax.swing.text.Segment docs says "It should be treated as immutable even though the array is directly accessible. This gives fast access to fragments of text without the overhead of copying around characters. This is effectively an unprotected String. "

Was this what was suggested and does this seem a reasonable approach?

Thanks,

Chris
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you take a look at the source code of JPasswordField (available in the src.zip file inside your JDK installation folder), you will see that getPassword() is implemented like this:
As you see, that indeed uses a Segment. The BadLocationException shouldn't occur unless someone is not respecting the Swing threading rules (from Concurrency in Swing).

FYI, getText() is inherited from JTextComponent and is implemented like this:
 
Chris Kimball
Ranch Hand
Posts: 35
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your assistance! Also, I didn't know I had the source code.

Chris
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic