简体   繁体   English

摇摆:单击特定按钮时,忽略文本框焦点丢失事件

[英]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. 我有一个swing文本框,其中包含在focuslost事件上验证的值。 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. 我建议删除在focusLost上执行验证的代码,或者在DocumentListener实现您的验证,或者选择具有内置验证的JFormattedTextField

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 当然,这要求您的验证不弹出消息,而只是在输入字段中/旁边添加一个指示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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