[英]Java equivalent to C# TextBox TextChanged event
in C# there is an event for textboxes as follows 在C#中,文本框有一个事件,如下所示
private void fooText_TextChanged(object sender, EventArgs e)
{
//do something
}
the code in the fooText_TextChanged is fired once the text within the textbox is altered. 一旦文本框中的文本被更改,就会触发fooText_TextChanged中的代码。
What is the java equivalent to this? 什么是Java相当于此? Or how can something similar to this be achieved in java?
或者如何在java中实现与此类似的东西?
Thanks for any feedback/help/advice. 感谢您提供任何反馈/帮助/建议。
For Swing, If you wanted to be notified after the text component's text had changed, you'd use a DocumentListener that was added to the JTextComponent's Document. 对于Swing,如果希望在文本组件的文本发生更改后得到通知,则使用已添加到JTextComponent的Document的DocumentListener。 eg,
例如,
JTextField myField = new JTextField();
myField.getDocument().addDocumentListener(new DocumentListener() {
public void removeUpdate(DocumentEvent e) {
// TODO add code!
}
public void insertUpdate(DocumentEvent e) {
// TODO add code!
}
public void changedUpdate(DocumentEvent e) {
// TODO add code!
}
});
If on the other hand, you wanted to check text before it has been committed to the text component, you'd add a DocumentFilter to the JTextComponent's Document. 另一方面,如果要在将文本提交到文本组件之前检查文本,则需要将DocumentFilter添加到JTextComponent的Document中。
I recommend registering a DocumentListener
to your component's document . 我建议将
DocumentListener
注册到组件的文档中 。 Therein, you'll listen for DocumentEvent
s. 在那里,你会听
DocumentEvent
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.