如何根据用户类型获取JTextField内容的长度? [英] How can I get the length of a JTextField's contents as the user types?
问题描述
JTextField有一个keyTyped事件,但似乎在它触发单元格的内容还没有改变。
JTextField has a keyTyped event but it seems that at the time it fires the contents of the cell have not yet changed.
因为.length()是如果在这里阅读,总是错的。
Because of that .length() is always wrong if read here.
在关键笔划之后,用户看到的长度必须有一个简单的方法?
There must be a simple way of getting the length as it appears to the user after a key stroke?
推荐答案
这可能不是最佳方式(而且已经有一段时间了),但是在过去,我已经添加了一个DocumentListener到JTextField,事件(插入,更新,删除)I:
This is probably not the optimal way (and it's been a while), but in the past, I have added a DocumentListener to the JTextField and on any of the events (insert, update, remove) I:
evt.getDocument().getLength()
其中返回文本字段内容的总长度。
Which returns the total length of text field's contents.
这篇关于如何根据用户类型获取JTextField内容的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!