• Post Reply Bookmark Topic Watch Topic
  • New Topic

Formatting of textfield

 
Ash Kondhalkar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have requirement of formatting a textfield. Formatting requirements are,
1. Number(integer/double) formatting:- If user enters 1234 then on focus lost it should change to 1,234. Also if user enters a double value as 1234.56 it should change to 1,234.56.
2. % value formatting:- If in a percent field user enters lets say 0.05 then on focus lost it should change to 5%
3. currency formatting:- if in a currency field user enters lets say 2345, it should change to $2345 on focus lost.

I know jdk provides JFormattedTextfield, but there is a issue with JFormattedTextfield.
a. If I apply NumberFormat.getNumberInstance() to JFormattedTextfield and enter 1234 then on focus lost it does format it as 1,234. But if I try to remove the complete data i.e. 1,234 it reverts back to 1,234. The isssue is "It does not allow emptying the textfield".

Thanks in anticipation
Ashwin


Regard
Ashwin
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you mean you want a single textfield to handle all scenarios - not possible.

for the moment, work on only one scenario at a time, and post what you've tried
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well it seems to me that JFormattedTextField can handle everything you mention without too much trouble. If new JFormattedTextField(NumberFormat.getInstance()) doesn't behave the way you want, then configure or override a NumberFormatter and pass that in instead.

It shouldn't be too hard to "allow emptying the textfield."
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> user enters 1234 then on focus lost it should change to 1,234
> user enters lets say 2345, it should change to $2345 on focus lost.

I can't see how even a JFormattedTextfield could handle both scenarios together.
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Dunn, given the "if in a percent field"/"if in a currency field" clauses, I didn't get the impression that "both scenarios" are intended for a single instance.

Mr. Kondhalkar, you don't say what you want it to do when the user removes "the complete data." If you don't want it to revert back to 1,234 what should it do instead?
[ December 31, 2006: Message edited by: Brian Cole ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I didn't get the impression that "both scenarios" are intended for a single instance.

Just re-read the thread, and agree.

My initial impression was based on the OP's "I have requirement of formatting a textfield." (singular),
but he/she probably just wants to be spoon-fed the tutorial.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!