JTextField listener when text changes that modifies textField's text

时光总嘲笑我的痴心妄想 提交于 2019-12-22 10:26:36

问题


Any idea how I can add a text listener on to a JTextField, that triggers when the text changes and as an action modifies the JTextField's text.

I have tried using addInputMethodListener which seems to be appropriate but it doesn't seem to work. I have also tried textField.getDocument().addDocumentListener() but this throws java.lang.IllegalStateException: Attempt to mutate in notification when I try to modify textField's text.


回答1:


DocumentListeners do not permit the modification of the underlying document of the JTextComponent. You are looking for a DocumentFilter.

Example



来源:https://stackoverflow.com/questions/14303695/jtextfield-listener-when-text-changes-that-modifies-textfields-text

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