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
If a user types in 10 numbers they see
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
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*.
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 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.