aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Disappointed with JFormattedTextField Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Disappointed with JFormattedTextField" Watch "Disappointed with JFormattedTextField" New topic
Author

Disappointed with JFormattedTextField

Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
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.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> 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
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I think the former is what Steven actually *wants*.

Thanks, now it makes sense (I read it as the description of the problem)
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
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 ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
this is not a lot different to what you posted.

it changes the format backwards, on delete or backspace.
it overwrites when full/complete.
only problem is caret position, can't quite get it right.

Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
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.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> but I don't think the above code handles inserts of more than one character.

It can't receive inserts of more than 1 character.

paste is blocked here
JTextField tf = new JTextField(10){public void paste(){}};
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Disappointed with JFormattedTextField