PasswordField
A single line of editable text the JPasswordField shows as masking characters rather than as what was typed. It holds the characters value declares and reports every edit as the field's whole contents.
The value is a CharArray of raw characters rather than a String, so value, onValueChange and the comparison this wrapper makes against the field's characters need no extra, unzeroable String copy of the password. Committing an edit still makes one: JPasswordField.setText takes a String, so the password is materialized as one on its way into the field. That copy, the characters this wrapper goes on mirroring to settle a change away from value, and any copy Swing itself retains are all outside what a caller can zero.
This field is strictly controlled: characters the field settles on that onValueChange does not answer with a matching value are settled back onto the declared value on the very next pass, so the field never ends up holding characters the caller has not adopted.
Array ownership: the array delivered to onValueChange is a fresh copy owned by the receiver, free to retain or zero. The value array stays owned by the caller, read only through the next recomposition; zeroing it once it stops being the current value is the caller's responsibility.
For incremental editing over a shared Document, undo/redo, or observing the text as a flow, drive the field with PasswordField and a DocumentState from rememberDocumentState.
Parameters
the current text value, as raw characters
callback invoked with the field's new characters when the field is edited; applying value is not itself reported
the SwingModifier applied to the underlying component
the masking character; null, the default, applies the look-and-feel's installed echo character, and the NUL character (U+0000) shows the text in clear text
the preferred width in columns; 0 by default, taking the width from the characters the field holds
whether the user can type into the field; true by default
See also
the DocumentState-driven overload for large or complex editors
A PasswordField driven by a raw DocumentListener instead of an onValueChange lambda. The documentListener is attached to the field's document as-is and removed on the same instance; pass a stable instance (e.g. remember {}) to avoid churn. Being attached as-is, it observes every change to that document, including the one that applies value.
This field is strictly controlled: characters the field settles on that are not followed by value moving to match are settled back onto the declared value on the very next pass, so the field never ends up holding characters the caller has not adopted.
The value array stays owned by the caller, read only through the next recomposition; zeroing it once it stops being the current value is the caller's responsibility.
For incremental editing over a shared Document, undo/redo, or observing the text as a flow, drive the field with PasswordField and a DocumentState from rememberDocumentState.
Parameters
the current text value, as raw characters
the listener notified of document edits
the SwingModifier applied to the underlying component
the masking character; null, the default, applies the look-and-feel's installed echo character, and the NUL character (U+0000) shows the text in clear text
the preferred width in columns; 0 by default, taking the width from the characters the field holds
whether the user can type into the field; true by default
See also
the DocumentState-driven overload for large or complex editors
A PasswordField driven by a DocumentState. The field renders the state's own document, so masked text typed into the field and edits made through the state are the same content, and the caret is kept two-way with DocumentState.selection. The state is the single source of truth.
The state models plain text: DocumentState.text materializes the password as an ordinary String, which the caller cannot zero and which persists on the heap until garbage-collected.
Parameters
the hoistable text state the field renders and drives.
the SwingModifier applied to the underlying component
the masking character; null, the default, applies the look-and-feel's installed echo character, and the NUL character (U+0000) shows the text in clear text
the preferred width in columns; 0 by default, taking the width from the characters the field holds
whether the user can type into the field; true by default