DocumentFilter: Why is replace() invoked and not insertString()?

北战南征 提交于 2020-12-30 09:47:18

问题


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?


回答1:


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

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.



来源:https://stackoverflow.com/questions/23525441/documentfilter-why-is-replace-invoked-and-not-insertstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!