javaswingfocuslistenersinputverifier

Swing focus lost inputverifier


I have a frame with some JTextFields on it. When the user tabs out from a JTextField, I execute custom logic in the FocusTraversalPolicy, which sets some properties and based on them the FocusTraversalPolicy decides where should the focus go next. So far so good.

But I want this custom logic to execute everytime the JTextField loses focus(in an InputVerifier, because there is also validation in it).

Now the problem and the question: If the user leaves the field with the mouse the InputVerifier will be triggered and everything will be fine. But if the user presses TAB, both the FocusTraversalPolicy and the InputVerifier will be triggered and the custom logic will be executed twice, which I don't want. How can I detect in the InputVerifier that TAB was already pressed and don't execute anything? I want to execute the custom logic in the InputVerifier only when the field is left with the mouse. Or is there a better solution?

Hope my question is clear.

Regards, Petar


Solution

  • I ended up setting a boolean flag from the FocusTraversalPolicy if the custom logic is executed. Then in the InputVerifier if it is already executed I don't execute it again. Then I reset the flag.