如何编写文档监听器

原文: https://docs.oracle.com/javase/tutorial/uiswing/events/documentlistener.html

Swing 文本组件使用 Document 来表示其内容。当文档内容以任何方式更改时,都会发生文档事件。您将文档监听器附加到文本组件的文档,而不是文本组件本身。有关详细信息,请参阅实现文档过滤器

以下示例演示两个纯文本组件上的文档事件。

This screenshot demonstrates the output of DocumentEventDemo example.


Try this:

  1. 单击“启动”按钮以使用 Java™Web Start下载 JDK 7 或更高版本)运行 DocumentEventDemo。或者,要自己编译并运行示例,请参考示例索引Launches the DocumentEventDemo example

  2. 键入窗口左上角的文本字段或文本字段下方的文本区域。 为每个键入的字符触发一个文档事件。

  3. 使用退格键删除文本。 为每个键入的退格键触发一个文档事件。
  4. 选择文本,然后通过键入退格键或使用CTRL-X(剪切)等键盘命令将其删除。 整个删除都会触发一个文档事件。
  5. 使用CTRL-C(复制)和CTRL-V(粘贴)等键盘命令将文本从一个文本组件复制到另一个文本组件。 无论粘贴的文本长度如何,都会针对整个粘贴操作触发一个文档事件。如果在发出粘贴命令之前在目标文本组件中选择了文本,则会触发其他文档事件,因为首先删除所选文本。

您可以在 DocumentEventDemo.java 中找到演示代码。这是演示文档的事件处理代码:

  1. public class DocumentEventDemo ... {
  2. ...//where initialization occurs:
  3. textField = new JTextField(20);
  4. textField.addActionListener(new MyTextActionListener());
  5. textField.getDocument().addDocumentListener(new MyDocumentListener());
  6. textField.getDocument().putProperty("name", "Text Field");
  7. textArea = new JTextArea();
  8. textArea.getDocument().addDocumentListener(new MyDocumentListener());
  9. textArea.getDocument().putProperty("name", "Text Area");
  10. ...
  11. class MyDocumentListener implements DocumentListener {
  12. String newline = "\n";
  13. public void insertUpdate(DocumentEvent e) {
  14. updateLog(e, "inserted into");
  15. }
  16. public void removeUpdate(DocumentEvent e) {
  17. updateLog(e, "removed from");
  18. }
  19. public void changedUpdate(DocumentEvent e) {
  20. //Plain text components do not fire these events
  21. }
  22. public void updateLog(DocumentEvent e, String action) {
  23. Document doc = (Document)e.getDocument();
  24. int changeLength = e.getLength();
  25. displayArea.append(
  26. changeLength + " character" +
  27. ((changeLength == 1) ? " " : "s ") +
  28. action + doc.getProperty("name") + "." + newline +
  29. " Text length = " + doc.getLength() + newline);
  30. }
  31. }

文档监听器不应修改文档的内容;在收听者收到更改通知时,更改已完成。而是编写一个覆盖insertStringremove方法或两者的自定义文档。有关详细信息,请参阅聆听文档更改

DocumentListener没有适配器类。

方法 目的
changedUpdate(DocumentEvent) 当收听文档中某些文本的样式发生更改时调用。这种事件仅从StyledDocument触发 - PlainDocument不会触发这些事件。
insertUpdate(DocumentEvent) 将文本插入收听的文档时调用。
removeUpdate(DocumentEvent) 从收听的文档中删除文本时调用。

每个文档事件方法都传递一个实现DocumentEvent接口的对象。通常,这是 DefaultDocumentEvent 的实例,在AbstractDocument中定义。

方法 目的
文件 getDocument() 返回触发事件的文档。请注意,DocumentEvent接口不会从EventObject继承。因此,它不继承getSource方法。
int getLength() 返回更改的长度。
int getOffset() 返回更改的第一个字符的文档中的位置。
ElementChange getChange(Element) 返回有关文档中哪些元素已更改以及如何更改的详细信息。 ElementChangeDocumentEvent接口中定义的接口。
EventType getType() 返回发生的更改类型。 EventTypeDocumentEvent接口中定义的类,它枚举了文档可能发生的更改:插入文本,删除文本和更改文本样式。

使用文档监听器的示例

下表列出了使用文档监听器的示例。

在哪里描述 笔记
DocumentEventDemo 这个部分 报告文档字段和文本区域的文档上发生的所有文档事件。一个监听器侦听两个文本组件,并使用文档上的客户端属性来确定触发事件的组件。
TextComponentDemo 聆听文件的变更 每次收听文档中的文本发生更改时,都会更新更改日志。此示例中的文档支持样式文本,因此在此示例中调用changedUpdate。需要此附加源文件: DocumentSizeFilter