views:

37

answers:

1

Hi All,

I want to set upper bound for a number in formatter textfield. Let's say that there is a string which has following format:

036 12' 23.67"

The first number must be less than 180, so when the user tries to type a number greater than 180, it must be masked. I used MaskFormatter to format string as above, but I couldn't set an upper bound for the first number. How can I achieve this. Thanks.

+1  A: 

I don't know how to do this directly with a JFormattedTextField.

One solution is to use an InputVerifier so that when the user tabs away they will get an error message and focus is returned to the text field.

Another approach is to add a DocumentFilter to the AbstractDocument. Then you can edit the text before it is added to the Document. Read the section from the Swing tutorial on Text Component Features for more information.

camickr
+1 Capital idea! An implementation of `HeadingFormat` might be worthwhile; here's a `DateTimeVerifier` for reference: http://stackoverflow.com/questions/2234726/jformattedtextfield-input-time-duration-value/2241997#2241997
trashgod
thanks for your answer, but the invalid value should be masked while typing just like MaskFormatter does. The invalid number shouldn't be displayed in textfield. Any other suggestions?
Thats what a DocumentFilter is used for. It intercepts the character as it is typed.
camickr