Detecting JTextField “deselect” event

后端 未结 1 591
孤独总比滥情好
孤独总比滥情好 2020-11-29 11:34

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

相关标签:
1条回答
  • 2020-11-29 12:29

    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
    0 讨论(0)
提交回复
热议问题