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 exampleJFormattedTextField
. See here for an example