# 如何写一个插入符号听众 > 原文: [https://docs.oracle.com/javase/tutorial/uiswing/events/caretlistener.html](https://docs.oracle.com/javase/tutorial/uiswing/events/caretlistener.html) 当*插入符号 _ - 指示插入点的光标 - 在文本组件中移动或文本组件中的选择发生变化时,会发生插入事件。例如,文本组件的文档可以在插入或删除文本时启动插入符事件。您可以使用`addCaretListener`方法将插入符监听器附加到任何`JTextComponent`子类的实例。 * * * **Note:** An alternate way of detecting caret changes is to attach a listener directly to the caret object itself rather than to the text component that manages the caret. A caret fires change events (_not_ caret events), so you would need to write a [change listener](changelistener.html) rather than a caret listener. 以下是来自名为`TextComponentDemo`的应用程序的插入事件处理代码: ```java ... //where initialization occurs CaretListenerLabel caretListenerLabel = new CaretListenerLabel("Caret Status"); ... textPane.addCaretListener(caretListenerLabel); ... protected class CaretListenerLabel extends JLabel implements CaretListener { ... //Might not be invoked from the event dispatching thread. public void caretUpdate(CaretEvent e) { displaySelectionInfo(e.getDot(), e.getMark()); } //This method can be invoked from any thread. It //invokes the setText and modelToView methods, which //must run in the event dispatching thread. We use //invokeLater to schedule the code for execution //in the event dispatching thread. protected void displaySelectionInfo(final int dot, final int mark) { SwingUtilities.invokeLater(new Runnable() { public void run() { if (dot == mark) { // no selection ... } }); } } } ``` * * * **Note:** The `caretUpdate` method is not guaranteed to be called in the event-dispatching thread. To use any methods inside of `caretUpdate` that update the GUI special handling is required to ensure they are executed on the event-dispatching thread. You can do this by wrapping the code inside a `Runnable` and calling `SwingUtilities.invokeLater` on that `Runnable`. * * * 您可以在[示例索引中找到使用 Swing Components](../examples/components/index.html#TextComponentDemo) 的`TextComponentDemo`源文件的链接。有关该程序的插入符号监听器方面的讨论,请参阅 [](../components/generaltext.html) [文本组件功能](../components/generaltext.html)中的[听取插入符号和选择更改](../components/generaltext.html#caret)。 *因为`CaretListener`只有一个方法,所以它没有相应的适配器类。* | 方法 | 目的 | | :-- | :-- | | [caretUpdate(CaretEvent)](https://docs.oracle.com/javase/8/docs/api/javax/swing/event/CaretListener.html#caretUpdate-javax.swing.event.CaretEvent-) | 当收听组件中的插入符移动或收​​听组件中的选择发生更改时调用。 | | 方法 | 目的 | | :-- | :-- | | [int getDot()](https://docs.oracle.com/javase/8/docs/api/javax/swing/event/CaretEvent.html#getDot--) | 返回插入符的当前位置。如果选择了文本,则插入符号标记选择的一端。 | | [int getMark()](https://docs.oracle.com/javase/8/docs/api/javax/swing/event/CaretEvent.html#getMark--) | 返回选择的另一端。如果未选择任何内容,则此方法返回的值等于`getDot`返回的值。请注意,点不能保证小于标记。 | | [对象 getSource()](https://docs.oracle.com/javase/8/docs/api/java/util/EventObject.html#getSource--) (`java.util.EventObject`中的*)_ | 返回触发事件的对象。 | ## 使用 Caret Listeners 的示例 下表列出了使用插入符监听器的示例。 | 例 | 在哪里描述 | 笔记 | | :-- | :-- | :-- | | [`TextComponentDemo`](../examples/components/index.html#TextComponentDemo) | [听取插入符号和选择更改](../components/generaltext.html#caret) | 使用监听器标签显示插入符号和选择状态。 | [« Previous](actionlistener.html) • [Trail](../TOC.html) • [Next »](changelistener.html)