Over the course of developing a few different Swing apps I've, of course, found the need to have properly formatted and validated text fields.
I've found using JFormattedTextField to be easy, but it has less that satisfactory results.
The main problems I have are that you are unable to leave the field blank once something has been typed into it, and when editing the text you have to over type rather than delete and reenter (which can be very confusing to users).
I have gotten around this by using JFormattedTextFields sparingly and doing a lot of work with custom Documents and InputVerifiers. This always feels like a hack (mainly because I don't think I'm really doing right), but it works.
So the question (finally) is:
Is there a good, elegant, way to create custom formatted fields. Is there a tutorial out there (I haven't seen one), does somebody have some sample code.
One example of the type of formatting is a phone number If a user types in just 7 numbers they see 555-5555 If a user types in 10 numbers they see (555) 555-5555 and the ()- are all added automatically. The user can delete and add numbers at any point and the formatting isn't broken.
Originally posted by Michael Dunn: > If a user types in just 7 numbers they see > 555-5555
I'm not quite following along with this.
If I have a JFormattedTextField with a MaskFormatter of "(###) ###-####"
the display is "( ) - "
and, regardless of where, or how many, characters are typed, the '() -' remain
I think the former is what Steven actually *wants*.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Steven, I share your concerns. The standard behaviour of JFormattedTextField often is inappropriate, and customizing it (in other than the most trivial ways) very hard to impossible, in our experience. We are more and more going back to our own implementations.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Maybe it would be more constructive if I showed an example and maybe somebody could offer some advice for improvement.
This is the code I used to get the functionality I wanted for phone numbers. The code for the InputVerifier isn't too bad, but I think the Document is just ugly.
P.S. I should mention I use this with a standard JTextField.
Here is the Custom Document
And here is the code for the Input Verifier
[ January 19, 2006: Message edited by: Steven Bell ]
I haven't tried it, but I don't think the above code handles inserts of more than one character. For example if a phone number is copy/pasted into the field.
I think I'm going to do some work on this. I'm going to look at making the Document class better and see if I can make a Formatter to put in a JFormattedTexField. Hopefully one of those can be done cleanly. It will probably take me a little while to get something workable, but I'll post my results here when I'm done.