检测JTextField“取消选择”事件 [英] Detecting JTextField "deselect" event

查看:142
本文介绍了检测JTextField“取消选择”事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有某种方法可以检测是否取消选择了JTextField,即选择了字段WAS,但现在您已经选择了其他内容。我之所以这样做,是因为我想在用户表单中检查任何非法字符,然后再尝试提交数据。如果有更简单的方法,而不是我试图解决它的方式,我很乐意接受启蒙。

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:


我想在用户b $ b尝试提交数据之前检查我的用户表单是否有任何非法字符

I want to check my users forms for any illegal characters before they try to submit their data

我建议阅读如何使用焦点子系统 - 验证输入

FocusAdapter 更好的选择 - 为valida量身定做ting /限制用户输入:

Better options than a FocusAdapter - tailored made for validating/restricting user input:

  • DocumentFilter. See here and this variation for an example.
  • InputVerifier. See here for an example
  • JFormattedTextField. See here for an example

这篇关于检测JTextField“取消选择”事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆