澄清从DocumentListener修改Document的内容 [英] Clarification on modifying a Document's contents from a DocumentListener
问题描述
来自文本组件的Swing教程:
From the Swing tutorial on text components:
您可能希望在文档侦听器中更改文档的文本。但是,您永远不应该从文档侦听器中修改文本组件的内容。如果你这样做,该程序可能会陷入僵局。相反,您可以使用带格式的文本字段或提供文档过滤器。
You may want to change the document's text within a document listener. However, you should never modify the contents of a text component from within a document listener. If you do, the program will likely deadlock. Instead, you can use a formatted text field or provide a document filter.
我很困惑。那么改变文本的正确方法是什么,例如。作为KeyEvent的结果?
I am confused. So what is the correct way to change the text eg. as a result of a KeyEvent ?
推荐答案
1)使用 DocumentListener
-
从
JTextComponent
输出到GUI
HightLighter
或样式文字
2) DocumentFilter
用于过滤
-
不受欢迎的字符,
unwanted chars,
字符序列,
这些过滤后的字符可能是
these filtered chars could be
-
替换为另一个char(或定义的字符序列)
replaced with another char (or with defined chars sequence)
已删除(永远不会显示在 JTextComponent
)
removed (never will be displayed in the JTextComponent
)
3)similair funcionality提供 JFormattedTextField
可以输入到 JTextComponent
只有字符 0 - 9
,小数点分隔符
,否定唱
,
3) similair funcionality to provide JFormattedTextField
is possible to input to the JTextComponent
only chars 0 - 9
, decimal separator
, negative sing
,
4)
那么什么是改变文本的正确方法,例如。作为KeyEvent的结果?
4) So what is the correct way to change the text eg. as a result of a KeyEvent ?
这篇关于澄清从DocumentListener修改Document的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!