问题描述
我知道如何在JTextField上添加文本侦听器,在文本发生变化时触发,并在动作中修改JTextField的文本。
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.
我试过了使用 addInputMethodListener
这似乎是合适的,但它似乎不起作用。我也尝试了 textField.getDocument()。addDocumentListener()
但是这会抛出 java.lang.IllegalStateException:尝试在通知中变异
当我尝试修改textField的文本时。
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.
推荐答案
DocumentListeners
不允许修改 JTextComponent
的基础文档。您正在寻找。
DocumentListeners
do not permit the modification of the underlying document of the JTextComponent
. You are looking for a DocumentFilter.
这篇关于文本更改时的JTextField侦听器修改textField的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!