my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes String-less JTextField wanted Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "String-less JTextField wanted" Watch "String-less JTextField wanted" New topic

String-less JTextField wanted

Chris Kimball
Ranch Hand

Joined: Apr 23, 2012
Posts: 32

I'm building an application that displays text from character arrays. An objective is not to leave compromising Strings throughout the system memory. I haven't found a way to eliminate Strings from the JTextField / swing.text.Document classes. In looking at the source code, such as I can, the use of Strings goes very deep.

Does anyone have a JTextField-like class that has only visible and clearable character arrays for displayed data? Must have full Unicode/BIDI support, too.

Regrettably, JPasswordField handles only a single line.


Is it impossible to cleanup after displaying information in Java?


Chris Kimball
Rob Spoor

Joined: Oct 27, 2005
Posts: 19654

You already asked this question in, and it was answered there as well. You probably forgot.

How To Ask Questions How To Answer Questions
Chris Kimball
Ranch Hand

Joined: Apr 23, 2012
Posts: 32

I certainly forgot, sorry for the repetition.

However, the problem is now how to put to the JTextField, not to get from it, as the suggested getText(0, count, Segment) method works fine for getting characters from the Document / JTextField.

I have tried putting a phony String of the necessary length via insertString(0, spacer, null) followed by a getText(0, count, Segment) then filling the Segment.array with the actual, sensitive characters. Amazingly, this seems to work for low order Unicode (ASCII), but not for full Unicode text. Note that Segments are supposed to provide read-only access and that it's far from obvious that changing the Segment values should change the displayed text.

Inside AbstractDocument is a mysterious "data" class that handles the internal workings of the Document. I'm presumably working with the PlainDocument, how do I determine what this "data" class looks like? AbstractDocument has only String-based methods operating on the data class. All very complicated at my level.

Suggestions are appreciated. Being as lazy as the next guy, I was hoping this was a general enough problem that someone else might have solved it!

Chris Kimball

Chris Kimball
Ranch Hand

Joined: Apr 23, 2012
Posts: 32

Thanks to JavaRanch's suggestion to look at the Java sources and to its pointing me to the sources, I've been able to come up with a String-less JTextArea (not JTextField). In case anyone else is interested, here's what I did:

I cloned javax.swing.text.GapVector to another class KGapVector initialized by providing an external char[], rather than an int initialSize.

I cloned javax.swing.text.GapContent to another class KGapContent initialized by providing an external char[], rather than an int initialSize and using KGapVector for its text storage. The
MarkVector continues to extend the original GapVector. KGapContent has a method insertChars supplementing the usual insertString method.

The new classes are applied by creating a char[] in the app and using that to create a new KGapContent. Then the KGapContent object is used to instantiate a PlainDocument that is set into the JTextArea with a setDocument method. The JTextArea ComponentOrientation must be set BEFORE the setDocument. The app places char[] text into the JTextArea via the new insertChars method and cleans up by cleaning the app's char[] that set the KGapContent. All input from the JTextArea is via the Segment returned by the usual JTextArea.getText method.

Seems to work OK.


Chris Kimball
It is sorta covered in the JavaRanch Style Guide.
subject: String-less JTextField wanted
Similar Threads
characters can not be displaied for codes between 0 and 65,535
Validating Strings in JTextFields
Absence of NULL in string ?
finding byte[] in subset of another byte[]
validate JTextfield input