Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Avoid user entering more than 2 decimal figures in textfield  RSS feed

 
Smriti Anchu
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • 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
 
Dave Jones
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the JFormattedTextField.
 
Smriti Anchu
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
How do i validate? Can u give me some example.

regards
smriti
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!