关于从 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) using DocumentListener
for
1) using DocumentListener
for
从
JTextComponent
输出到 GUI
HightLighter
或 Styled text
2) DocumentFilter
用于过滤
不需要的字符,
unwanted chars,
字符序列,
这些过滤后的字符可能是
these filtered chars could be
替换为另一个字符(或定义的字符序列)
replaced with another char (or with defined chars sequence)
移除(永远不会在JTextComponent
中显示)
removed (never will be displayed in the JTextComponent
)
3) 提供 JFormattedTextField
的类似功能可以输入到 JTextComponent
仅字符 0 - 9
, decimal separator
, 否定唱
,
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屋!