| public class javax.swing.text DefaultCaret
|
Java SE 6 |
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
getSelectionHighlighter
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 JavaBeansTM
has been added to the java.beans package.
Please see java.beans.XMLEncoder.
| version | 1.147 04/14/06 |
| See also | javax.swing.text.Caret |
| Fields | |||||
|---|---|---|---|---|---|
| final public static int | UPDATE_WHEN_ON_EDT Details
Indicates that the caret position is to be updated only when
document changes are performed on the Event Dispatching Thread.
| ||||
| final public static int | NEVER_UPDATE Details
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. In that case the caret
position is adjusted to the end of the document.
| ||||
| final public static int | ALWAYS_UPDATE Details
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.
| ||||
| protected EventListenerList | listenerList The event listener list. | ||||
| protected ChangeEvent | changeEvent The change event for the model. Only one ChangeEvent is needed per model instance since the event's only (read-only) state is the source property. The source of events generated here is always "this". | ||||
| Constructors | |
|---|---|
| public | DefaultCaret() Constructs a default caret. |
| Methods | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| public void | addChangeListener(ChangeListener l) Details
Adds a listener to track whenever the caret position has
been changed.
| ||||||||||
| protected void | adjustVisibility(Rectangle nloc) Details
Scrolls the associated view (if necessary) to make
the caret visible. Since how this should be done
is somewhat of a policy, this method can be
reimplemented to change the behavior. By default
the scrollRectToVisible method is called on the
associated component.
| ||||||||||
| protected void | damage(Rectangle r) Details
Damages the area surrounding the caret to cause
it to be repainted in a new location. If paint()
is reimplemented, this method should also be
reimplemented. This method should update the
caret bounds (x, y, width, and height).
| ||||||||||
| public void | deinstall(JTextComponent c) Details
Called when the UI is being removed from the
interface of a JTextComponent. This is used to
unregister any listeners that were attached.
| ||||||||||
| public boolean | equals(Object obj) Details
Compares this object to the specified object.
The superclass behavior of comparing rectangles
is not desired, so this is changed to the Object
behavior.
| ||||||||||
| protected void | fireStateChanged() Details
Notifies all listeners that have registered interest for
notification on this event type. The event instance
is lazily created using the parameters passed into
the fire method. The listener list is processed last to first.
| ||||||||||
| public void | focusGained(FocusEvent e) Details
Called when the component containing the caret gains
focus. This is implemented to set the caret to visible
if the component is editable.
| ||||||||||
| public void | focusLost(FocusEvent e) Details
Called when the component containing the caret loses
focus. This is implemented to set the caret to visibility
to false.
| ||||||||||
| public void | install(JTextComponent c) Details
Called when the UI is being installed into the
interface of a JTextComponent. This can be used
to gain access to the model that is being navigated
by the implementation of this interface. Sets the dot
and mark to 0, and establishes document, property change,
focus, mouse, and mouse motion listeners.
| ||||||||||
| public void | mouseClicked(MouseEvent e) Details
Called when the mouse is clicked. If the click was generated
from button1, a double click selects a word,
and a triple click the current line.
| ||||||||||
| public void | mouseDragged(MouseEvent e) Details
Moves the caret position
according to the mouse pointer's current
location. This effectively extends the
selection. By default, this is only done
for mouse button 1.
| ||||||||||
| public void | mouseEntered(MouseEvent e) Details
Called when the mouse enters a region.
| ||||||||||
| public void | mouseExited(MouseEvent e) Details
Called when the mouse exits a region.
| ||||||||||
| public void | mouseMoved(MouseEvent e) Details
Called when the mouse is moved.
| ||||||||||
| public void | mousePressed(MouseEvent e) Details
If button 1 is pressed, this is implemented to
request focus on the associated text component,
and to set the caret position. If the shift key is held down,
the caret will be moved, potentially resulting in a selection,
otherwise the
caret position will be set to the new location. If the component
is not enabled, there will be no request for focus.
| ||||||||||
| public void | mouseReleased(MouseEvent e) Details
Called when the mouse is released.
| ||||||||||
| protected void | moveCaret(MouseEvent e) Details
Tries to move the position of the caret from
the coordinates of a mouse event, using viewToModel().
This will cause a selection if the dot and mark
are different.
| ||||||||||
| public void | moveDot(int dot) Details
Moves the caret position to the specified position,
with a forward bias.
| ||||||||||
| public void | moveDot(int dot, Position.Bias dotBias) Details
Moves the caret position to the specified position, with the
specified bias.
| ||||||||||
| public void | paint(Graphics g) Details
Renders the caret as a vertical line. If this is reimplemented
the damage method should also be reimplemented as it assumes the
shape of the caret is a vertical line. Sets the caret color to
the value returned by getCaretColor().
If there are multiple text directions present in the associated document, a flag indicating the caret bias will be rendered. This will occur only if the associated document is a subclass of AbstractDocument and there are multiple bidi levels present in the bidi element structure (i.e. the text has multiple directions associated with it).
| ||||||||||
| protected void | positionCaret(MouseEvent e) Details
Tries to set the position of the caret from
the coordinates of a mouse event, using viewToModel().
| ||||||||||
| public void | removeChangeListener(ChangeListener l) Details
Removes a listener that was tracking caret position changes.
| ||||||||||
| final protected void | repaint() Cause the caret to be painted. The repaint area is the bounding box of the caret (i.e. the caret rectangle or this). This method is thread safe, although most Swing methods are not. Please see How to Use Threads for more information. | ||||||||||
| public String | toString() | ||||||||||
| Properties | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| public boolean | isActive() Details
Determines if the caret is currently active.
This method returns whether or not the
| ||||||||||
| public void | setBlinkRate(int rate) Details
Sets the caret blink rate.
| ||||||||||
| public int | getBlinkRate() Details
Gets the caret blink rate.
| ||||||||||
| public ChangeListener[] | getChangeListeners() Details
Returns an array of all the change listeners
registered on this caret.
| ||||||||||
| final protected JTextComponent | getComponent() Details
Gets the text editor component that this caret is
is bound to.
| ||||||||||
| public void | setDot(int dot) Details
Sets the caret position and mark to the specified position,
with a forward bias. This implicitly sets the
selection range to zero.
| ||||||||||
| public void | setDot(int dot, Position.Bias dotBias) Details
Sets the caret position and mark to the specified position, with the
specified bias. This implicitly sets the selection range
to zero.
| ||||||||||
| public int | getDot() Details
Fetches the current position of the caret.
| ||||||||||
| public Position.Bias | getDotBias() Details
Returns the bias of the caret position.
| ||||||||||
| public EventListener[]<T> | getListeners(Class listenerType) Details
Returns an array of all the objects currently registered
as FooListeners
upon this caret.
FooListeners are registered using the
addFooListener method.
You can specify the ChangeListener[] cls = (ChangeListener[])(c.getListeners(ChangeListener.class));If no such listeners exist, this method returns an empty array.
| ||||||||||
| public void | setMagicCaretPosition(Point p) Details
Saves the current caret position. This is used when
caret up/down actions occur, moving between lines
that have uneven end positions.
| ||||||||||
| public Point | getMagicCaretPosition() Details
Gets the saved caret position.
| ||||||||||
| public int | getMark() Details
Fetches the current position of the mark. If there is a selection,
the dot and mark will not be the same.
| ||||||||||
| public Position.Bias | getMarkBias() Details
Returns the bias of the mark.
| ||||||||||
| protected Highlighter.HighlightPainter | getSelectionPainter() Details
Gets the painter for the Highlighter.
| ||||||||||
| public void | setSelectionVisible(boolean vis) Details
Changes the selection visibility.
| ||||||||||
| public boolean | isSelectionVisible() Details
Checks whether the current selection is visible.
| ||||||||||
| public void | setUpdatePolicy(int policy) Details
Sets the caret movement policy on the document updates. Normally
the caret updates its absolute position within the document on
insertions occurred before or at the caret position and
on removals before the caret position. 'Absolute position'
means here the position relative to the start of the document.
For example if
a character is typed within editable text component it is inserted
at the caret position and the caret moves to the next absolute
position within the document due to insertion and if
BACKSPACE is typed then caret decreases its absolute
position due to removal of a character before it. Sometimes
it may be useful to turn off the caret position updates so that
the caret stays at the same absolute position within the
document position regardless of any document updates.
The following update policies are allowed:
The default property value is
| ||||||||||
| public int | getUpdatePolicy() Details
Gets the caret movement policy on document updates.
| ||||||||||
| public void | setVisible(boolean e) Details
Sets the caret visibility, and repaints the caret.
It is important to understand the relationship between this method,
isVisible and isActive.
Calling this method with a value of true activates the
caret blinking. Setting it to false turns it completely off.
To determine whether the blinking is active, you should call
isActive. In effect, isActive is an
appropriate corresponding "getter" method for this one.
isVisible can be used to fetch the current
visibility status of the caret, meaning whether or not it is currently
painted. This status will change as the caret blinks on and off.
Here's a list showing the potential return values of both
| ||||||||||
| public boolean | isVisible() Details
Indicates whether or not the caret is currently visible. As the
caret flashes on and off the return value of this will change
between true, when the caret is painted, and false, when the
caret is not painted. isActive indicates whether
or not the caret is in a blinking state, such that it can
be visible, and isVisible indicates whether or not
the caret is actually visible.
Subclasses that wish to render a different flashing caret should override paint and only paint the caret if this method returns true.
| ||||||||||
| About DocWeb · Bundles · Export · Export All | Top 10 · Statistics · Login |
| About Sun · Contact · Privacy · Terms of Use · Trademarks | Java SE 6 · Copyright © 1994-2013 Sun Microsystems, Inc.All rights reserved. Use is subject to license terms |
![]() |
![]() |
|