This implementation expects two sources of asynchronous notification.
The timer thread fires asynchronously, and causes the caret to simply
repaint the most recent bounding box. The caret also tracks change
as the document is modified. Typically this will happen on the
event dispatch thread as a result of some mouse or keyboard event.
The caret behavior on both synchronous and asynchronous documents updates
is controlled by UpdatePolicy property. The repaint of the
new caret location will occur on the event thread in any case, as calls to
modelToView are only safe on the event thread.
The caret acts as a mouse and focus listener on the text component it has been installed in, and defines the caret semantics based upon those events. The listener methods can be reimplemented to change the semantics. By default, the first mouse button will be used to set focus and caret position. Dragging the mouse pointer with the first mouse button will sweep out a selection that is contiguous in the model. If the associated text component is editable, the caret will become visible when focus is gained, and invisible when focus is lost.
The Highlighter bound to the associated text component is used to
render the selection by default.
Selection appearance can be customized by supplying a
painter to use for the highlights. By default a painter is used that
will render a solid color as specified in the associated text component
in the SelectionColor property. This can easily be changed
by reimplementing the
getSelectionPainter
method.
A customized caret appearance can be achieved by reimplementing the paint method. If the paint method is changed, the damage method should also be reimplemented to cause a repaint for the area needed to render the caret. The caret extends the Rectangle class which is used to hold the bounding box for where the caret was last rendered. This enables the caret to repaint in a thread-safe manner when the caret moves without making a call to modelToView which is unstable between model updates and view repair (i.e. the order of delivery to DocumentListeners is not guaranteed).
The magic caret position is set to null when the caret position changes. A timer is used to determine the new location (after the caret change). When the timer fires, if the magic caret position is still null it is reset to the current caret position. Any actions that change the caret position and want the magic caret position to remain the same, must remember the magic caret position, change the cursor, and then set the magic caret position to its original value. This has the benefit that only actions that want the magic caret position to persist (such as open/down) need to know about it.
Warning
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is
appropriate for short term storage or RMI between applications running
the same version of Swing. As of 1.4, support for long term storage
of all JavaBeans
has been added to the java.beans package.
Please see java..
Caret
| Modifier and Type | Class and Description |
|---|---|
| private class | |
| pack-priv class | |
| pack-priv class |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv boolean | |
| public static final int | ALWAYS_UPDATE
Indicates that the caret position is to be always updated accordingly to the document changes regardless whether the document updates are performed on the Event Dispatching Thread or not. |
| private float | |
| private int | caretWidth
The width of the caret in pixels. |
| protected transient ChangeEvent | changeEvent
The change event for the model. |
| pack-priv JTextComponent | |
| pack-priv int | |
| pack-priv transient Position. | |
| pack-priv boolean | |
| private transient NavigationFilter. | |
| private transient int[] | |
| private transient int[] | |
| pack-priv Timer | |
| private boolean | forceCaretPositionChange
If this is true, the location of the dot is updated regardless of the current location. |
| pack-priv transient DefaultCaret. | |
| private boolean | |
| protected EventListenerList | listenerList
The event listener list. |
| pack-priv Point | |
| pack-priv int | |
| pack-priv transient Position. | |
| pack-priv boolean | |
| public static final int | NEVER_UPDATE
Indicates that the caret should remain at the same absolute position in the document regardless of any document updates, except when the document length becomes less than the current caret position due to removal. |
| private boolean | ownsSelection
This is used to indicate if the caret currently owns the selection. |
| private int | |
| private transient MouseEvent | selectedWordEvent
holds last MouseEvent which caused the word selection |
| pack-priv Object | |
| pack-priv boolean | |
| private static transient Action | |
| private static transient Action | |
| private transient boolean | shouldHandleRelease
Whether or not mouseReleased should adjust the caret and focus. |
| public static final int | UPDATE_WHEN_ON_EDT
Indicates that the caret position is to be updated only when document changes are performed on the Event Dispatching Thread. |
| pack-priv int | |
| pack-priv boolean |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| private boolean | |
| public void | addChangeListener(ChangeListener
the listener l)Implements javax. Adds a listener to track whenever the caret position has been changed. |
| private void | |
| pack-priv void | |
| private void | adjustFocus(boolean
if true indicates requestFocusInWindow should be used inWindow)Adjusts the focus, if necessary. |
| protected void | adjustVisibility(Rectangle
the new position to scroll to nloc)Scrolls the associated view (if necessary) to make the caret visible. |
| pack-priv void | |
| protected synchronized void | |
| public void | deinstall(JTextComponent
the component c)Implements javax. Called when the UI is being removed from the interface of a JTextComponent. |
| private void | ensureValidPosition()
This is invoked after the document changes to verify the current dot/mark is valid. |
| public boolean | Returns: true if the objects are equal;
false otherwisethe object to compare this font with obj)Overrides java. Compares this object to the specified object. |
| protected void | fireStateChanged()
Notifies all listeners that have registered interest for notification on this event type. |
| public void | focusGained(FocusEvent
the focus event e)Implements java. Called when the component containing the caret gains focus. |
| public void | focusLost(FocusEvent
the focus event e)Implements java. Called when the component containing the caret loses focus. |
| public int | Returns: the delay in milliseconds. If this is zero the caret will not blink.Implements javax. Gets the caret blink rate. |
| pack-priv int | |
| public ChangeListener[] | Returns: all of this caret'sChangeListeners
or an empty
array if no change listeners are currently registeredReturns an array of all the change listeners registered on this caret. |
| private ClipboardOwner | |
| protected final JTextComponent | |
| public int | Returns: the position >= 0Implements javax. Fetches the current position of the caret. |
| public Position. | |
| private NavigationFilter. | |
| public < the listener type T extends EventListener> T[] | Returns: an array of all objects registered asFooListeners on this component,
or an empty array if no such
listeners have been addedthe type of listeners requested listenerType)Returns an array of all the objects currently registered
as |
| public Point | Returns: the position see #setMagicCaretPositionImplements javax. Gets the saved caret position. |
| public int | Returns: the position >= 0Implements javax. Fetches the current position of the mark. |
| public Position. | |
| protected Highlighter. | |
| private Clipboard | |
| public int | Returns: one of the following values :UPDATE_WHEN_ON_EDT,
NEVER_UPDATE, ALWAYS_UPDATEGets the caret movement policy on document updates. |
| pack-priv Position. | |
| pack-priv void | |
| pack-priv void | |
| public void | install(JTextComponent
the component c)Implements javax. Called when the UI is being installed into the interface of a JTextComponent. |
| public boolean | |
| pack-priv boolean | |
| pack-priv boolean | |
| pack-priv boolean | |
| public boolean | Returns: true if the selection is visibleImplements javax. Checks whether the current selection is visible. |
| public boolean | Returns: true if visible else falseImplements javax. Indicates whether or not the caret is currently visible. |
| public void | mouseClicked(MouseEvent
the mouse event e)Implements java. Called when the mouse is clicked. |
| public void | mouseDragged(MouseEvent
the mouse event e)Implements java. Moves the caret position according to the mouse pointer's current location. |
| public void | mouseEntered(MouseEvent
the mouse event e)Implements java. Called when the mouse enters a region. |
| public void | mouseExited(MouseEvent
the mouse event e)Implements java. Called when the mouse exits a region. |
| public void | mouseMoved(MouseEvent
the mouse event e)Implements java. Called when the mouse is moved. |
| public void | mousePressed(MouseEvent
the mouse event e)Implements java. If button 1 is pressed, this is implemented to request focus on the associated text component, and to set the caret position. |
| public void | mouseReleased(MouseEvent
the mouse event e)Implements java. Called when the mouse is released. |
| protected void | moveCaret(MouseEvent
the mouse event e)Tries to move the position of the caret from the coordinates of a mouse event, using viewToModel(). |
| public void | moveDot(int
the position >= 0 dot)Implements javax. Moves the caret position to the specified position, with a forward bias. |
| public void | |
| public void | paint(Graphics
the graphics context g)Implements javax. Renders the caret as a vertical line. |
| protected void | positionCaret(MouseEvent
the mouse event e)Tries to set the position of the caret from the coordinates of a mouse event, using viewToModel(). |
| private void | |
| public void | removeChangeListener(ChangeListener
the listener l)Implements javax. Removes a listener that was tracking caret position changes. |
| protected final synchronized void | |
| pack-priv void | repaintNewCaret()
Repaints the new caret position, with the
assumption that this is happening on the
event thread so that calling |
| private void | |
| public void | setBlinkRate(int
the rate in milliseconds, 0 to stop blinking rate)Implements javax. Sets the caret blink rate. |
| public void | setDot(int
the position >= 0 dot)Implements javax. Sets the caret position and mark to the specified position, with a forward bias. |
| public void | |
| public void | setMagicCaretPosition(Point
the position p)Implements javax. Saves the current caret position. |
| public void | setSelectionVisible(boolean
the new visibility vis)Implements javax. Changes the selection visibility. |
| public void | setUpdatePolicy(int
one of the following values : policy)UPDATE_WHEN_ON_EDT,
NEVER_UPDATE, ALWAYS_UPDATESets the caret movement policy on the document updates. |
| public void | setVisible(boolean
the visibility specifier e)Implements javax. Sets the caret visibility, and repaints the caret. |
| public String | toString()
Overrides java. Returns a |
| private void | |
| private void |