开发者

Validation of input integer

How can i validate an input in the JTEXTfield? If I want it in the ff: 1. An integer 2. Only contains 0-12

For example of valid input (hour and minute), Hour = 开发者_C百科12 Minute = 30

Example of invalid input (hour and minute), Hour != 14 Minute != 78


JTEXTField provides several events, hook up to the 'lost focus' event. Then when the users starts typing somewhere else an event gets fired which calls your method that can do these checks for you. (Just try to parse the string to an integer, then do the checks >0 < 12 etc...).


Use JFormattedTextField. You can specify desired format e.g. NumberFormat or DateFormat for the field and user's input will be checked automatically.


Not a real answer to the original question, but you may consider using a JSpinner instead of a JTextField in your case.

Otherwise, if you want to keep JTextField, then you should use a DocumentListener to prevent user from typing bad input.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜