DocumentFilter:为什么调用 replace() 而不是 insertString()? [英] DocumentFilter: Why is replace() invoked and not insertString()?

查看:39
本文介绍了DocumentFilter:为什么调用 replace() 而不是 insertString()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个 DocumentFilter 子类,当我在 JTextComponent 中输入文本时,过滤器的 replace() 方法是被调用,而不是 insertString()(从未被调用).知道这是为什么吗?

I've implemented a DocumentFilter subclass, and when I type text into the JTextComponent, the replace() method of the filter is invoked, and not insertString() (which is never invoked). Any idea why that is?

推荐答案

insertString(...) 方法在您使用 Document.insertString(...) 方法.

The insertString(...) method is invoked when you update the Document directly, by using the Document.insertString(...) method.

replace(...) 方法在用户输入文本或用户调用操作(剪切、粘贴) 与文本字段关联.

The replace(...) method is invoked when the Document is updated by methods of the View (ie. the JTextField) when the user enters text or the user invokes Actions (cut, paste) associated with the text field.

我想总是替换更容易,然后让替换逻辑检查是否需要删除/插入.

I guess its is easier to always to a replace and then let the replace logic check to see if remove/insert is required.

这篇关于DocumentFilter:为什么调用 replace() 而不是 insertString()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆