假设我在 JPanel 中有一个 JEditorPane。我希望能够在每次用户在 JEditorPane 组件中输入/粘贴文本时执行回调。我应该创建什么类型的监听器?
最佳答案
您可以使用 DocumentListener 来通知文档的任何更改。
由于我还不能发表评论,我只想说尽可能使用监听器比覆盖类更好,就像上面给出的覆盖 PlainDocument 的示例。
监听器方法适用于 JTextField、JTextArea、JEditorPane 或 JTextPane。默认情况下,编辑器 Pane 使用 HTMLDocument,而 JTextPane 使用 StyledDocument。因此,通过强制组件使用 PlainDocument,您将失去功能。
如果您担心在将文本添加到文档之前编辑文本,那么您应该使用 DocumentFilter
关于java - 我应该为什么 JEditorPane 事件创建监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1082300/