caretListener

Runs onCaretUpdate whenever the caret moves or the selection changes. Requires a JTextComponent target.

onCaretUpdate is read when the event fires, so writing a fresh lambda on every recomposition registers nothing again.

Return

this chain with the caret callback declared on it.

Parameters

onCaretUpdate

receives the event, whose dot is where the caret sits and whose mark is the other end of the selection; the two are equal where nothing is selected.

See also


Attaches a CaretListener (addCaretListener/removeCaretListener). Requires a JTextComponent target (JTextField, JTextArea, ...). Each event carries the caret offset and the selection anchor, so one listener observes both the caret position and the selected range.

The listener observes the component's caret rather than a document, so it keeps reporting after the component's document is replaced - as does documentListener, which follows the document the component swaps in.

Return

this chain with the caret listener declared on it.

Parameters

listener

attached by identity, so a remembered instance stays put while a fresh one on every pass is detached and re-added.

See also