java 如何在用户键入时获取 JTextField 内容的长度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/401598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the length of a JTextField's contents as the user types?
提问by Allain Lalonde
JTextField has a keyTyped event but it seems that at the time it fires the contents of the cell have not yet changed.
JTextField 有一个 keyTyped 事件,但似乎在它触发时单元格的内容尚未更改。
Because of that .length() is always wrong if read here.
因此,如果在这里阅读 .length() 总是错误的。
There must be a simple way of getting the length as it appears to the user after a key stroke?
必须有一种简单的方法来获取用户在击键后显示的长度吗?
回答by Sean Bright
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:
这可能不是最佳方式(而且已经有一段时间了),但在过去,我已将 DocumentListener 添加到 JTextField 和任何事件(插入、更新、删除)我:
evt.getDocument().getLength()
Which returns the total length of text field's contents.
它返回文本字段内容的总长度。
回答by VonC
This may be related to this "bug" (or rather "feature")
这可能与此“错误”(或更确切地说是“功能”)有关
The listeners are notified of the key events prior to processing them to allow the listeners to "steal" the events by consuming them. This gives compatibility with the older awt notion of consuming events.
The "typed" event does not mean text was entered into the component. This is NOT a bug, it is intended behavior.
在处理关键事件之前,将关键事件通知侦听器,以允许侦听器通过使用它们来“窃取”事件。这提供了与消费事件的旧 awt 概念的兼容性。
“typed”事件并不意味着文本被输入到组件中。这不是错误,而是预期行为。
A possible solution is to listen to an associated Document
一种可能的解决方案是侦听关联的文档
// Listen for changes in the text
myTextField.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
// text was changed
}
public void removeUpdate(DocumentEvent e) {
// text was deleted
}
public void insertUpdate(DocumentEvent e) {
// text was inserted
}
});
Note this works no matter how the text gets changed; via a clipboard cut/paste, progamatic "setText()" on the TextField, or the user typing into the field on the UI.
请注意,无论文本如何更改,这都有效;通过剪贴板剪切/粘贴、TextField 上的程序化“setText()”或用户在 UI 上的字段中键入内容。
回答by Tom Hawtin - tackline
KeyEvents are low-level events that are not appropriate here [that sounds familiar].
KeyEvents 是不适合这里的低级事件 [听起来很熟悉]。
How does the JTextFieldsystem know that a character has been typed? Through a key typed event (IIRC, done through the PL&F). Does the event get dispatched to the system listener before your listener? It might or might not do.
JTextField系统如何知道某个字符已被输入?通过键类型事件(IIRC,通过 PL&F 完成)。事件是否在您的侦听器之前被分派到系统侦听器?它可能会也可能不会。
In this case, you probably want to go to the Documentand add a higher-level listener. With Swing it's a good idea to go for the model early - the 'J' class interfaces are incoherent. If you are intercepting input data, then you probably want a custom model (or in the case of Documenta DocumentFilter).
在这种情况下,您可能希望转到Document并添加更高级别的侦听器。使用 Swing 时,最好尽早使用模型 - 'J' 类接口是不连贯的。如果你是截取输入数据,那么你可能想自定义模型(或在案件Document一DocumentFilter)。
回答by Akhil P M Paruthikudiyil
Use this code:
使用此代码:
public void jTextField6KeyReleased(java.awt.event.KeyEvent evt)
{
System.out.println(jTextField6.getText().length());
}

