• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Disappointed with JFormattedTextField

 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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*.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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(){}};
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
I don't even know how to spell CIA. But this tiny ad does:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic