Carets

Carets represent the location where new text is inserted.

The Caret Interface

The Caret interface provides a number of useful features for dealing with text insertion and selection.

Properties

The Caret interface defines the properties shown in Table 21-1. The blinkRate property specifies the number of milliseconds between Caret blinks. A value of 0 indicates that the Caret shouldn’t blink at all.

Table 21-1. Caret properties

Property

Data type

get

is

set

default Value

blinkRate

int

·

 

·

 

dot

int

·

 

·

 

magicCaretPosition

Point

·

 

·

 

mark

int

·

   

selectionVisible

boolean

 

·

·

 

visible

boolean

 

·

·

 

The dot property is the current Caret position as an offset into the Document model. The mark is the other end of the current selection. If there is no selection, the value of mark is the same as dot. The selectionVisible property designates whether the current selection (if any) should be decorated by the component’s Highlighter.

The visible property indicates whether the Caret itself should be visible. This is almost always true when the Caret’s text component is editable and has focus but may not be in other situations.

magicCaretPosition is a Point used when moving among lines with uneven end positions to ensure that the up and down arrow keys produce the desired effect. For example, consider the following text:

Line 1 is long
Line 2
Line 3 is long

If the caret was initially positioned before the o in long in line 3, you’d expect the up arrow key to move the caret to the end of line 2. A second up ...

Get Java Swing, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.