This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Want JTextField outputting a char[] instead of String. (Similar to JPasswordField) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Want JTextField outputting a char[] instead of String. (Similar to JPasswordField)" Watch "Want JTextField outputting a char[] instead of String. (Similar to JPasswordField)" New topic
Author

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

Chris Kimball
Ranch Hand

Joined: Apr 23, 2012
Posts: 32

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

Joined: Jun 09, 2003
Posts: 4632
read the methods of the String class, what you seek is there.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

And welcome to the Ranch, Chris!
Chris Kimball
Ranch Hand

Joined: Apr 23, 2012
Posts: 32

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

Joined: May 03, 2008
Posts: 4523
    
    5

JPasswordField extends JTextField, so maybe you could write your own PasswordArea by extending JTextArea along the same lines.


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Chris Kimball
Ranch Hand

Joined: Apr 23, 2012
Posts: 32

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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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

Joined: Apr 23, 2012
Posts: 32

Thanks for your assistance! Also, I didn't know I had the source code.

Chris
 
 
subject: Want JTextField outputting a char[] instead of String. (Similar to JPasswordField)
 
Similar Threads
jPasswordField is not working please help..
how to get a string return from JPasswordField?
Password verification program
char to String
how to hidden text ?