Detecting JTextField “deselect” event

大城市里の小女人 提交于 2020-02-09 04:41:41

问题


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.


回答1:


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

Id suggest a read on How to Use the Focus Subsystem - Validating Input

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


来源:https://stackoverflow.com/questions/14305921/detecting-jtextfield-deselect-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!