• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Avoid user entering more than 2 decimal figures in textfield

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


I have a textfield which has to accept the currency. While keying in the the values, the user when he tries to enter a 3rd decimal value, it should consume. How do i do this?

Can anybody help me in this


regarsd
smriti
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello !
You did not say if you are using swing,AWT or SWT, anyway, I'm using SWT
and I believe that basiclly this shuld be in every one of them (maybe a not excactly the same name)
Anyway , use this:

( Text myText = new Text(composite,SWT.NONE) )
myText.setTextLimit(2);
this will not let the user enter more that 2 digits
Hope this is what you need
Dave
 
Smriti Anchu
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dave,
I am using swing. I tried your code,

I dont want to limit the numbers to only 2 but i want the user not to enter more than 2 decimals.
For ex. the number can be 4311.22 and not 4311.222222. I want to limit the number of digits in the decimal place to 2.


thanks
smriti
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the JFormattedTextField.
 
Smriti Anchu
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Is there any other way other than using JFormattedTextField ?


smriti
 
Smriti Anchu
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i tried using formattedTextField..

but if i give the format to be of
JFormattedTextField tamount;
MaskFormatter amount;

try {
amount = new MaskFormatter("################.##");
}
catch(IOException e){}

tamount = new JFormattedTextField(amount);


When i use the above, the application wants me to enter all the 16 numbers. and 2 fraction digits. How do i avoid this?

regards
smriti
 
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use a MaskFormatter; use a NumberFormat and the setMaximumFractionDigits method.
 
Smriti Anchu
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do i use a NumberFormat

I tried in the following manner


But when i start typing digits into the textfield it is allowing me to enter more than 2 decimal digits.

regards s
smriti
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True, but when you move away from the field the value should be validated, the result depending on whether you have chosen to COMMIT or REVERT.
 
Smriti Anchu
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do i validate? Can u give me some example.

regards
smriti
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic