• 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

JFormattedTextField and MaskFormatter woes

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Hoping a Swing guru on here might shed a little light on a problem I have.

I've got little or no time to get a solution for this into a clients application and so far... well, I'm losing lots of hair - so any help on this would be gratefully appreciated!!!

Basically, all I need to do is permit the user to enter an amount of money into a text field. However, it needs to be formatted on the fly so that:

If they press 1, the output appears as 0.01
If they then press 2, the output appears as 0.12
If they then press 3, the output appears as 1.23
and so on.

The idea is that it automatically determines where the decimal point is - without the user having to enter it themselves.

The formatter has to permit only numeric characters and support the deletion of characters.

I've tried all sorts of solutions:

MaskFormatter - starts entering the characters from left to right - no good.
DefaultFormatterFactory - doesn't format the field until focus changes or enter pressed
KeyListener - can detect a new keypress but can't change the text from within the event without it appearing twice
I've even tried messing around with the Caret, but to no avail.

Help!

This should be a simple thing to do, but Swing is baffling me on this one

 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here's something quick'n'dirty, so there's probably (definitely) a bucketload
of ways to do it better (but it might give you something to play around with).



all works on the character entered being the last character
e.g. enter 1 2 3 4 5 will produce
0.01
0.12
1.23
12.34
123.45

if you enter 1 2 4 5 , then put the caret after the 2, and enter 3, you will get
0.01
0.12
1.24
12.45
124.53//probably not what you want
 
Andy White
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Michael - I appreciate it.

Not quite there, but as you say, it does give me something to play around with.
 
reply
    Bookmark Topic Watch Topic
  • New Topic