|
JavaTM 2 Platform Standard Edition |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.awt.geom.RectangularShape | +--java.awt.geom.Rectangle2D | +--java.awt.Rectangle | +--javax.swing.text.DefaultCaret
A default implementation of Caret. The caret is rendered as a vertical line in the color specified by the CaretColor property of the associated JTextComponent. It can blink at the rate specified by the BlinkRate property.
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 thread as a result of some mouse or keyboard event. Updates
can also occur from some other thread mutating the document. There
is a property AsynchronousMovement
that determines if
the caret will move on asynchronous updates. The default behavior
is to not update on asynchronous updates. If asynchronous
updates are allowed, the update thread will fire the caret position
change to listeners asynchronously. 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).
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. A future release of Swing will provide support for long term persistence.
Caret
, Serialized FormInner classes inherited from class java.awt.geom.Rectangle2D |
Rectangle2D.Double,
Rectangle2D.Float |
Field Summary | |
protected ChangeEvent |
changeEvent
The change event for the model. |
protected EventListenerList |
listenerList
The event listener list. |
Fields inherited from class java.awt.Rectangle |
height,
width,
x,
y |
Fields inherited from class java.awt.geom.Rectangle2D |
OUT_BOTTOM,
OUT_LEFT,
OUT_RIGHT,
OUT_TOP |
Constructor Summary | |
DefaultCaret()
Constructs a default caret. |
Method Summary | |
void |
addChangeListener(ChangeListener l)
Adds a listener to track whenever the caret position has been changed. |
protected void |
adjustVisibility(Rectangle nloc)
Scrolls the associated view (if necessary) to make the caret visible. |
protected void |
damage(Rectangle r)
Damages the area surrounding the caret to cause it to be repainted in a new location. |
void |
deinstall(JTextComponent c)
Called when the UI is being removed from the interface of a JTextComponent. |
boolean |
equals(Object obj)
Compares this object to the specifed object. |
protected void |
fireStateChanged()
Notifies all listeners that have registered interest for notification on this event type. |
void |
focusGained(FocusEvent e)
Called when the component containing the caret gains focus. |
void |
focusLost(FocusEvent e)
Called when the component containing the caret loses focus. |
int |
getBlinkRate()
Gets the caret blink rate. |
protected JTextComponent |
getComponent()
Gets the text editor component that this caret is is bound to. |
int |
getDot()
Fetches the current position of the caret. |
Point |
getMagicCaretPosition()
Gets the saved caret position. |
int |
getMark()
Fetches the current position of the mark. |
protected Highlighter.HighlightPainter |
getSelectionPainter()
Gets the painter for the Highlighter. |
void |
install(JTextComponent c)
Called when the UI is being installed into the interface of a JTextComponent. |
boolean |
isSelectionVisible()
Checks whether the current selection is visible. |
boolean |
isVisible()
Determines if the caret is currently visible. |
void |
mouseClicked(MouseEvent e)
Called when the mouse is clicked. |
void |
mouseDragged(MouseEvent e)
Moves the caret position according to the mouse pointer's current location. |
void |
mouseEntered(MouseEvent e)
Called when the mouse enters a region. |
void |
mouseExited(MouseEvent e)
Called when the mouse exits a region. |
void |
mouseMoved(MouseEvent e)
Called when the mouse is moved. |
void |
mousePressed(MouseEvent e)
If button 1 is pressed, this is implemented to request focus on the associated text component, and to set the caret position. |
void |
mouseReleased(MouseEvent e)
Called when the mouse is released. |
protected void |
moveCaret(MouseEvent e)
Tries to move the position of the caret from the coordinates of a mouse event, using viewToModel(). |
void |
moveDot(int dot)
Moves the caret position to some other position. |
void |
paint(Graphics g)
Renders the caret as a vertical line. |
protected void |
positionCaret(MouseEvent e)
Tries to set the position of the caret from the coordinates of a mouse event, using viewToModel(). |
void |
removeChangeListener(ChangeListener l)
Removes a listener that was tracking caret position changes. |
protected void |
repaint()
Cause the caret to be painted. |
void |
setBlinkRate(int rate)
Sets the caret blink rate. |
void |
setDot(int dot)
Sets the caret position and mark to some position. |
void |
setMagicCaretPosition(Point p)
Saves the current caret position. |
void |
setSelectionVisible(boolean vis)
Changes the selection visibility. |
void |
setVisible(boolean e)
Sets the caret visibility, and repaints the caret. |
String |
toString()
Returns a String representing this
Rectangle and its values. |
Methods inherited from class java.awt.Rectangle |
add,
add,
add,
contains,
contains,
contains,
contains,
createIntersection,
createUnion,
getBounds,
getBounds2D,
getHeight,
getLocation,
getSize,
getWidth,
getX,
getY,
grow,
inside,
intersection,
intersects,
isEmpty,
move,
outcode,
reshape,
resize,
setBounds,
setBounds,
setLocation,
setLocation,
setRect,
setSize,
setSize,
translate,
union |
Methods inherited from class java.awt.geom.Rectangle2D |
add,
add,
add,
contains,
contains,
getPathIterator,
getPathIterator,
hashCode,
intersect,
intersects,
intersectsLine,
intersectsLine,
outcode,
setFrame,
setRect,
union |
Methods inherited from class java.awt.geom.RectangularShape |
clone,
contains,
contains,
getCenterX,
getCenterY,
getFrame,
getMaxX,
getMaxY,
getMinX,
getMinY,
intersects,
setFrame,
setFrame,
setFrameFromCenter,
setFrameFromCenter,
setFrameFromDiagonal,
setFrameFromDiagonal |
Methods inherited from class java.lang.Object |
finalize,
getClass,
notify,
notifyAll,
wait,
wait,
wait |
Field Detail |
protected EventListenerList listenerList
protected transient ChangeEvent changeEvent
Constructor Detail |
public DefaultCaret()
Method Detail |
protected final JTextComponent getComponent()
protected final void repaint()
This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.
protected void damage(Rectangle r)
r
- the current location of the caretpaint(java.awt.Graphics)
protected void adjustVisibility(Rectangle nloc)
nloc
- the new position to scroll toprotected Highlighter.HighlightPainter getSelectionPainter()
protected void positionCaret(MouseEvent e)
e
- the mouse eventprotected void moveCaret(MouseEvent e)
e
- the mouse eventpublic void focusGained(FocusEvent e)
e
- the focus eventFocusListener.focusGained(java.awt.event.FocusEvent)
public void focusLost(FocusEvent e)
e
- the focus eventFocusListener.focusLost(java.awt.event.FocusEvent)
public void mouseClicked(MouseEvent e)
e
- the mouse eventMouseListener.mouseClicked(java.awt.event.MouseEvent)
public void mousePressed(MouseEvent e)
e
- the mouse eventMouseListener.mousePressed(java.awt.event.MouseEvent)
public void mouseReleased(MouseEvent e)
e
- the mouse eventMouseListener.mouseReleased(java.awt.event.MouseEvent)
public void mouseEntered(MouseEvent e)
e
- the mouse eventMouseListener.mouseEntered(java.awt.event.MouseEvent)
public void mouseExited(MouseEvent e)
e
- the mouse eventMouseListener.mouseExited(java.awt.event.MouseEvent)
public void mouseDragged(MouseEvent e)
e
- the mouse eventMouseMotionListener.mouseDragged(java.awt.event.MouseEvent)
public void mouseMoved(MouseEvent e)
e
- the mouse eventMouseMotionListener.mouseMoved(java.awt.event.MouseEvent)
public void paint(Graphics g)
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).
g
- the graphics contextdamage(java.awt.Rectangle)
public void install(JTextComponent c)
c
- the componentCaret.install(javax.swing.text.JTextComponent)
public void deinstall(JTextComponent c)
c
- the componentCaret.deinstall(javax.swing.text.JTextComponent)
public void addChangeListener(ChangeListener l)
l
- the listenerCaret.addChangeListener(javax.swing.event.ChangeListener)
public void removeChangeListener(ChangeListener l)
l
- the listenerCaret.removeChangeListener(javax.swing.event.ChangeListener)
protected void fireStateChanged()
EventListenerList
public void setSelectionVisible(boolean vis)
vis
- the new visibilitypublic boolean isSelectionVisible()
public boolean isVisible()
Caret.isVisible()
public void setVisible(boolean e)
e
- the visibility specifierCaret.setVisible(boolean)
public void setBlinkRate(int rate)
rate
- the rate in milliseconds, 0 to stop blinkingCaret.setBlinkRate(int)
public int getBlinkRate()
Caret.getBlinkRate()
public int getDot()
Caret.getDot()
public int getMark()
Caret.getMark()
public void setDot(int dot)
dot
- the position >= 0Caret.setDot(int)
public void moveDot(int dot)
dot
- the position >= 0Caret.moveDot(int)
public void setMagicCaretPosition(Point p)
p
- the positiongetMagicCaretPosition()
,
UpAction
,
DownAction
public Point getMagicCaretPosition()
public boolean equals(Object obj)
obj
- the object to compare this font with.true
if the objects are equal;
false
otherwise.public String toString()
String
representing this
Rectangle
and its values.String
representing this
Rectangle
object's coordinate and size values.
|
JavaTM 2 Platform Standard Edition |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |