Java Swing: Implementing a validity check of input values

前端 未结 4 928
猫巷女王i
猫巷女王i 2020-12-31 16:08

In my Swing application, the user must insert numbers and values, before switching to the next window. Now as a clean program should, I check every input if its valid or not

4条回答
  •  离开以前
    2020-12-31 16:29

    I prefer to use an improved version of JFormattedTextField. By improved I mean better caret behavior, validation on each change to provide immediate user feedback (e.g. change background color when input is invalid), ... . That combined with a button which is disabled until the input is valid.

    Main benefits over the "click the button and see error messages appear":

    • instant feedback for the user .If web-applications can avoid a round-trip to the server and use javascript for immediate feedback, there is no excuse for not having that in a desktop applications. Pressing a button to validate is so '90.
    • visual feedback is important, and better then the InputVerifier which just avoids changing focus
    • highly reusable component. Just make sure your 'utility code pack' contains a bunch of Formats (for dates, doubles, integers, ranges, ... ) and you can handle almost any situation
    • due to the use of Formats, easy adjustable for different Locales
    • You never need to parse the input after the JFormattedTextField. All the parsing code is contained in the format, and you can simply use JFormattedTextField#getValue
    • All validation is taken care of by the JFormattedTextField. You know that the value you retrieve using getValue is a valid one

提交回复
热议问题