简体   繁体   中英

Swing: Ignore text box focus lost event when specific button clicked

I have a swing textbox that contains a value which is validated on focuslost event. The form has many other controls, including a cancel button. I want validation to be skipped in case focus has been lost due to clicking of cancel button. How can this be done?

I would suggest to remove the code which performs validation on focusLost and either implement your validation in a DocumentListener , or opt for a JFormattedTextField which has built-in validation.

Also from a user perspective I prefer on-the-fly validation instead of on focus-lost. When I start typing, it is nice to see when your input becomes incorrect. Further, you could opt to disable the OK button while there is invalid input, and only leave the cancel button enabled. This of course requires that your validation does not pop-up messages but just adds an indication in/next to the input fields

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM