my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Looking for a Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply locked New topic

Looking for a "secure" JTextField.

Chris Kimball
Ranch Hand

Joined: Apr 23, 2012
Posts: 32

JTextField outputs only String objects, which are immutable. I'm looking for a look-alike class that allows a char[] output for security, just as JPasswordField does for single lines of text.

I can convert to and from Strings, but I don't want the text to exist as a String anywhere, for security purposes.


Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
JPasswordField uses document.getText(), which returns a String.

I'd say you'd have to roll-your-own textfield for what you want.

one way might be to create a JTextField that is limited to 1 character,
it has no borders/margins etc, so it's just a little white square. you add
say 15 of these to a GridLayout panel, then you set the panel's border
to that of a normal textfield, so it should look like one textfield.

focuslisteners needed to check
1) if prior textfield empty, move the focus there
2) if textfield already has 1 character, move focus to next textfield

hard part will be handling e.g password is java, and you type jva,
you can place the caret between jv and add a.
Ranganathan Kaliyur Mannar

Joined: Oct 16, 2003
Posts: 1075

This seems to be a duplicate of your earlier post.
Many people answered and finally a simple suggestion was provided by Rob Spoor. Did you use it? Did you even read the replies to that post?

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13869

Let's continue in your other post.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
I agree. Here's the link:
subject: Looking for a "secure" JTextField.
Similar Threads
My First GUI.....maybe
how to increase the size of the text field and change its position ?
How to set JTextField with the name in a String
Text boxes over an image
automatic scrolling of JTextField