检测 JTextField “取消选择"事件 [英] Detecting JTextField "deselect" event
问题描述
有什么方法可以检测 JTextField 是否被取消选择,即该字段被选中但现在您选择了其他内容.我想要这样做的原因是因为我想在他们尝试提交数据之前检查我的用户表单是否有任何非法字符.如果有一些更简单的方法来做到这一点,而不是我试图解决的方法,我会很乐意接受启示.
Is there some way to detect if a JTextField is deselected, i.e. the field WAS selected but now you have selected something else. The reason why I want to do this is because I want to check my users forms for any illegal characters before they try to submit their data. If there is some easier way to do that, instead of the way I'm trying to solve it, I'll gladly accept enlightenment.
推荐答案
起初虽然使用 FocusAdapter
并覆盖 focusLost(FocusEvent fe)
当 JTextField
失去焦点时将被调用,即另一个组件被选中.
At first though use a FocusAdapter
and override focusLost(FocusEvent fe)
which will be called when JTextField
loses focuses, i.e another component is selected.
但是因为你有一个目的:
However because you have a purpose:
我想先检查我的用户表单是否有任何非法字符尝试提交他们的数据
I want to check my users forms for any illegal characters before they try to submit their data
我建议阅读如何使用焦点子系统- 验证输入
比 FocusAdapter
更好的选项 - 为验证/限制用户输入量身定制:
Better options than a FocusAdapter
- tailored made for validating/restricting user input:
DocumentFilter
.请参阅此处和这个变体示例.InputVerifier
.有关示例,请参见此处JFormattedTextField
.请参阅此处 举例
DocumentFilter
. See here and this variation for an example.InputVerifier
. See here for an exampleJFormattedTextField
. See here for an example
这篇关于检测 JTextField “取消选择"事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!