TextField

@Composable
fun TextField(value: @Nls String, onValueChange: (@Nls String) -> Unit, modifier: SwingModifier = SwingModifier, columns: Int = 0, editable: Boolean = true)

A single line of editable plain text. The JTextField shows the text value declares, and every edit reports the field's whole text through onValueChange.

This field is strictly controlled: if onValueChange does not answer with a matching value, the field is settled back onto the declared value on the very next pass. It never ends up holding text the caller has not adopted. A callback that filters a keystroke rather than adopting it leaves the caret where that keystroke would have gone.

For incremental editing over a shared Document, undo/redo, or observing the text as a flow, drive the field with the DocumentState overload and a DocumentState from rememberDocumentState.

Parameters

value

the current text value

onValueChange

callback invoked with the field's new text when the field is edited; applying value is not itself reported

modifier

the SwingModifier applied to the underlying component

columns

the preferred width in columns; 0 by default, taking the width from the text the field holds

editable

whether the user can type into the field; true by default

See also

the DocumentState-driven overload for large or complex editors


@Composable
fun TextField(value: @Nls String, documentListener: DocumentListener, modifier: SwingModifier = SwingModifier, columns: Int = 0, editable: Boolean = true)

A TextField 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. It observes every change to that document, including the one that applies value.

This field is strictly controlled: if text the field settles on is not followed by value moving to match, it is settled back onto the declared value on the very next pass. It never ends up holding text the caller has not adopted.

For incremental editing over a shared Document, undo/redo, or observing the text as a flow, drive the field with the DocumentState overload and a DocumentState from rememberDocumentState.

Parameters

value

the current text value

documentListener

the listener notified of document edits

modifier

the SwingModifier applied to the underlying component

columns

the preferred width in columns; 0 by default, taking the width from the text the field holds

editable

whether the user can type into the field; true by default

See also

the DocumentState-driven overload for large or complex editors


@Composable
fun TextField(state: DocumentState, modifier: SwingModifier = SwingModifier, columns: Int = 0, editable: Boolean = true)

A TextField driven by a DocumentState. The field renders the state's own document, so 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; there is no onValueChange.

Parameters

state

the hoistable text state the field renders and drives.

modifier

the SwingModifier applied to the underlying component

columns

the preferred width in columns; 0 by default, taking the width from the text the field holds

editable

whether the user can type into the field; true by default

See also