caret

Installs caret as the text component's caret - the object that holds the insertion point and the selection, paints them, and answers the focus and mouse gestures that move them. Removing the declaration puts back the caret the component carried before.

Three things follow from the way Swing installs a caret:

  • installing one places it at offset 0 with nothing selected, so hand this a caret that outlives a recomposition - one kept in a remember, or an object - and it is installed once, leaving the selection the user makes afterwards in place;

  • a caret handed in here does not blink until it is given a rate, which caretBlinkRate declares: a look and feel gives its blink rate to the caret it created itself and to no other;

  • a later look and feel leaves this caret in place, for the same reason - a look and feel replaces only a caret it created.

Whether the selection stays painted while the component is unfocused is the caret's own answer, recomputed on every focus change, so a caret of your own is where that answer is given:

val caret = remember {
object : DefaultCaret() {
override fun focusLost(event: FocusEvent) {
super.focusLost(event)
isSelectionVisible = true
}
}
}
TextField(state, modifier = SwingModifier.caret(caret).caretBlinkRate(500))

Where the caret may go is declared by navigationFilter, and what it selects by DocumentState.selection, the one owner of a component's selection; key bindings are declared by onKeyStroke. A part of a text component this library ships no builder for - a highlighter, a drop mode - is reachable through a SwingModifier.NodeElement of your own; see docs/CUSTOM-COMPONENTS.md.

Return

this modifier with caret declared on it.

Parameters

caret

the caret the component navigates and selects with.

See also